|
| 1 | +package com.addhen.github.di |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.addhen.github.App |
| 5 | +import com.addhen.github.AppRxSchedulers |
| 6 | +import com.addhen.github.BuildConfig |
| 7 | +import com.addhen.github.RxSchedulers |
| 8 | +import com.jakewharton.retrofit2.adapter.kotlin.coroutines.experimental.CoroutineCallAdapterFactory |
| 9 | +import com.squareup.moshi.Moshi |
| 10 | +import com.squareup.moshi.Rfc3339DateJsonAdapter |
| 11 | +import dagger.Module |
| 12 | +import dagger.Provides |
| 13 | +import okhttp3.Cache |
| 14 | +import okhttp3.OkHttpClient |
| 15 | +import okhttp3.logging.HttpLoggingInterceptor |
| 16 | +import retrofit2.Retrofit |
| 17 | +import retrofit2.converter.moshi.MoshiConverterFactory |
| 18 | +import timber.log.Timber |
| 19 | +import java.io.File |
| 20 | +import java.util.Date |
| 21 | +import java.util.concurrent.TimeUnit |
| 22 | +import javax.inject.Singleton |
| 23 | + |
| 24 | +@Module(includes = [ |
| 25 | + ViewModelBuilder::class] |
| 26 | +) |
| 27 | +object AppModule { |
| 28 | + |
| 29 | + private val DISK_CACHE_SIZE: Long = 50 * 1024 * 1024 // 50MB |
| 30 | + |
| 31 | + private val HTTP_TIMEOUT = 15L |
| 32 | + |
| 33 | + @Singleton |
| 34 | + @Provides |
| 35 | + @JvmStatic |
| 36 | + fun provideAppContext(app: App) = app.applicationContext |
| 37 | + |
| 38 | + @Singleton |
| 39 | + @Provides |
| 40 | + @JvmStatic |
| 41 | + fun provideLoggingInterceptor(): HttpLoggingInterceptor { |
| 42 | + val loggingInterceptor = HttpLoggingInterceptor { message -> |
| 43 | + Timber.tag("OkHttp").d(message) |
| 44 | + } |
| 45 | + loggingInterceptor.level = if (BuildConfig.DEBUG) { |
| 46 | + HttpLoggingInterceptor.Level.BODY |
| 47 | + } else { |
| 48 | + HttpLoggingInterceptor.Level.BASIC |
| 49 | + } |
| 50 | + return loggingInterceptor |
| 51 | + } |
| 52 | + |
| 53 | + @Singleton |
| 54 | + @Provides |
| 55 | + @JvmStatic |
| 56 | + fun provideOkHttpClient( |
| 57 | + context: Context, |
| 58 | + loggingInterceptor: HttpLoggingInterceptor |
| 59 | + ): OkHttpClient { |
| 60 | + val okHttpClientBuilder = OkHttpClient.Builder() |
| 61 | + val cacheDir = File(context.applicationContext.cacheDir, "app-http-cache") |
| 62 | + val cache = Cache(cacheDir, DISK_CACHE_SIZE) |
| 63 | + okHttpClientBuilder.cache(cache) |
| 64 | + initOkHttpBuilder(okHttpClientBuilder, loggingInterceptor) |
| 65 | + return okHttpClientBuilder.build() |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + @Singleton |
| 70 | + @Provides |
| 71 | + @JvmStatic |
| 72 | + fun provideMoshi(): Moshi { |
| 73 | + return Moshi.Builder() |
| 74 | + .add(Date::class.java, Rfc3339DateJsonAdapter()) |
| 75 | + .build() |
| 76 | + } |
| 77 | + |
| 78 | + @Singleton |
| 79 | + @Provides |
| 80 | + @JvmStatic |
| 81 | + fun provideRetrofit(okHttpClient: OkHttpClient, moshi: Moshi): Retrofit { |
| 82 | + return Retrofit.Builder() |
| 83 | + .baseUrl(BuildConfig.BASE_URL) |
| 84 | + .addConverterFactory(MoshiConverterFactory.create(moshi)) |
| 85 | + .addCallAdapterFactory(CoroutineCallAdapterFactory()) |
| 86 | + .client(okHttpClient) |
| 87 | + .build() |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + @Singleton |
| 92 | + @Provides |
| 93 | + @JvmStatic |
| 94 | + fun provideRxScheduler(): RxSchedulers { |
| 95 | + return AppRxSchedulers() |
| 96 | + } |
| 97 | + |
| 98 | + private fun initOkHttpBuilder( |
| 99 | + okHttpClientBuilder: OkHttpClient.Builder, |
| 100 | + loggingInterceptor: HttpLoggingInterceptor |
| 101 | + ) { |
| 102 | + okHttpClientBuilder.connectTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS) |
| 103 | + okHttpClientBuilder.writeTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS) |
| 104 | + okHttpClientBuilder.readTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS) |
| 105 | + okHttpClientBuilder.addInterceptor(loggingInterceptor) |
| 106 | + } |
| 107 | +} |
0 commit comments