Advertisement
BlackZerg

Untitled

Feb 10th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.40 KB | None | 0 0
  1. val apiModule = module {
  2.     fun provideGeoCodingApi(retrofit: Retrofit): ApiInterface =
  3.         retrofit.create(ApiInterface::class.java)
  4.  
  5.     single { provideGeoCodingApi(get()) }
  6. }
  7.  
  8. val networkModule = module {
  9.     val logger = run {
  10.         val interceptor = HttpLoggingInterceptor()
  11.         interceptor.apply {
  12.             interceptor.level =
  13.                 if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
  14.         }
  15.     }
  16.  
  17.     val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
  18.         .tlsVersions(TlsVersion.TLS_1_2)
  19.         .cipherSuites(
  20.             CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  21.             CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  22.             CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  23.         )
  24.         .build()
  25.  
  26.     fun httpClient(context: Context): OkHttpClient {
  27.         val cacheSize = (10 * 1024 * 1024).toLong()
  28.         val httpCacheDirectory = File(context.cacheDir, "offlineCache")
  29.         val httpCache = Cache(httpCacheDirectory, cacheSize)
  30.  
  31.         return OkHttpClient.Builder()
  32.             .cache(httpCache)
  33.             .connectionSpecs(Collections.singletonList(connectionSpec))
  34.             .addInterceptor(logger)
  35.             .build()
  36.     }
  37.  
  38.     fun provideRetrofit(httpClient: OkHttpClient, url: String): Retrofit =
  39.         Retrofit.Builder()
  40.             .baseUrl(url)
  41.             .addConverterFactory(GsonConverterFactory.create())
  42.             .client(httpClient)
  43.             .build()
  44.  
  45.     single { httpClient(androidContext()) }
  46.     single { provideRetrofit(get(), GEO_CODING_BASE_URL) }
  47. }
  48.  
  49. val storageModule = module {
  50.     factory<SharedPreferences> { androidContext().getSharedPreferences("local_storage", Context.MODE_PRIVATE) }
  51. }
  52.  
  53. val dataBaseModule = module {
  54.     fun provideDB(application: Application): RoomDB = RoomDB.getDataBase(application)
  55.     fun provideLocationDao(db: RoomDB): LocationDao = db.locationDao()
  56.  
  57.     single { provideDB(androidApplication()) }
  58.     single { provideLocationDao(get()) }
  59. }
  60.  
  61. val repositoryModule = module {
  62.     fun provideDataBaseRepository(locationDao: LocationDao): DataBase =
  63.         DataBaseRepository(locationDao)
  64.  
  65.     fun provideNetworkRepository(api: ApiInterface): Network =
  66.         NetworkRepository(api)
  67.  
  68.     single { provideDataBaseRepository(get()) }
  69.     single { provideNetworkRepository(get()) }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement