Napkin

Additional

Language
Java
Version
0.7.0 (Jan 7, 2017)
Created
May 12, 2016
Updated
Mar 5, 2017
Owner
Aleksander Mielczarek (AleksanderMielczarek)
Contributor
Aleksander Mielczarek (AleksanderMielczarek)
1
Activity
Badge
Generate
Download
Source code
APK file

Advertising

Napkin

Common scopes, qualifiers and few utilities for Dagger 2. Scopes and qualifiers can be used also with toothpick.

Usage

Add it in your root build.gradle at the end of repositories:

allprojects {
 repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Core

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:napkin:0.7.0'
}
  • with Napkin you have easy access to Component in Application
public class MyApplication extends Application implements ComponentProvider<AppComponent> {

    private AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }

    @Override
    public AppComponent provideComponent() {
        return appComponent;
    }

}
DaggerMainComponent.builder()
                .appComponent(Napkin.<AppComponent>provideAppComponent(context))
                .build()
                .inject(this);
  • in case you just need component you can use:
AppComponent appComponent = Napkin.<AppComponent>provideAppComponent(context);
DaggerMainComponent.builder()
                .appComponent(Napkin.provideAppComponent(context))
                .build()
                .inject(this);
                
AppComponent appComponent = Napkin.provideAppComponent(context);
  • access component stored in any class implementing ComponentProvider
Component component = Napkin.<Component>provideComponent(object);
  • access component stored in Application class implementing ComponentProvider by any Context
Component component = Napkin.<Component>provideAppComponent(context);
  • access component stored in Application class implementing ComponentProvider by Fragment
Component component = Napkin.<Component>provideAppComponent(fragment);
  • access component stored in Application class implementing ComponentProvider by View
Component component = Napkin.<Component>provideAppComponent(view);
  • access component stored in Activity class implementing ComponentProvider by Fragment
Component component = Napkin.<Component>provideActivityComponent(fragment);
  • access component stored in Activity class implementing ComponentProvider by View
Component component = Napkin.<Component>provideActivityComponent(view);
  • you can use Napkin with sub components:
@AppScope
@Component(modules = AppModule.class)
public interface AppComponent {
   
    ActivityComponent with(ActivityModule activityModule);
 
}
@ActivityScope
@Subcomponent(modules = ActivityModule.class)
public interface ActivityComponent {

    FragmentComponent with(FragmentModule fragmentModule);

    void inject(MyActivity myActivity);
}
@FragmentScope
@Subcomponent(modules = FragmentModule.class)
public interface FragmentComponent {

    void inject(MyFragment myFragment);
}
public class MyApplication extends Application implements ComponentProvider<AppComponent> {
  
    private AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
   
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();      
    }

    @Override
    public AppComponent provideComponent() {
        return appComponent;
    }
}
public class MyActivity extends AppCompatActivity implements ComponentProvider<ActivityComponent> {
  
    private ActivityComponent activityComponent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        activityComponent = Napkin.<AppComponent>provideAppComponent(this)
                .with(new ActivityModule(this));
        activityComponent.inject(this);
    }

    @Override
    public ActivityComponent provideComponent() {
        return activityComponent;
    }
}
public class MyFragment extends Fragment {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Napkin.<ActivityComponent>provideActivityComponent(this)
                .with(new FragmentModule(this))
                .inject(this);
    }   
}

Common qualifiers

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:qualifier-common:0.7.0'
}
  • list of annotations
@RestEndpoint

Scopes

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:scope:0.7.0'
}
  • list of annotations
@AppScope
@ActivityScope
@FragmentScope
@ServiceScope
@ReceiverScope
@ProviderScope
@UserScope
@SessionScope

Qualifiers

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:qualifier:0.7.0'
}
  • list of annotations
@AppContext 
@ActivityContext
@FragmentContext
@ServiceContext
@ReceiverContext
@ProviderContext

Modules

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:module:0.7.0'
}

Napkin modules can be override to add custom methods.

  • list of modules
@Module
@AppScope
public class NapkinAppModule extends AbstractNapkinAppModule {

