Skip to content

Instantly share code, notes, and snippets.

@rafakob
Last active June 14, 2016 13:03
Show Gist options
  • Save rafakob/015da9ebed247c4305d07741c4a97f01 to your computer and use it in GitHub Desktop.
Save rafakob/015da9ebed247c4305d07741c4a97f01 to your computer and use it in GitHub Desktop.
//////////////////// APP COMPONENT ////////////////////
@PerApp
@Component(modules = {AppModule.class, NetworkModule.class, RepoModule.class})
public interface AppComponent {
ActivityComponent with(ActivityModule activityModule);
}
//////////////////// ACTIVITY COMPONENT ////////////////////
@PerActivity
@Subcomponent(modules = ActivityModule.class)
public interface ActivityComponent {
void inject(LoginActivity loginActivity);
// ...
}
//////////////////// ACTIVITY MODULE ////////////////////
@Module
public class ActivityModule {
protected final Activity mActivity;
public ActivityModule(Activity activity) {
mActivity = activity;
}
@Provides
@PerActivity
Activity providesActivity() {
return mActivity;
}
@Provides
@PerActivity
@ContextActivity
Context providesContext() {
return mActivity;
}
}
//////////////////// APP MODULE ////////////////////
@Module
public class AppModule {
protected final Application mApplication;
public AppModule(Application application) {
this.mApplication = application;
}
@Provides
@PerApp
Application providesApplication() {
return mApplication;
}
@Provides
@ContextApp
Context providesContext() {
return mApplication;
}
}
//////////////////// REPO MODULE ////////////////////
@Module
public class RepoModule {
@Provides
@PerApp
Prefser providesPrefser(@ContextApp Context context) {
return new Prefser(context);
}
@Provides
@PerApp
UserRepo providesUserRepo(Prefser prefser, ApiService apiService) {
return new UserRepoReal(prefser, apiService);
}
}
@Module
public class NetworkModule {
@Provides
@PerApp
OkHttpClient providesOkHttpClient() {
return new OkHttpClient.Builder()
.build();
}
@Provides
@PerApp
Retrofit providesRetrofit(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl("...")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build();
}
@Provides
@PerApp
ApiService providesApiService(Retrofit retrofit) {
return retrofit.create(ApiService.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment