Common scopes, qualifiers and few utilities for Dagger 2. Scopes and qualifiers can be used also with toothpick.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
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);
- with Retrolambda you can use:
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);
}
}
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:qualifier-common:0.7.0'
}
- list of annotations
@RestEndpoint
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:scope:0.7.0'
}
- list of annotations
@AppScope
@ActivityScope
@FragmentScope
@ServiceScope
@ReceiverScope
@ProviderScope
@UserScope
@SessionScope
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:qualifier:0.7.0'
}
- list of annotations
@AppContext
@ActivityContext
@FragmentContext
@ServiceContext
@ReceiverContext
@ProviderContext
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;
}
}
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
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
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:qualifier-inverse:0.7.0'
}
- list of annotations
@ContextApp
@ContextActivity
@ContextFragment
@ContextService
@ContextReceiver
@ContextProvider
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:module-scope-per:0.7.0'
}
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:module-scope-inverse:0.7.0'
}
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:module-qualifier-inverse:0.7.0'
}
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:module-scope-per-qualifier-inverse:0.7.0'
}
Add the dependency
dependencies {
compile 'com.github.AleksanderMielczarek.Napkin:module-scope-inverse-qualifier-inverse:0.7.0'
}
- remove Dagger 2 dependency from scopes and qualifiers
- move common qualifiers to separate sub project
- update dependencies
- update dependencies
- rename modules
- changes in methods for providing component
- split into sub projects
- add modules
- add new annotations
- delete method for retrieving component by Class
- update SDK
- add new method for retrieve component
- update SDK
- add component provider
- add new annotation
- add new annotations
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.