Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val apiModule = module {
- fun provideGeoCodingApi(retrofit: Retrofit): ApiInterface =
- retrofit.create(ApiInterface::class.java)
- single { provideGeoCodingApi(get()) }
- }
- val networkModule = module {
- val logger = run {
- val interceptor = HttpLoggingInterceptor()
- interceptor.apply {
- interceptor.level =
- if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
- }
- }
- val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
- .tlsVersions(TlsVersion.TLS_1_2)
- .cipherSuites(
- CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
- CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
- CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
- )
- .build()
- fun httpClient(context: Context): OkHttpClient {
- val cacheSize = (10 * 1024 * 1024).toLong()
- val httpCacheDirectory = File(context.cacheDir, "offlineCache")
- val httpCache = Cache(httpCacheDirectory, cacheSize)
- return OkHttpClient.Builder()
- .cache(httpCache)
- .connectionSpecs(Collections.singletonList(connectionSpec))
- .addInterceptor(logger)
- .build()
- }
- fun provideRetrofit(httpClient: OkHttpClient, url: String): Retrofit =
- Retrofit.Builder()
- .baseUrl(url)
- .addConverterFactory(GsonConverterFactory.create())
- .client(httpClient)
- .build()
- single { httpClient(androidContext()) }
- single { provideRetrofit(get(), GEO_CODING_BASE_URL) }
- }
- val storageModule = module {
- factory<SharedPreferences> { androidContext().getSharedPreferences("local_storage", Context.MODE_PRIVATE) }
- }
- val dataBaseModule = module {
- fun provideDB(application: Application): RoomDB = RoomDB.getDataBase(application)
- fun provideLocationDao(db: RoomDB): LocationDao = db.locationDao()
- single { provideDB(androidApplication()) }
- single { provideLocationDao(get()) }
- }
- val repositoryModule = module {
- fun provideDataBaseRepository(locationDao: LocationDao): DataBase =
- DataBaseRepository(locationDao)
- fun provideNetworkRepository(api: ApiInterface): Network =
- NetworkRepository(api)
- single { provideDataBaseRepository(get()) }
- single { provideNetworkRepository(get()) }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement