-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkProvider.kt
53 lines (46 loc) · 1.52 KB
/
NetworkProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package <YOUR_PACKAGE_NAME>
import <YOUR_PACKAGE_NAME>.BuildConfig
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
@Module
@InstallIn(ViewModelComponent::class)
class NetworkProvider {
@Provides
fun provideNetworkDispatcher(): CoroutineDispatcher = Dispatchers.IO
@Provides
fun provideMoshi(): Moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
@Provides
fun provideHttpClient(): OkHttpClient {
return OkHttpClient()
.newBuilder()
.addInterceptor(getLogger())
.build()
}
private fun getLogger(): HttpLoggingInterceptor {
return HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) {
HttpLoggingInterceptor.Level.BODY
} else {
HttpLoggingInterceptor.Level.NONE
}
}
}
@Provides
fun provideRetrofit(client: OkHttpClient, moshi: Moshi): Retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.SERVER_URL)
.client(client)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
}