-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppConfiguration.kt
More file actions
64 lines (52 loc) · 2.16 KB
/
Copy pathAppConfiguration.kt
File metadata and controls
64 lines (52 loc) · 2.16 KB
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
54
55
56
57
58
59
60
61
62
63
64
package band.effective.compose.drawer
import android.app.Application
import band.effective.compose.drawer.network.GamesApi
import band.effective.compose.drawer.network.HttpConfiguration
import band.effective.compose.drawer.network.HttpConfiguration.API_URL
import band.effective.compose.drawer_modules.okhttp.HttpLogger
import band.effective.compose.drawer_modules.retrofit.DebugRetrofitConfig
import band.effective.compose.drawer_modules.retrofit.Endpoint
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.mock.MockRetrofit
import retrofit2.mock.NetworkBehavior
/**
* Debug configuration for the sample app.
*/
object AppConfiguration {
private lateinit var app: Application
private val endpoints = listOf(
Endpoint("Stage", API_URL, isMock = false),
Endpoint("Production", API_URL, isMock = false),
Endpoint("Mock", "http://localhost/mock/", isMock = true),
)
private val networkBehavior = NetworkBehavior.create()
val httpLogger by lazy { HttpLogger(app) }
val debugRetrofitConfig by lazy { DebugRetrofitConfig(app, endpoints, networkBehavior) }
val api: GamesApi by lazy { createApi() }
fun init(app: Application) {
AppConfiguration.app = app
}
private fun createApi(): GamesApi {
val currentEndpoint: Endpoint = debugRetrofitConfig.currentEndpoint
val httpClient = HttpConfiguration.client.newBuilder()
.addInterceptor(httpLogger.interceptor)
.build()
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val retrofit = Retrofit.Builder()
.baseUrl(currentEndpoint.url)
.client(httpClient)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
if (currentEndpoint.isMock) {
val mockRetrofit =
MockRetrofit.Builder(retrofit).networkBehavior(networkBehavior).build()
return MockGamesApi(mockRetrofit)
}
return retrofit.create(GamesApi::class.java)
}
}