-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKoin.kt
91 lines (81 loc) · 2.58 KB
/
Koin.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package di
import data.mapper.PhotoCreatorMapper
import data.mapper.PhotoMapper
import data.mapper.PhotosUrlsMapper
import data.repository.ImageRepository
import data.repository.ImageRepositoryImpl
import data.repository.SharedRepository
import data.repository.SharedRepositoryImpl
import de.jensklingenberg.ktorfit.Ktorfit
import io.ktor.client.HttpClient
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.DEFAULT
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import networking.ApiInterface
import networking.AuthorizationTokenInterceptor
import networking.createApiInterface
import org.koin.compose.viewmodel.dsl.viewModelOf
import org.koin.core.context.startKoin
import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.module
import ui.viewmodel.HomeScreenViewModel
import ui.viewmodel.PhotoDetailViewModel
fun initKoin(appDeclaration: KoinAppDeclaration = {}) =
startKoin {
appDeclaration()
modules(
networkModule(),
mapperModule(),
repositoryModule(),
viewModelModule(),
platformModule()
)
}
// called by iOS etc
fun initKoin() = initKoin() {}
private fun networkModule() = module {
single {
HttpClient {
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
prettyPrint = true
isLenient = true
}
)
}
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.ALL
}
install(AuthorizationTokenInterceptor) {
tokenProvider = { "" }
}
}
}
single<Ktorfit> {
Ktorfit.Builder().build {
httpClient(get<HttpClient>())
baseUrl("https://api.unsplash.com/")
}
}
single<ApiInterface> { get<Ktorfit>().createApiInterface() }
}
private fun mapperModule() = module {
single { PhotoCreatorMapper() }
single { PhotosUrlsMapper() }
single { PhotoMapper(get(), get()) }
}
private fun repositoryModule() = module {
single<ImageRepository> { ImageRepositoryImpl(get()) }
single<SharedRepository> { SharedRepositoryImpl() }
}
private fun viewModelModule() = module {
viewModelOf(::HomeScreenViewModel)
viewModelOf(::PhotoDetailViewModel)
}