Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (64 sloc) 2.03 KB
package com.karntrehan.posts.core.di
import android.content.Context
import com.facebook.stetho.okhttp3.StethoInterceptor
import com.google.gson.Gson
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import com.karntrehan.posts.core.BuildConfig
import com.karntrehan.posts.core.constants.Constants
import dagger.Module
import dagger.Provides
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton
@Module
class NetworkModule {
@Provides
@Singleton
fun providesRetrofit(
gsonConverterFactory: GsonConverterFactory,
rxJava2CallAdapterFactory: RxJava2CallAdapterFactory,
okHttpClient: OkHttpClient
): Retrofit {
return Retrofit.Builder().baseUrl(Constants.API_URL)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.client(okHttpClient)
.build()
}
@Provides
@Singleton
fun providesOkHttpClient(cache: Cache): OkHttpClient {
val client = OkHttpClient.Builder()
.cache(cache)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
if (BuildConfig.DEBUG)
client.addNetworkInterceptor(StethoInterceptor())
return client.build()
}
@Provides
@Singleton
fun providesOkhttpCache(context: Context): Cache {
val cacheSize = 10 * 1024 * 1024 // 10 MB
return Cache(context.cacheDir, cacheSize.toLong())
}
@Provides
@Singleton
fun providesGson(): Gson {
return Gson()
}
@Provides
@Singleton
fun providesGsonConverterFactory(): GsonConverterFactory {
return GsonConverterFactory.create()
}
@Provides
@Singleton
fun providesRxJavaCallAdapterFactory(): RxJava2CallAdapterFactory {
return RxJava2CallAdapterFactory.create()
}
}