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) }
}
@@ -18,14 +18,14 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.util.fastForEach
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.rememberScreenModel
import androidx.lifecycle.viewmodel.compose.viewModel
import cafe.adriel.voyager.core.screen.Screen
import eu.kanade.domain.manga.model.hasCustomCover
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.download.DownloadManager
import kotlinx.coroutines.flow.update
import mihon.core.viewmodel.StateViewModel
import mihon.domain.migration.models.MigrationFlag
import mihon.domain.migration.usecases.MigrateMangaUseCase
import mihon.feature.common.utils.getLabel
@@ -50,11 +50,11 @@ internal fun Screen.MigrateMangaDialog(
) {
val scope = rememberCoroutineScope()
val screenModel = rememberScreenModel { MigrateDialogScreenModel() }
val viewModel = viewModel<MigrateDialogViewModel>()
LaunchedEffect(current, target) {
screenModel.init(current, target)
viewModel.init(current, target)
}
val state by screenModel.state.collectAsState()
val state by viewModel.state.collectAsState()
if (state.isMigrated) return
@@ -78,7 +78,7 @@ internal fun Screen.MigrateMangaDialog(
LabeledCheckbox(
label = stringResource(flag.getLabel()),
checked = flag in state.selectedFlags,
onCheckedChange = { screenModel.toggleSelection(flag) },
onCheckedChange = { viewModel.toggleSelection(flag) },
)
}
}
@@ -101,7 +101,7 @@ internal fun Screen.MigrateMangaDialog(
TextButton(
onClick = {
scope.launchIO {
screenModel.migrateManga(replace = false)
viewModel.migrateManga(replace = false)
withUIContext { onComplete() }
}
},
@@ -111,7 +111,7 @@ internal fun Screen.MigrateMangaDialog(
TextButton(
onClick = {
scope.launchIO {
screenModel.migrateManga(replace = true)
viewModel.migrateManga(replace = true)
withUIContext { onComplete() }
}
},
@@ -123,12 +123,12 @@ internal fun Screen.MigrateMangaDialog(
)
}
private class MigrateDialogScreenModel(
private class MigrateDialogViewModel(
private val sourcePreference: SourcePreferences = Injekt.get(),
private val coverCache: CoverCache = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get(),
private val migrateManga: MigrateMangaUseCase = Injekt.get(),
) : StateScreenModel<MigrateDialogScreenModel.State>(State()) {
) : StateViewModel<MigrateDialogViewModel.State>(State()) {
fun init(current: Manga, target: Manga) {
val applicableFlags = buildList {
@@ -7,7 +7,8 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import cafe.adriel.voyager.core.model.rememberScreenModel
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.compose.viewModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import eu.kanade.presentation.util.Screen
@@ -30,13 +31,19 @@ class MigrationListScreen(private val mangaIds: Collection<Long>, private val ex
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val screenModel = rememberScreenModel { MigrationListScreenModel(mangaIds, extraSearchQuery) }
val state by screenModel.state.collectAsState()
val viewModel = viewModel<MigrationListViewModel>(
factory = MigrationListViewModel.Factory,
extras = CreationExtras {
set(MigrationListViewModel.MANGA_IDS_KEY, mangaIds)
set(MigrationListViewModel.EXTRA_SEARCH_QUERY_KEY, extraSearchQuery)
},
)
val state by viewModel.state.collectAsState()
val context = LocalContext.current
LaunchedEffect(matchOverride) {
val (current, target) = matchOverride ?: return@LaunchedEffect
screenModel.useMangaForMigration(
viewModel.useMangaForMigration(
current = current,
target = target,
onMissingChapters = {
@@ -46,8 +53,8 @@ class MigrationListScreen(private val mangaIds: Collection<Long>, private val ex
matchOverride = null
}
LaunchedEffect(screenModel) {
screenModel.navigateBackEvent.collect {
LaunchedEffect(viewModel) {
viewModel.navigateBackEvent.collect {
navigator.pop()
}
}
@@ -61,37 +68,37 @@ class MigrationListScreen(private val mangaIds: Collection<Long>, private val ex
onSearchManually = { migrationItem ->
navigator push MigrateSearchScreen(migrationItem.manga.id)
},
onSkip = { screenModel.removeManga(it) },
onMigrate = { screenModel.migrateNow(mangaId = it, replace = true) },
onCopy = { screenModel.migrateNow(mangaId = it, replace = false) },
openMigrationDialog = screenModel::showMigrateDialog,
onSkip = { viewModel.removeManga(it) },
onMigrate = { viewModel.migrateNow(mangaId = it, replace = true) },
onCopy = { viewModel.migrateNow(mangaId = it, replace = false) },
openMigrationDialog = viewModel::showMigrateDialog,
)
when (val dialog = state.dialog) {
is MigrationListScreenModel.Dialog.Migrate -> {
is MigrationListViewModel.Dialog.Migrate -> {
MigrationMangaDialog(
onDismissRequest = screenModel::dismissDialog,
onDismissRequest = viewModel::dismissDialog,
copy = dialog.copy,
totalCount = dialog.totalCount,
skippedCount = dialog.skippedCount,
onMigrate = {
if (dialog.copy) {
screenModel.copyMangas()
viewModel.copyMangas()
} else {
screenModel.migrateMangas()
viewModel.migrateMangas()
}
},
)
}
is MigrationListScreenModel.Dialog.Progress -> {
is MigrationListViewModel.Dialog.Progress -> {
MigrationProgressDialog(
progress = dialog.progress,
exitMigration = screenModel::cancelMigrate,
exitMigration = viewModel::cancelMigrate,
)
}
MigrationListScreenModel.Dialog.Exit -> {
MigrationListViewModel.Dialog.Exit -> {
MigrationExitDialog(
onDismissRequest = screenModel::dismissDialog,
onDismissRequest = viewModel::dismissDialog,
exitMigration = navigator::pop,
)
}
@@ -99,7 +106,7 @@ class MigrationListScreen(private val mangaIds: Collection<Long>, private val ex
}
BackHandler(true) {
screenModel.showExitDialog()
viewModel.showExitDialog()
}
}
}
@@ -1,9 +1,10 @@
package mihon.feature.migration.list
import androidx.annotation.FloatRange
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.getNameForMangaInfo
@@ -21,6 +22,7 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import logcat.LogPriority
import mihon.core.viewmodel.StateViewModel
import mihon.domain.migration.usecases.MigrateMangaUseCase
import mihon.domain.source.interactor.UpdateMangaFromRemote
import mihon.feature.migration.list.models.MigratingManga
@@ -37,18 +39,32 @@ import tachiyomi.domain.source.service.SourceManager
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class MigrationListScreenModel(
class MigrationListViewModel(
mangaIds: Collection<Long>,
extraSearchQuery: String?,
private val preferences: SourcePreferences = Injekt.get(),
private val sourceManager: SourceManager = Injekt.get(),
private val getManga: GetManga = Injekt.get(),
private val networkToLocalManga: NetworkToLocalManga = Injekt.get(),
private val syncChaptersWithSource: SyncChaptersWithSource = Injekt.get(),
private val getChaptersByMangaId: GetChaptersByMangaId = Injekt.get(),
private val migrateManga: MigrateMangaUseCase = Injekt.get(),
private val updateMangaFromRemote: UpdateMangaFromRemote = Injekt.get(),
) : StateScreenModel<MigrationListScreenModel.State>(State()) {
) : StateViewModel<MigrationListViewModel.State>(State()) {
companion object {
val MANGA_IDS_KEY = CreationExtras.Key<Collection<Long>>()
val EXTRA_SEARCH_QUERY_KEY = CreationExtras.Key<String?>()
val Factory = viewModelFactory {
initializer {
MigrationListViewModel(
mangaIds = get(MANGA_IDS_KEY)!!,
extraSearchQuery = get(EXTRA_SEARCH_QUERY_KEY),
)
}
}
}
private val smartSearchEngine = SmartSourceSearchEngine(extraSearchQuery)
@@ -64,7 +80,7 @@ class MigrationListScreenModel(
private var migrateJob: Job? = null
init {
screenModelScope.launchIO {
viewModelScope.launchIO {
val manga = mangaIds
.map {
async {
@@ -75,7 +91,7 @@ class MigrationListScreenModel(
chapterCount = chapterInfo.chapterCount,
latestChapter = chapterInfo.latestChapter,
source = sourceManager.getOrStub(manga.source).getNameForMangaInfo(),
parentContext = screenModelScope.coroutineContext,
parentContext = viewModelScope.coroutineContext,
)
}
}
@@ -216,7 +232,7 @@ class MigrationListScreenModel(
fun useMangaForMigration(current: Long, target: Long, onMissingChapters: () -> Unit) {
val migratingManga = items.find { it.manga.id == current } ?: return
migratingManga.searchResult.value = SearchResult.Searching
screenModelScope.launchIO {
viewModelScope.launchIO {
val result = migratingManga.migrationScope.async {
val manga = getManga.await(target) ?: return@async null
try {
@@ -248,7 +264,7 @@ class MigrationListScreenModel(
}
private fun migrateMangas(replace: Boolean) {
migrateJob = screenModelScope.launchIO {
migrateJob = viewModelScope.launchIO {
mutableState.update { it.copy(dialog = Dialog.Progress(0f)) }
val items = items
try {
@@ -292,7 +308,7 @@ class MigrationListScreenModel(
}
fun migrateNow(mangaId: Long, replace: Boolean) {
screenModelScope.launchIO {
viewModelScope.launchIO {
val manga = items.find { it.manga.id == mangaId } ?: return@launchIO
val target = (manga.searchResult.value as? SearchResult.Success)?.manga ?: return@launchIO
migrateManga(current = manga.manga, target = target, replace = replace)
@@ -302,7 +318,7 @@ class MigrationListScreenModel(
}
fun removeManga(mangaId: Long) {
screenModelScope.launchIO {
viewModelScope.launchIO {
val item = items.find { it.manga.id == mangaId } ?: return@launchIO
removeManga(item)
item.migrationScope.cancel()
@@ -314,8 +330,7 @@ class MigrationListScreenModel(
mutableState.update { it.copy(items = items.toMutableList().apply { remove(item) }) }
}
override fun onDispose() {
super.onDispose()
override fun onCleared() {
items.forEach {
it.migrationScope.cancel()
}
@@ -3,7 +3,7 @@ package mihon.feature.upcoming
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import cafe.adriel.voyager.core.model.rememberScreenModel
import androidx.lifecycle.viewmodel.compose.viewModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import eu.kanade.presentation.util.Screen
@@ -15,12 +15,12 @@ class UpcomingScreen : Screen() {
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val screenModel = rememberScreenModel { UpcomingScreenModel() }
val state by screenModel.state.collectAsState()
val viewModel = viewModel<UpcomingViewModel>()
val state by viewModel.state.collectAsState()
UpcomingScreenContent(
state = state,
setSelectedYearMonth = screenModel::setSelectedYearMonth,
setSelectedYearMonth = viewModel::setSelectedYearMonth,
onClickUpcoming = { navigator.push(MangaScreen(it.id)) },
)
}
@@ -41,7 +41,7 @@ import java.time.YearMonth
@Composable
fun UpcomingScreenContent(
state: UpcomingScreenModel.State,
state: UpcomingViewModel.State,
setSelectedYearMonth: (YearMonth) -> Unit,
onClickUpcoming: (manga: Manga) -> Unit,
modifier: Modifier = Modifier,
@@ -2,13 +2,13 @@ package mihon.feature.upcoming
import androidx.compose.ui.util.fastMap
import androidx.compose.ui.util.fastMapIndexedNotNull
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import androidx.lifecycle.viewModelScope
import eu.kanade.core.util.insertSeparatorsReversed
import eu.kanade.tachiyomi.util.lang.toLocalDate
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import mihon.core.viewmodel.StateViewModel
import mihon.domain.upcoming.interactor.GetUpcomingManga
import tachiyomi.domain.manga.model.Manga
import uy.kohesive.injekt.Injekt
@@ -16,12 +16,12 @@ import uy.kohesive.injekt.api.get
import java.time.LocalDate
import java.time.YearMonth
class UpcomingScreenModel(
class UpcomingViewModel(
private val getUpcomingManga: GetUpcomingManga = Injekt.get(),
) : StateScreenModel<UpcomingScreenModel.State>(State()) {
) : StateViewModel<UpcomingViewModel.State>(State()) {
init {
screenModelScope.launch {
viewModelScope.launch {
getUpcomingManga.subscribe().collectLatest {
mutableState.update { state ->
val upcomingItems = it.toUpcomingUIModels()