Replace StateViewModel with explicit backing fields (#3763)
Assisted-by: Claude:claude-opus-5
This commit is contained in:
@@ -36,6 +36,7 @@ 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 androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import cafe.adriel.voyager.navigator.LocalNavigator
|
||||
@@ -48,8 +49,9 @@ import eu.kanade.presentation.util.Screen
|
||||
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.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
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
|
||||
@@ -309,7 +311,10 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
|
||||
class Model(
|
||||
val sourcePreferences: SourcePreferences = Injekt.get(),
|
||||
private val sourceManager: SourceManager = Injekt.get(),
|
||||
) : StateViewModel<Model.State>(State()) {
|
||||
) : ViewModel() {
|
||||
|
||||
val state: StateFlow<Model.State>
|
||||
field = MutableStateFlow<Model.State>(State())
|
||||
|
||||
private val sourcesComparator = { includedSources: List<Long> ->
|
||||
compareBy<MigrationSource>(
|
||||
@@ -322,12 +327,12 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
|
||||
init {
|
||||
viewModelScope.launchIO {
|
||||
initSources()
|
||||
mutableState.update { it.copy(isLoading = false) }
|
||||
state.update { it.copy(isLoading = false) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateSources(action: (List<MigrationSource>) -> List<MigrationSource>) {
|
||||
mutableState.update { state ->
|
||||
state.update { state ->
|
||||
val updatedSources = action(state.sources)
|
||||
val includedSources = updatedSources.mapNotNull { if (!it.isSelected) null else it.id }
|
||||
state.copy(sources = updatedSources.sortedWith(sourcesComparator(includedSources)))
|
||||
@@ -364,7 +369,7 @@ class MigrationConfigScreen(private val mangaIds: Collection<Long>) : Screen() {
|
||||
}
|
||||
.toList()
|
||||
|
||||
mutableState.update { state ->
|
||||
state.update { state ->
|
||||
state.copy(sources = sources.sortedWith(sourcesComparator(includedSources)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,14 +18,16 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.util.fastForEach
|
||||
import androidx.lifecycle.ViewModel
|
||||
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.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
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
|
||||
@@ -128,7 +130,10 @@ class MigrateDialogViewModel(
|
||||
private val coverCache: CoverCache = Injekt.get(),
|
||||
private val downloadManager: DownloadManager = Injekt.get(),
|
||||
private val migrateManga: MigrateMangaUseCase = Injekt.get(),
|
||||
) : StateViewModel<MigrateDialogViewModel.State>(State()) {
|
||||
) : ViewModel() {
|
||||
|
||||
val state: StateFlow<MigrateDialogViewModel.State>
|
||||
field = MutableStateFlow<MigrateDialogViewModel.State>(State())
|
||||
|
||||
fun init(current: Manga, target: Manga) {
|
||||
val applicableFlags = buildList {
|
||||
@@ -144,7 +149,7 @@ class MigrateDialogViewModel(
|
||||
}
|
||||
}
|
||||
val selectedFlags = sourcePreference.migrationFlags.get()
|
||||
mutableState.update {
|
||||
state.update {
|
||||
State(
|
||||
current = current,
|
||||
target = target,
|
||||
@@ -155,7 +160,7 @@ class MigrateDialogViewModel(
|
||||
}
|
||||
|
||||
fun toggleSelection(flag: MigrationFlag) {
|
||||
mutableState.update {
|
||||
state.update {
|
||||
val selectedFlags = it.selectedFlags.toMutableSet()
|
||||
.apply { if (contains(flag)) remove(flag) else add(flag) }
|
||||
.toSet()
|
||||
@@ -164,13 +169,13 @@ class MigrateDialogViewModel(
|
||||
}
|
||||
|
||||
suspend fun migrateManga(replace: Boolean) {
|
||||
val state = state.value
|
||||
val current = state.current ?: return
|
||||
val target = state.target ?: return
|
||||
sourcePreference.migrationFlags.set(state.selectedFlags)
|
||||
mutableState.update { it.copy(isMigrating = true) }
|
||||
val currentState = state.value
|
||||
val current = currentState.current ?: return
|
||||
val target = currentState.target ?: return
|
||||
sourcePreference.migrationFlags.set(currentState.selectedFlags)
|
||||
state.update { it.copy(isMigrating = true) }
|
||||
migrateManga(current, target, replace)
|
||||
mutableState.update { it.copy(isMigrating = false, isMigrated = true) }
|
||||
state.update { it.copy(isMigrating = false, isMigrated = true) }
|
||||
}
|
||||
|
||||
data class State(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mihon.feature.migration.list
|
||||
|
||||
import androidx.annotation.FloatRange
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import androidx.lifecycle.viewmodel.initializer
|
||||
@@ -16,13 +17,14 @@ import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.receiveAsFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
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
|
||||
@@ -49,7 +51,10 @@ class MigrationListViewModel(
|
||||
private val getChaptersByMangaId: GetChaptersByMangaId = Injekt.get(),
|
||||
private val migrateManga: MigrateMangaUseCase = Injekt.get(),
|
||||
private val updateMangaFromRemote: UpdateMangaFromRemote = Injekt.get(),
|
||||
) : StateViewModel<MigrationListViewModel.State>(State()) {
|
||||
) : ViewModel() {
|
||||
|
||||
val state: StateFlow<MigrationListViewModel.State>
|
||||
field = MutableStateFlow<MigrationListViewModel.State>(State())
|
||||
|
||||
companion object {
|
||||
val MANGA_IDS_KEY = CreationExtras.Key<Collection<Long>>()
|
||||
@@ -97,7 +102,7 @@ class MigrationListViewModel(
|
||||
}
|
||||
.awaitAll()
|
||||
.filterNotNull()
|
||||
mutableState.update { it.copy(items = manga) }
|
||||
state.update { it.copy(items = manga) }
|
||||
runMigrations(manga)
|
||||
}
|
||||
}
|
||||
@@ -215,7 +220,7 @@ class MigrationListViewModel(
|
||||
}
|
||||
|
||||
private suspend fun updateMigrationProgress() {
|
||||
mutableState.update { state ->
|
||||
state.update { state ->
|
||||
state.copy(
|
||||
finishedCount = items.count { it.searchResult.value != SearchResult.Searching },
|
||||
migrationComplete = migrationComplete(),
|
||||
@@ -265,7 +270,7 @@ class MigrationListViewModel(
|
||||
|
||||
private fun migrateMangas(replace: Boolean) {
|
||||
migrateJob = viewModelScope.launchIO {
|
||||
mutableState.update { it.copy(dialog = Dialog.Progress(0f)) }
|
||||
state.update { it.copy(dialog = Dialog.Progress(0f)) }
|
||||
val items = items
|
||||
try {
|
||||
items.forEachIndexed { index, manga ->
|
||||
@@ -285,14 +290,14 @@ class MigrationListViewModel(
|
||||
if (e is CancellationException) throw e
|
||||
logcat(LogPriority.WARN, throwable = e)
|
||||
}
|
||||
mutableState.update {
|
||||
state.update {
|
||||
it.copy(dialog = Dialog.Progress((index.toFloat() / items.size).coerceAtMost(1f)))
|
||||
}
|
||||
}
|
||||
|
||||
navigateBack()
|
||||
} finally {
|
||||
mutableState.update { it.copy(dialog = null) }
|
||||
state.update { it.copy(dialog = null) }
|
||||
migrateJob = null
|
||||
}
|
||||
}
|
||||
@@ -327,7 +332,7 @@ class MigrationListViewModel(
|
||||
}
|
||||
|
||||
private fun removeManga(item: MigratingManga) {
|
||||
mutableState.update { it.copy(items = items.toMutableList().apply { remove(item) }) }
|
||||
state.update { it.copy(items = items.toMutableList().apply { remove(item) }) }
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
@@ -337,7 +342,7 @@ class MigrationListViewModel(
|
||||
}
|
||||
|
||||
fun showMigrateDialog(copy: Boolean) {
|
||||
mutableState.update { state ->
|
||||
state.update { state ->
|
||||
state.copy(
|
||||
dialog = Dialog.Migrate(
|
||||
copy = copy,
|
||||
@@ -349,13 +354,13 @@ class MigrationListViewModel(
|
||||
}
|
||||
|
||||
fun showExitDialog() {
|
||||
mutableState.update {
|
||||
state.update {
|
||||
it.copy(dialog = Dialog.Exit)
|
||||
}
|
||||
}
|
||||
|
||||
fun dismissDialog() {
|
||||
mutableState.update { it.copy(dialog = null) }
|
||||
state.update { it.copy(dialog = null) }
|
||||
}
|
||||
|
||||
data class ChapterInfo(
|
||||
|
||||
Reference in New Issue
Block a user