Posts

Showing posts from October, 2019

Dagger 03: Injecting via an AppModule using @Provides and @Inject

1. Create a class AppModule to inject stuff at the Application level. Add the AppModule in the modules section. Do not forget. @Module public class AppModule { } 2. In this class you can inject stuff like Retrofit instance or Glide Instance or a Constant class instance into your Activities by using @Provides . To Inject a String that represents a value, add the following code into the AppModule class    @Provides     static String someString() {         return "This is a test string";     } 3. To use these values inside your Activity using @Inject annotation in front the properties into which you wanna inject these values.      private static final String TAG = "AuthActivity";          @Inject     String injectedString;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_auth);         Log.d(TAG, "onCreate: " + injectedString)

Dagger02: Inject Activities with @ContributesAndroidInjector

Components acts as Services . Activities/Fragments acts as Clients . Dagger Module: They are a place for dependencies to live so that we can add them to the components. 1. Create a ActivityBuildersModule class. This is the class where the DI of the Activity we want would happen. In this case it is AuthActivity . import dagger.Module; import dagger.android.ContributesAndroidInjector; @Module public abstract class ActivityBuildersModule {     @ContributesAndroidInjector     abstract AuthActivity contributeAuthActivity(); } 2. Add this module into the AppComponent class module section. @Component(         modules = {                 AndroidSupportInjectionModule.class,                 ActivityBuildersModule.class         } ) Now we can basically inject stuff into the AuthActivity . 3. Change the Activity to extend from DaggerCompatActivity .

Dagger01: Setting up

1. Install the Dagger Dependencies. def DAGGER_VERSION = "2.22" implementation "com.google.dagger:dagger:$DAGGER_VERSION" annotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION" implementation "com.google.dagger:dagger-android:$DAGGER_VERSION" implementation "com.google.dagger:dagger-android-support:$DAGGER_VERSION" annotationProcessor "com.google.dagger:dagger-android-processor:$DAGGER_VERSION" 2. Create a BaseApplication class that extends DaggerApplication import dagger.android.AndroidInjector; import dagger.android.support.DaggerApplication; public class BaseApplication extends DaggerApplication {     @Override     protected AndroidInjector<? extends DaggerApplication> applicationInjector() {         return null;     } } 3. Add the BaseApplication class to the Manifest file.  <application         android:allowBackup="true"         android:name=".

Reading local files in an iOS Project

Reading String content from a Bundle