Created
March 7, 2016 22:14
-
-
Save frogermcs/7721959e806e0755216b to your computer and use it in GitHub Desktop.
Dagger 2 Producers
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
@ProducerModule | |
public class GithubApiProducerModule { | |
@Produces | |
static OkHttpClient produceOkHttpClient() { | |
final OkHttpClient.Builder builder = new OkHttpClient.Builder(); | |
if (BuildConfig.DEBUG) { | |
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); | |
logging.setLevel(HttpLoggingInterceptor.Level.BODY); | |
builder.addInterceptor(logging); | |
} | |
builder.connectTimeout(60 * 1000, TimeUnit.MILLISECONDS) | |
.readTimeout(60 * 1000, TimeUnit.MILLISECONDS); | |
return builder.build(); | |
} | |
@Produces | |
public Retrofit produceRestAdapter(Application application, OkHttpClient okHttpClient) { | |
Retrofit.Builder builder = new Retrofit.Builder(); | |
builder.client(okHttpClient) | |
.baseUrl(application.getString(R.string.endpoint)) | |
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | |
.addConverterFactory(GsonConverterFactory.create()); | |
return builder.build(); | |
} | |
@Produces | |
public GithubApiService produceGithubApiService(Retrofit restAdapter) { | |
return restAdapter.create(GithubApiService.class); | |
} | |
@Produces | |
public UserManager produceUserManager(GithubApiService githubApiService) { | |
return new UserManager(githubApiService); | |
} | |
@Produces | |
public UserModule.Factory produceUserModuleFactory(GithubApiService githubApiService) { | |
return new UserModule.Factory(githubApiService); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment