package br.com.mgalhardo.guidebook.dagger.module.infraestruture; import android.app.Application; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import javax.inject.Named; import javax.inject.Singleton; import br.com.mgalhardo.guidebook.dagger.module.SettingModule; import dagger.Module; import dagger.Provides; import okhttp3.Cache; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; @Module public class NetworkModule { private static final int CACHE_SIZE_10_MB = 10 * 1024 * 1024; @Provides @Singleton Cache providesOkHttpCache(Application application) { int cacheSize = CACHE_SIZE_10_MB; return new Cache(application.getCacheDir(), cacheSize); } @Provides @Singleton Gson providesGson() { return new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") .create(); } @Provides @Singleton HttpLoggingInterceptor providesHttpLoggingInterceptor() { HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); return httpLoggingInterceptor; } @Provides @Singleton OkHttpClient providesOkHttpClient(Cache cache, HttpLoggingInterceptor httpLoggingInterceptor) { return new OkHttpClient.Builder() .addInterceptor(httpLoggingInterceptor) .cache(cache).build(); } @Provides @Singleton GsonConverterFactory providesGsonConverterFactory(Gson gson) { return GsonConverterFactory.create(gson); } @Provides @Singleton RxJavaCallAdapterFactory providesRxJavaCallAdapterFactory() { return RxJavaCallAdapterFactory.create(); } @Provides @Singleton Retrofit providesRetrofit(GsonConverterFactory gsonConverterFactory, OkHttpClient okHttpClient, RxJavaCallAdapterFactory rxJavaCallAdapterFactory, @Named(SettingModule.BASE_URL) String baseUrl) { return new Retrofit.Builder().addConverterFactory(gsonConverterFactory) .addCallAdapterFactory(rxJavaCallAdapterFactory) .baseUrl(baseUrl) .client(okHttpClient) .build(); } }