    public NapkinAppModule(Application application) {
        super(application);
    }

    @Override
    @Provides
    @AppScope
    public Application provideApplication() {
        return application;
    }

    @Override
    @Provides
    @AppScope
    @AppContext
    public Context provideContext() {
        return application;
    }
}
@Module
@ActivityScope
public class NapkinActivityModule extends AbstractNapkinActivityModule {

    public NapkinActivityModule(AppCompatActivity activity) {
        super(activity);
    }

    @Override
    @Provides
    @ActivityScope
    public AppCompatActivity provideActivity() {
        return activity;
    }

    @Override
    @Provides
    @ActivityScope
    @ActivityContext
    public Context provideContext() {
        return activity;
    }
}
@Module
@FragmentScope
public class NapkinFragmentModule extends AbstractNapkinFragmentModule {

    public NapkinFragmentModule(Fragment fragment) {
        super(fragment);
    }

    @Override
    @Provides
    @FragmentScope
    public Fragment provideFragment() {
        return fragment;
    }

}
@Module
@ServiceScope
public class NapkinServiceModule extends AbstractNapkinServiceModule {

    public NapkinServiceModule(Context context) {
        super(context);
    }

    @Override
    @Provides
    @ServiceScope
    @ServiceContext
    public Context provideContext() {
        return context;
    }
}
@Module
@ReceiverScope
public class NapkinReceiverModule extends AbstractNapkinReceiverModule {

    public NapkinReceiverModule(Context context) {
        super(context);
    }

    @Override
    @Provides
    @ReceiverScope
    @ReceiverContext
    public Context provideContext() {
        return context;
    }
}
@Module
@ProviderScope
public class NapkinProviderModule extends AbstractNapkinProviderModule {

    public NapkinProviderModule(Context context) {
        super(context);
    }

    @Override
    @Provides
    @ProviderScope
    @ProviderContext
    public Context provideContext() {
        return context;
    }
}

Scopes with prefix Per

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:scope-per:0.7.0'
}
  • list of annotations
@PerApp
@PerActivity
@PerFragment
@PerService
@PerReceiver
@PerProvider
@PerUser
@PerSession

Scopes with inverted names

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:scope-inverse:0.7.0'
}
  • list of annotations
@ScopeApp
@ScopeActivity
@ScopeFragment
@ScopeService
@ScopeReceiver
@ScopeProvider
@ScopeUser
@ScopeSession

Qualifiers with inverted names

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:qualifier-inverse:0.7.0'
}
  • list of annotations
@ContextApp 
@ContextActivity
@ContextFragment
@ContextService
@ContextReceiver
@ContextProvider

Modules with scopes with prefix Per

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:module-scope-per:0.7.0'
}

Modules with scopes with inverted names

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:module-scope-inverse:0.7.0'
}

Modules with qualifiers with inverted names

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:module-qualifier-inverse:0.7.0'
}

Modules with scopes with prefix Per and qualifiers with inverted names

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:module-scope-per-qualifier-inverse:0.7.0'
}

Modules with scopes with inverted names and qualifiers with inverted names

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.Napkin:module-scope-inverse-qualifier-inverse:0.7.0'
}

Changelog

0.7.0 (2017-01-07)

  • remove Dagger 2 dependency from scopes and qualifiers
  • move common qualifiers to separate sub project
  • update dependencies

0.6.1 (2016-12-01)

  • update dependencies

0.6.0 (2016-11-18)

  • rename modules
  • changes in methods for providing component

0.5.0 (2016-11-11)

  • split into sub projects
  • add modules

0.4.0 (2016-10-31)

  • add new annotations
  • delete method for retrieving component by Class

0.3.0 (2016-10-25)

  • update SDK
  • add new method for retrieve component

0.2.1 (2016-09-14)

  • update SDK

0.2.0 (2016-05-14)

  • add component provider
  • add new annotation

0.1.1 (2016-05-13)

  • add new annotations

License

Copyright 2016 Aleksander Mielczarek

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.