Last active
March 29, 2019 04:11
-
-
Save frogermcs/f1d0c916d91d1a341e4a to your computer and use it in GitHub Desktop.
Sources for blog post "Dependency injection with Dagger 2 - Scopes"
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 { | |
UserComponent plus(UserModule userModule); | |
SplashActivityComponent plus(SplashActivityModule splashActivityModule); | |
} |
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 GithubClientApplication extends Application { | |
private AppComponent appComponent; | |
private UserComponent userComponent; | |
//... | |
public UserComponent createUserComponent(User user) { | |
userComponent = appComponent.plus(new UserModule(user)); | |
return userComponent; | |
} | |
public void releaseUserComponent() { | |
userComponent = null; | |
} | |
//... | |
} |
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 final class ScopedProvider<T> implements Provider<T> { | |
//... | |
private ScopedProvider(Factory<T> factory) { | |
assert factory != null; | |
this.factory = factory; | |
} | |
@SuppressWarnings("unchecked") // cast only happens when result comes from the factory | |
@Override | |
public T get() { | |
// double-check idiom from EJ2: Item 71 | |
Object result = instance; | |
if (result == UNINITIALIZED) { | |
synchronized (this) { | |
result = instance; | |
if (result == UNINITIALIZED) { | |
instance = result = factory.get(); | |
} | |
} | |
} | |
return (T) result; | |
} | |
//... | |
} |
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
@UserScope | |
@Subcomponent( | |
modules = { | |
UserModule.class | |
} | |
) | |
public interface UserComponent { | |
RepositoriesListActivityComponent plus(RepositoriesListActivityModule repositoriesListActivityModule); | |
RepositoryDetailsActivityComponent plus(RepositoryDetailsActivityModule repositoryDetailsActivityModule); | |
} |
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
private final class UserComponentImpl implements UserComponent { | |
//... | |
private UserComponentImpl(UserModule userModule) { | |
if (userModule == null) { | |
throw new NullPointerException(); | |
} | |
this.userModule = userModule; | |
initialize(); | |
} | |
private void initialize() { | |
this.provideUserProvider = ScopedProvider.create(UserModule_ProvideUserFactory.create(userModule)); | |
this.provideRepositoriesManagerProvider = ScopedProvider.create(UserModule_ProvideRepositoriesManagerFactory.create(userModule, provideUserProvider, DaggerAppComponent.this.provideGithubApiServiceProvider)); | |
} | |
//... | |
} |
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
@Generated("dagger.internal.codegen.ComponentProcessor") | |
public final class UserModule_ProvideRepositoriesManagerFactory implements Factory<RepositoriesManager> { | |
//... | |
@Override | |
public RepositoriesManager get() { | |
RepositoriesManager provided = module.provideRepositoriesManager(userProvider.get(), githubApiServiceProvider.get()); | |
if (provided == null) { | |
throw new NullPointerException("Cannot return null from a non-@Nullable @Provides method"); | |
} | |
return provided; | |
} | |
public static Factory<RepositoriesManager> create(UserModule module, Provider<User> userProvider, Provider<GithubApiService> githubApiServiceProvider) { | |
return new UserModule_ProvideRepositoriesManagerFactory(module, userProvider, githubApiServiceProvider); | |
} | |
} |
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 | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface UserScope { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment