Last active
January 23, 2020 00:34
-
-
Save iChintanSoni/e0b0aad5442fb4fd50b88b9701cbff43 to your computer and use it in GitHub Desktop.
Daggering kotlin-android: AppModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ApiService { | |
@GET("api") | |
fun getUsers(@Query("result") result: Int): Observable<RandomUserResponse> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
class AppModule { | |
@Provides | |
@Singleton | |
fun file(application: Application): File { | |
val file = File(application.cacheDir, "OkHttpCache") | |
file.mkdirs() | |
return file | |
} | |
@Provides | |
@Singleton | |
fun cache(file: File): Cache { | |
return Cache(file, 10 * 1000 * 1000) // 10 mb cache file | |
} | |
@Singleton | |
@Provides | |
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor { | |
val logging = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message -> Timber.tag("OkHttp").d(message) }) | |
logging.level = HttpLoggingInterceptor.Level.BODY | |
return logging | |
} | |
@Provides | |
@Singleton | |
fun okHttpClient(cache: Cache, httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient { | |
return OkHttpClient.Builder() | |
.cache(cache) | |
.addInterceptor(httpLoggingInterceptor) | |
.build() | |
} | |
@Singleton | |
@Provides | |
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit { | |
return Retrofit.Builder() | |
.baseUrl(BuildConfig.BASE_URL) | |
.client(okHttpClient) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
} | |
@Singleton | |
@Provides | |
fun provideApiService(retrofit: Retrofit): ApiService { | |
return retrofit.create(ApiService::class.java) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment