Last active
April 23, 2020 17:14
-
-
Save frogermcs/eb984b6fb1c1fc856db3 to your computer and use it in GitHub Desktop.
Sources for blog post "Dependency injection with Dagger 2 - the API"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Scope | |
public @interface ActivityScope { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
@Component( | |
modules = { | |
AppModule.class, | |
GithubApiModule.class | |
} | |
) | |
public interface AppComponent { | |
void inject(GithubClientApplication githubClientApplication); | |
Application getApplication(); | |
AnalyticsManager getAnalyticsManager(); | |
UserManager getUserManager(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public @interface Component { | |
Class<?>[] modules() default {}; | |
Class<?>[] dependencies() default {}; | |
} | |
public @interface Subcomponent { | |
Class<?>[] modules() default {}; | |
} | |
public @interface Module { | |
Class<?>[] includes() default {}; | |
} | |
public @interface Provides { | |
} | |
public @interface MapKey { | |
boolean unwrapValue() default true; | |
} | |
public interface Lazy<T> { | |
T get(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
public class GithubApiModule { | |
@Provides | |
@Singleton | |
OkHttpClient provideOkHttpClient() { | |
OkHttpClient okHttpClient = new OkHttpClient(); | |
okHttpClient.setConnectTimeout(60 * 1000, TimeUnit.MILLISECONDS); | |
okHttpClient.setReadTimeout(60 * 1000, TimeUnit.MILLISECONDS); | |
return okHttpClient; | |
} | |
@Provides | |
@Singleton | |
RestAdapter provideRestAdapter(Application application, OkHttpClient okHttpClient) { | |
RestAdapter.Builder builder = new RestAdapter.Builder(); | |
builder.setClient(new OkClient(okHttpClient)) | |
.setEndpoint(application.getString(R.string.endpoint)); | |
return builder.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
public class GithubApiModule { | |
//... | |
@Provides //This annotation means that method below provides dependency | |
@Singleton | |
RestAdapter provideRestAdapter(Application application, OkHttpClient okHttpClient) { | |
RestAdapter.Builder builder = new RestAdapter.Builder(); | |
builder.setClient(new OkClient(okHttpClient)) | |
.setEndpoint(application.getString(R.string.endpoint)); | |
return builder.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SomeType getSomeType(); | |
Provider<SomeType> getSomeTypeProvider(); | |
Lazy<SomeType> getLazySomeType(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public @interface Inject { | |
} | |
public @interface Scope { | |
} | |
public @interface Qualifier { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginActivity extends BaseActivity { | |
@Inject | |
LoginActivityPresenter presenter; | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginActivityPresenter { | |
private LoginActivity loginActivity; | |
private UserDataStore userDataStore; | |
private UserManager userManager; | |
@Inject | |
public LoginActivityPresenter(LoginActivity loginActivity, | |
UserDataStore userDataStore, | |
UserManager userManager) { | |
this.loginActivity = loginActivity; | |
this.userDataStore = userDataStore; | |
this.userManager = userManager; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginActivityPresenter { | |
private LoginActivity loginActivity; | |
@Inject | |
public LoginActivityPresenter(LoginActivity loginActivity) { | |
this.loginActivity = loginActivity; | |
} | |
@Inject | |
public void enableWatches(Watches watches) { | |
watches.register(this); //Watches instance required fully constructed LoginActivityPresenter | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MapKey(unwrapValue = true) | |
@interface TestKey { | |
String value(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Provides(type = Type.MAP) | |
@TestKey("foo") | |
String provideFooKey() { | |
return "foo value"; | |
} | |
@Provides(type = Type.MAP) | |
@TestKey("bar") | |
String provideBarKey() { | |
return "bar value"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Inject | |
Map<String, String> map; | |
map.toString() // => „{foo=foo value, bar=bar value}” |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SomeType getSomeType(); | |
Set<SomeType> getSomeTypes(); | |
@PortNumber int getPortNumber(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Inject | |
@GithubRestAdapter | |
RestAdapter githubRestAdapter; | |
@Inject | |
@FacebookRestAdapter | |
RestAdapter facebookRestAdapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Provides | |
@Singleton | |
@GithubRestAdapter //Qualifier | |
RestAdapter provideRestAdapter() { | |
return new RestAdapter.Builder() | |
.setEndpoint("https://api.github.com") | |
.build(); | |
} | |
@Provides | |
@Singleton | |
@FacebookRestAdapter //Qualifier | |
RestAdapter provideRestAdapter() { | |
return new RestAdapter.Builder() | |
.setEndpoint("https://api.facebook.com") | |
.build(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SplashActivity extends AppCompatActivity { | |
@Inject | |
LoginActivityPresenter presenter; | |
@Inject | |
AnalyticsManager analyticsManager; | |
@Override | |
protected void onCreate(Bundle bundle) { | |
super.onCreate(bundle); | |
getAppComponent().inject(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SplashActivity extends AppCompatActivity { | |
//... | |
@Override | |
protected void onCreate(Bundle bundle) { | |
super.onCreate(bundle); | |
getAppComponent().inject(this); //Requested depenencies are injected in this moment | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This class is generated automatically by Dagger 2 | |
public final class SplashActivity_MembersInjector implements MembersInjector<SplashActivity> { | |
//... | |
@Override | |
public void injectMembers(SplashActivity splashActivity) { | |
if (splashActivity == null) { | |
throw new NullPointerException("Cannot inject members into a null reference"); | |
} | |
supertypeInjector.injectMembers(splashActivity); | |
splashActivity.presenter = presenterProvider.get(); | |
splashActivity.analyticsManager = analyticsManagerProvider.get(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ActivityScope | |
@Component( | |
modules = SplashActivityModule.class, | |
dependencies = AppComponent.class | |
) | |
public interface SplashActivityComponent { | |
SplashActivity inject(SplashActivity splashActivity); | |
SplashActivityPresenter presenter(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was just reading your article. I believe injection of activities should happen before the call to super in onCreate(..) and after the call to super in onAttach() of fragments. At least that's what's in the official docs: