refactor(DI): clean up module structure
This commit is contained in:
parent
72abc53ed3
commit
e96b305c6e
8 changed files with 103 additions and 103 deletions
|
|
@ -2,8 +2,15 @@ package com.aliucord.manager
|
|||
|
||||
import android.app.Application
|
||||
import com.aliucord.manager.di.*
|
||||
import com.aliucord.manager.domain.repository.AliucordMavenRepository
|
||||
import com.aliucord.manager.domain.repository.GithubRepository
|
||||
import com.aliucord.manager.network.service.*
|
||||
import com.aliucord.manager.ui.viewmodel.*
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.androidx.viewmodel.dsl.viewModelOf
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
class ManagerApplication : Application() {
|
||||
override fun onCreate() {
|
||||
|
|
@ -11,7 +18,41 @@ class ManagerApplication : Application() {
|
|||
|
||||
startKoin {
|
||||
androidContext(this@ManagerApplication)
|
||||
modules(httpModule, serviceModule, repositoryModule, viewModelModule, managerModule)
|
||||
|
||||
// HTTP
|
||||
module {
|
||||
singleOf(::provideJson)
|
||||
singleOf(::provideHttpClient)
|
||||
}
|
||||
|
||||
// Services
|
||||
module {
|
||||
singleOf(::GithubService)
|
||||
singleOf(::MavenService)
|
||||
singleOf(::AliucordGithubService)
|
||||
}
|
||||
|
||||
// Repositories
|
||||
module {
|
||||
singleOf(::GithubRepository)
|
||||
singleOf(::AliucordMavenRepository)
|
||||
}
|
||||
|
||||
// ViewModels
|
||||
module {
|
||||
viewModelOf(::HomeViewModel)
|
||||
viewModelOf(::PluginsViewModel)
|
||||
viewModelOf(::AboutViewModel)
|
||||
viewModelOf(::InstallViewModel)
|
||||
viewModelOf(::SettingsViewModel)
|
||||
viewModelOf(::UpdaterViewModel)
|
||||
}
|
||||
|
||||
// Managers
|
||||
module {
|
||||
singleOf(::providePreferences)
|
||||
singleOf(::provideDownloadManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
42
app/src/main/kotlin/com/aliucord/manager/di/Http.kt
Normal file
42
app/src/main/kotlin/com/aliucord/manager/di/Http.kt
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package com.aliucord.manager.di
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.Dns
|
||||
import org.koin.core.scope.Scope
|
||||
import java.net.Inet4Address
|
||||
import java.net.InetAddress
|
||||
|
||||
context(Scope)
|
||||
fun provideJson() = Json {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
|
||||
context(Scope)
|
||||
fun provideHttpClient() = HttpClient(OkHttp) {
|
||||
val json: Json = get()
|
||||
|
||||
engine {
|
||||
config {
|
||||
dns(object : Dns {
|
||||
override fun lookup(hostname: String): List<InetAddress> {
|
||||
val addresses = Dns.SYSTEM.lookup(hostname)
|
||||
|
||||
// Github's nameservers do not respond to IPv6 requests for raw.githubusercontent.com,
|
||||
// which causes CIO, Android and OkHTTP to all hang
|
||||
return if (hostname == "raw.githubusercontent.com") {
|
||||
addresses.filterIsInstance<Inet4Address>()
|
||||
} else {
|
||||
addresses
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
install(ContentNegotiation) {
|
||||
json(json)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
package com.aliucord.manager.di
|
||||
|
||||
import com.aliucord.manager.network.service.HttpService
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.okhttp.*
|
||||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.Dns
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.module
|
||||
import java.net.Inet4Address
|
||||
import java.net.InetAddress
|
||||
|
||||
val httpModule = module {
|
||||
fun provideJson() = Json {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
|
||||
fun provideHttpClient(json: Json) = HttpClient(OkHttp) {
|
||||
engine {
|
||||
config {
|
||||
dns(object : Dns {
|
||||
override fun lookup(hostname: String): List<InetAddress> {
|
||||
val addresses = Dns.SYSTEM.lookup(hostname)
|
||||
|
||||
// Github's nameservers do not respond to IPv6 requests for raw.githubusercontent.com,
|
||||
// which causes CIO, Android and OkHTTP to all hang
|
||||
return if (hostname == "raw.githubusercontent.com") {
|
||||
addresses.filterIsInstance<Inet4Address>()
|
||||
} else {
|
||||
addresses
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
install(ContentNegotiation) {
|
||||
json(json)
|
||||
}
|
||||
}
|
||||
|
||||
singleOf(::provideJson)
|
||||
singleOf(::provideHttpClient)
|
||||
singleOf(::HttpService)
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
package com.aliucord.manager.di
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import com.aliucord.manager.domain.manager.DownloadManager
|
||||
import com.aliucord.manager.domain.manager.PreferencesManager
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val managerModule = module {
|
||||
fun providePreferences(context: Context): PreferencesManager {
|
||||
return PreferencesManager(context.getSharedPreferences("preferences", Context.MODE_PRIVATE))
|
||||
}
|
||||
|
||||
fun provideDownloadManager(application: Application) = DownloadManager(application)
|
||||
|
||||
singleOf(::providePreferences)
|
||||
singleOf(::provideDownloadManager)
|
||||
}
|
||||
19
app/src/main/kotlin/com/aliucord/manager/di/Managers.kt
Normal file
19
app/src/main/kotlin/com/aliucord/manager/di/Managers.kt
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package com.aliucord.manager.di
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import com.aliucord.manager.domain.manager.DownloadManager
|
||||
import com.aliucord.manager.domain.manager.PreferencesManager
|
||||
import org.koin.core.scope.Scope
|
||||
|
||||
context(Scope)
|
||||
fun providePreferences(): PreferencesManager {
|
||||
val ctx: Context = get()
|
||||
return PreferencesManager(ctx.getSharedPreferences("preferences", Context.MODE_PRIVATE))
|
||||
}
|
||||
|
||||
context(Scope)
|
||||
fun provideDownloadManager(): DownloadManager {
|
||||
val application: Application = get()
|
||||
return DownloadManager(application)
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
package com.aliucord.manager.di
|
||||
|
||||
import com.aliucord.manager.domain.repository.AliucordMavenRepository
|
||||
import com.aliucord.manager.domain.repository.GithubRepository
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val repositoryModule = module {
|
||||
singleOf(::GithubRepository)
|
||||
singleOf(::AliucordMavenRepository)
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
package com.aliucord.manager.di
|
||||
|
||||
import com.aliucord.manager.network.service.*
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val serviceModule = module {
|
||||
singleOf(::GithubService)
|
||||
singleOf(::MavenService)
|
||||
singleOf(::AliucordGithubService)
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package com.aliucord.manager.di
|
||||
|
||||
import com.aliucord.manager.ui.viewmodel.*
|
||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||
import org.koin.androidx.viewmodel.dsl.viewModelOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val viewModelModule = module {
|
||||
viewModelOf(::HomeViewModel)
|
||||
viewModelOf(::PluginsViewModel)
|
||||
viewModelOf(::AboutViewModel)
|
||||
viewModelOf(::InstallViewModel)
|
||||
viewModelOf(::SettingsViewModel)
|
||||
viewModelOf(::UpdaterViewModel)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue