refactor(di): новый ViewModel DSL Koin и инжект prefs в ProxyService
Старый org.koin.androidx.viewmodel.dsl deprecated — Koin 4.x перенёс ViewModel DSL в org.koin.core.module.dsl. Все три VM сведены к viewModelOf: после androidContext(this@App) Application забинден и как Context, явный androidContext() в лямбдах был избыточен. ProxyService получает AppPreferences через by inject() вместо ручного создания мимо графа.
This commit is contained in:
1 parent
77eebbe64d
commit
137ff49713
2 files changed
+5
-8
No files matched your search
@@ -40,6 +40,7 @@ import kotlinx.coroutines.flow.first
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import org.koin.android.ext.android.inject
|
||||||
|
|
||||||
sealed class StartupResult {
|
sealed class StartupResult {
|
||||||
data object Success : StartupResult()
|
data object Success : StartupResult()
|
||||||
@@ -100,7 +101,7 @@ class ProxyService : Service() {
|
|||||||
private val restartCount = AtomicInteger(0)
|
private val restartCount = AtomicInteger(0)
|
||||||
@Volatile private var captchaNotificationActive = false
|
@Volatile private var captchaNotificationActive = false
|
||||||
|
|
||||||
private lateinit var prefs: AppPreferences
|
private val prefs: AppPreferences by inject()
|
||||||
private lateinit var serviceScope: CoroutineScope
|
private lateinit var serviceScope: CoroutineScope
|
||||||
private lateinit var wireGuard: WireGuardTunnelManager
|
private lateinit var wireGuard: WireGuardTunnelManager
|
||||||
|
|
||||||
@@ -108,7 +109,6 @@ class ProxyService : Service() {
|
|||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
prefs = AppPreferences(applicationContext)
|
|
||||||
serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||||
wireGuard = WireGuardTunnelManager(applicationContext)
|
wireGuard = WireGuardTunnelManager(applicationContext)
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ import com.freeturn.app.viewmodel.ProxyViewModel
|
|||||||
import com.freeturn.app.viewmodel.ServerViewModel
|
import com.freeturn.app.viewmodel.ServerViewModel
|
||||||
import com.freeturn.app.viewmodel.SettingsViewModel
|
import com.freeturn.app.viewmodel.SettingsViewModel
|
||||||
import org.koin.android.ext.koin.androidContext
|
import org.koin.android.ext.koin.androidContext
|
||||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
import org.koin.core.module.dsl.viewModelOf
|
||||||
import org.koin.androidx.viewmodel.dsl.viewModelOf
|
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
|
|
||||||
val appModule = module {
|
val appModule = module {
|
||||||
@@ -21,8 +20,6 @@ val appModule = module {
|
|||||||
single { ProxyOrchestrator(get(), get(), get()) }
|
single { ProxyOrchestrator(get(), get(), get()) }
|
||||||
|
|
||||||
viewModelOf(::ProxyViewModel)
|
viewModelOf(::ProxyViewModel)
|
||||||
// Context резолвим явно через androidContext() (Application), а не неявно из
|
viewModelOf(::ServerViewModel)
|
||||||
// графа — VM сами сводят его к applicationContext.
|
viewModelOf(::SettingsViewModel)
|
||||||
viewModel { ServerViewModel(get(), get(), get(), androidContext()) }
|
|
||||||
viewModel { SettingsViewModel(get(), get(), get(), get(), get(), androidContext()) }
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user