Migrate to AndroidX ViewModel from Voyager ScreenModel (#3594)

This commit is contained in:
AntsyLich
2026-07-15 21:19:11 +06:00
committed by GitHub
parent ef1c529672
commit c3b99aea07
91 changed files with 1503 additions and 1091 deletions
@@ -36,9 +36,8 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastForEachIndexed
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import eu.kanade.domain.source.service.SourcePreferences
@@ -50,6 +49,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.browse.migration.search.MigrateSearchScreen
import eu.kanade.tachiyomi.util.system.LocaleHelper
import kotlinx.coroutines.flow.update
import mihon.core.viewmodel.StateViewModel
import mihon.feature.migration.list.MigrationListScreen
import sh.calvin.reorderable.ReorderableCollectionItemScope
import sh.calvin.reorderable.ReorderableItem
@@ -77,8 +77,8 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val screenModel = rememberScreenModel { ScreenModel() }
val state by screenModel.state.collectAsState()
val viewModel = viewModel<ViewModel>()
val state by viewModel.state.collectAsState()
var migrationSheetOpen by rememberSaveable { mutableStateOf(false) }
@@ -121,20 +121,20 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
AppBar.Action(
title = stringResource(MR.strings.migrationConfigScreen_selectAllLabel),
icon = Icons.Outlined.SelectAll,
onClick = { screenModel.toggleSelection(ScreenModel.SelectionConfig.All) },
onClick = { viewModel.toggleSelection(ViewModel.SelectionConfig.All) },
),
AppBar.Action(
title = stringResource(MR.strings.migrationConfigScreen_selectNoneLabel),
icon = Icons.Outlined.Deselect,
onClick = { screenModel.toggleSelection(ScreenModel.SelectionConfig.None) },
onClick = { viewModel.toggleSelection(ViewModel.SelectionConfig.None) },
),
AppBar.OverflowAction(
title = stringResource(MR.strings.migrationConfigScreen_selectEnabledLabel),
onClick = { screenModel.toggleSelection(ScreenModel.SelectionConfig.Enabled) },
onClick = { viewModel.toggleSelection(ViewModel.SelectionConfig.Enabled) },
),
AppBar.OverflowAction(
title = stringResource(MR.strings.migrationConfigScreen_selectPinnedLabel),
onClick = { screenModel.toggleSelection(ScreenModel.SelectionConfig.Pinned) },
onClick = { viewModel.toggleSelection(ViewModel.SelectionConfig.Pinned) },
),
),
)
@@ -146,7 +146,7 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
text = { Text(text = stringResource(MR.strings.migrationConfigScreen_continueButtonText)) },
icon = { Icon(imageVector = Icons.AutoMirrored.Outlined.ArrowForward, contentDescription = null) },
onClick = {
screenModel.saveSources()
viewModel.saveSources()
continueMigration(openSheet = true, extraSearchQuery = null)
},
expanded = lazyListState.shouldExpandFAB(),
@@ -157,7 +157,7 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
val fromIndex = selectedSources.indexOfFirst { it.id == from.key }
val toIndex = selectedSources.indexOfFirst { it.id == to.key }
if (fromIndex == -1 || toIndex == -1) return@rememberReorderableLazyListState
screenModel.orderSource(fromIndex, toIndex)
viewModel.orderSource(fromIndex, toIndex)
}
FastScrollLazyColumn(
@@ -197,7 +197,7 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
dragEnabled = selectedSourceList && sources.size > 1,
state = reorderableState,
key = { if (selectedSourceList) it.id else "available-${it.id}" },
onClick = { screenModel.toggleSelection(item.id) },
onClick = { viewModel.toggleSelection(item.id) },
)
}
}
@@ -206,7 +206,7 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
if (migrationSheetOpen) {
MigrationConfigScreenSheet(
preferences = screenModel.sourcePreferences,
preferences = viewModel.sourcePreferences,
onDismissRequest = { migrationSheetOpen = false },
onStartMigration = { extraSearchQuery ->
migrationSheetOpen = false
@@ -306,10 +306,10 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
}
}
private class ScreenModel(
private class ViewModel(
val sourcePreferences: SourcePreferences = Injekt.get(),
private val sourceManager: SourceManager = Injekt.get(),
) : StateScreenModel<ScreenModel.State>(State()) {
) : StateViewModel<ViewModel.State>(State()) {
private val sourcesComparator = { includedSources: List<Long> ->
compareBy<MigrationSource>(
@@ -320,7 +320,7 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
}
init {
screenModelScope.launchIO {
viewModelScope.launchIO {
initSources()
mutableState.update { it.copy(isLoading = false) }
}