From 82ecd53b25c2ef278f57ca02a2c6ae53cc9444df Mon Sep 17 00:00:00 2001 From: Idorenyin Obong Date: Mon, 11 Sep 2017 09:11:02 +0100 Subject: [PATCH] new dagger implementation --- app/build.gradle | 23 +++++++----- .../DemoApplication.java | 29 +++++++-------- .../android_dagger2_example/MainActivity.java | 35 ++++++++----------- .../di/component/ActivityBindingModule.java | 18 ++++++++++ .../di/component/ActivityComponent.java | 19 ---------- .../di/component/ApplicationComponent.java | 32 ++++++++--------- .../di/module/ActivityModule.java | 22 ++++-------- .../di/module/ApplicationModule.java | 26 ++++++-------- 8 files changed, 92 insertions(+), 112 deletions(-) create mode 100644 app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ActivityBindingModule.java delete mode 100644 app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ActivityComponent.java diff --git a/app/build.gradle b/app/build.gradle index e03bbd5..8c40caa 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.1" + compileSdkVersion 26 + buildToolsVersion "26.0.0" defaultConfig { applicationId "com.mindorks.example.android_dagger2_example" minSdkVersion 16 - targetSdkVersion 25 + targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -17,6 +17,11 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + + configurations.all { + resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9' + } + } dependencies { @@ -24,11 +29,13 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.1.0' + compile 'com.android.support:appcompat-v7:26.+' testCompile 'junit:junit:4.12' - compile "com.google.dagger:dagger:2.8" - annotationProcessor "com.google.dagger:dagger-compiler:2.8" - provided 'javax.annotation:jsr250-api:1.0' - compile 'javax.inject:javax.inject:1' + compile 'com.google.dagger:dagger:2.11' + compile 'com.google.dagger:dagger-android-support:2.11' + annotationProcessor 'com.google.dagger:dagger-compiler:2.11' + annotationProcessor 'com.google.dagger:dagger-android-processor:2.11' + //provided 'javax.annotation:jsr250-api:1.0' + //compile 'javax.inject:javax.inject:1' } diff --git a/app/src/main/java/com/mindorks/example/android_dagger2_example/DemoApplication.java b/app/src/main/java/com/mindorks/example/android_dagger2_example/DemoApplication.java index 2271e02..6f3b8e7 100644 --- a/app/src/main/java/com/mindorks/example/android_dagger2_example/DemoApplication.java +++ b/app/src/main/java/com/mindorks/example/android_dagger2_example/DemoApplication.java @@ -1,41 +1,38 @@ package com.mindorks.example.android_dagger2_example; +import android.app.Activity; import android.app.Application; -import android.content.Context; import com.mindorks.example.android_dagger2_example.data.DataManager; -import com.mindorks.example.android_dagger2_example.di.component.ApplicationComponent; import com.mindorks.example.android_dagger2_example.di.component.DaggerApplicationComponent; -import com.mindorks.example.android_dagger2_example.di.module.ApplicationModule; import javax.inject.Inject; +import dagger.android.AndroidInjector; +import dagger.android.DispatchingAndroidInjector; +import dagger.android.HasActivityInjector; + /** * Created by janisharali on 25/12/16. */ -public class DemoApplication extends Application { +public class DemoApplication extends Application implements HasActivityInjector { - protected ApplicationComponent applicationComponent; + @Inject + DispatchingAndroidInjector activityDispatchingAndroidInjector; @Inject DataManager dataManager; - public static DemoApplication get(Context context) { - return (DemoApplication) context.getApplicationContext(); - } - @Override public void onCreate() { super.onCreate(); - applicationComponent = DaggerApplicationComponent - .builder() - .applicationModule(new ApplicationModule(this)) - .build(); - applicationComponent.inject(this); + DaggerApplicationComponent.builder().application(this) + .build().inject(this); } - public ApplicationComponent getComponent(){ - return applicationComponent; + @Override + public AndroidInjector activityInjector() { + return activityDispatchingAndroidInjector; } } diff --git a/app/src/main/java/com/mindorks/example/android_dagger2_example/MainActivity.java b/app/src/main/java/com/mindorks/example/android_dagger2_example/MainActivity.java index e0ad07f..18b3cb1 100644 --- a/app/src/main/java/com/mindorks/example/android_dagger2_example/MainActivity.java +++ b/app/src/main/java/com/mindorks/example/android_dagger2_example/MainActivity.java @@ -1,46 +1,36 @@ package com.mindorks.example.android_dagger2_example; -import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import com.mindorks.example.android_dagger2_example.data.DataManager; import com.mindorks.example.android_dagger2_example.data.model.User; -import com.mindorks.example.android_dagger2_example.di.component.ActivityComponent; -import com.mindorks.example.android_dagger2_example.di.component.DaggerActivityComponent; -import com.mindorks.example.android_dagger2_example.di.module.ActivityModule; import javax.inject.Inject; -public class MainActivity extends AppCompatActivity { +import dagger.android.AndroidInjection; +import dagger.android.AndroidInjector; +import dagger.android.DispatchingAndroidInjector; +import dagger.android.support.HasSupportFragmentInjector; + +public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject DataManager mDataManager; - private ActivityComponent activityComponent; - + @Inject + DispatchingAndroidInjector fragmentDispatchingAndroidInjector; private TextView mTvUserInfo; private TextView mTvAccessToken; - public ActivityComponent getActivityComponent() { - if (activityComponent == null) { - activityComponent = DaggerActivityComponent.builder() - .activityModule(new ActivityModule(this)) - .applicationComponent(DemoApplication.get(this).getComponent()) - .build(); - } - return activityComponent; - } - @Override protected void onCreate(Bundle savedInstanceState) { + AndroidInjection.inject(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - - getActivityComponent().inject(this); - mTvUserInfo = (TextView) findViewById(R.id.tv_user_info); mTvAccessToken = (TextView) findViewById(R.id.tv_access_token); } @@ -70,4 +60,9 @@ private void getUser(){ mTvUserInfo.setText(user.toString()); }catch (Exception e){e.printStackTrace();} } + + @Override + public AndroidInjector supportFragmentInjector() { + return fragmentDispatchingAndroidInjector; + } } diff --git a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ActivityBindingModule.java b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ActivityBindingModule.java new file mode 100644 index 0000000..3bbfa30 --- /dev/null +++ b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ActivityBindingModule.java @@ -0,0 +1,18 @@ +package com.mindorks.example.android_dagger2_example.di.component; + +import com.mindorks.example.android_dagger2_example.MainActivity; +import com.mindorks.example.android_dagger2_example.di.module.ActivityModule; + +import dagger.Module; +import dagger.android.ContributesAndroidInjector; + +/** + * Created by idee on 9/11/17. + */ + +@Module +public abstract class ActivityBindingModule { + @ContributesAndroidInjector(modules = ActivityModule.class) + abstract MainActivity mainActivity(); +} + diff --git a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ActivityComponent.java b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ActivityComponent.java deleted file mode 100644 index a723d89..0000000 --- a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ActivityComponent.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mindorks.example.android_dagger2_example.di.component; - -import com.mindorks.example.android_dagger2_example.MainActivity; -import com.mindorks.example.android_dagger2_example.di.PerActivity; -import com.mindorks.example.android_dagger2_example.di.module.ActivityModule; - -import dagger.Component; - -/** - * Created by janisharali on 08/12/16. - */ - -@PerActivity -@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class) -public interface ActivityComponent { - - void inject(MainActivity mainActivity); - -} diff --git a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ApplicationComponent.java b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ApplicationComponent.java index cbcec1c..c550432 100644 --- a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ApplicationComponent.java +++ b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/component/ApplicationComponent.java @@ -1,18 +1,17 @@ package com.mindorks.example.android_dagger2_example.di.component; import android.app.Application; -import android.content.Context; import com.mindorks.example.android_dagger2_example.DemoApplication; -import com.mindorks.example.android_dagger2_example.data.DataManager; -import com.mindorks.example.android_dagger2_example.data.DbHelper; -import com.mindorks.example.android_dagger2_example.data.SharedPrefsHelper; -import com.mindorks.example.android_dagger2_example.di.ApplicationContext; +import com.mindorks.example.android_dagger2_example.di.module.ActivityModule; import com.mindorks.example.android_dagger2_example.di.module.ApplicationModule; import javax.inject.Singleton; +import dagger.BindsInstance; import dagger.Component; +import dagger.android.AndroidInjector; +import dagger.android.support.AndroidSupportInjectionModule; /** @@ -20,20 +19,17 @@ */ @Singleton -@Component(modules = ApplicationModule.class) -public interface ApplicationComponent { +@Component(modules = {ActivityBindingModule.class, ApplicationModule.class, AndroidSupportInjectionModule.class}) +public interface ApplicationComponent extends AndroidInjector { - void inject(DemoApplication demoApplication); - - @ApplicationContext - Context getContext(); - - Application getApplication(); + @Component.Builder + interface Builder { + @BindsInstance + ApplicationComponent.Builder application(Application application); + ApplicationComponent build(); + } - DataManager getDataManager(); - - SharedPrefsHelper getPreferenceHelper(); - - DbHelper getDbHelper(); + @Override + void inject(DemoApplication demoApplication); } diff --git a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/module/ActivityModule.java b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/module/ActivityModule.java index f5aec7d..2b32e5d 100644 --- a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/module/ActivityModule.java +++ b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/module/ActivityModule.java @@ -1,10 +1,14 @@ package com.mindorks.example.android_dagger2_example.di.module; import android.app.Activity; +import android.app.Application; import android.content.Context; +import android.content.SharedPreferences; +import com.mindorks.example.android_dagger2_example.MainActivity; import com.mindorks.example.android_dagger2_example.di.ActivityContext; +import dagger.Binds; import dagger.Module; import dagger.Provides; @@ -13,22 +17,10 @@ */ @Module -public class ActivityModule { +public abstract class ActivityModule { - private Activity mActivity; - - public ActivityModule(Activity activity) { - mActivity = activity; - } - - @Provides + @Binds @ActivityContext - Context provideContext() { - return mActivity; - } + abstract Activity provideActivity(MainActivity activity); - @Provides - Activity provideActivity() { - return mActivity; - } } diff --git a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/module/ApplicationModule.java b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/module/ApplicationModule.java index fb95ada..9cd710f 100644 --- a/app/src/main/java/com/mindorks/example/android_dagger2_example/di/module/ApplicationModule.java +++ b/app/src/main/java/com/mindorks/example/android_dagger2_example/di/module/ApplicationModule.java @@ -3,11 +3,12 @@ import android.app.Application; import android.content.Context; import android.content.SharedPreferences; -import android.database.sqlite.SQLiteDatabase; import com.mindorks.example.android_dagger2_example.di.ApplicationContext; import com.mindorks.example.android_dagger2_example.di.DatabaseInfo; +import javax.inject.Singleton; + import dagger.Module; import dagger.Provides; @@ -18,23 +19,19 @@ @Module public class ApplicationModule { - private final Application mApplication; - - public ApplicationModule(Application app) { - mApplication = app; - } - - @Provides @ApplicationContext - Context provideContext() { - return mApplication; + @Singleton + @Provides + Context provideContext(Application application) { + return application.getApplicationContext(); } @Provides - Application provideApplication() { - return mApplication; + static SharedPreferences provideSharedPrefs(Application application) { + return application.getSharedPreferences("demo-prefs", Context.MODE_PRIVATE); } + @Provides @DatabaseInfo String provideDatabaseName() { @@ -47,8 +44,5 @@ Integer provideDatabaseVersion() { return 2; } - @Provides - SharedPreferences provideSharedPrefs() { - return mApplication.getSharedPreferences("demo-prefs", Context.MODE_PRIVATE); - } + }