Fix migration dialog migrating to wrong entry (mihonapp/mihon#2631)
(cherry picked from commit 5e7fecc2c11b4a175fe1c3f698f7daeb58fe311f)
This commit is contained in:
parent
c6a0eaf67c
commit
e79b3f97f4
1 changed files with 26 additions and 9 deletions
|
|
@ -12,6 +12,7 @@ import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.TextButton
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
|
|
@ -49,9 +50,14 @@ internal fun Screen.MigrateMangaDialog(
|
||||||
) {
|
) {
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
|
|
||||||
val screenModel = rememberScreenModel { MigrateDialogScreenModel(current, target) }
|
val screenModel = rememberScreenModel { MigrateDialogScreenModel() }
|
||||||
|
LaunchedEffect(current, target) {
|
||||||
|
screenModel.init(current, target)
|
||||||
|
}
|
||||||
val state by screenModel.state.collectAsState()
|
val state by screenModel.state.collectAsState()
|
||||||
|
|
||||||
|
if (state.isMigrated) return
|
||||||
|
|
||||||
if (state.isMigrating) {
|
if (state.isMigrating) {
|
||||||
LoadingScreen(
|
LoadingScreen(
|
||||||
modifier = Modifier.background(MaterialTheme.colorScheme.background.copy(alpha = 0.7f)),
|
modifier = Modifier.background(MaterialTheme.colorScheme.background.copy(alpha = 0.7f)),
|
||||||
|
|
@ -121,15 +127,13 @@ internal fun Screen.MigrateMangaDialog(
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MigrateDialogScreenModel(
|
private class MigrateDialogScreenModel(
|
||||||
private val current: Manga,
|
val sourcePreference: SourcePreferences = Injekt.get(),
|
||||||
private val target: Manga,
|
|
||||||
sourcePreference: SourcePreferences = Injekt.get(),
|
|
||||||
private val coverCache: CoverCache = Injekt.get(),
|
private val coverCache: CoverCache = Injekt.get(),
|
||||||
private val downloadManager: DownloadManager = Injekt.get(),
|
private val downloadManager: DownloadManager = Injekt.get(),
|
||||||
private val migrateManga: MigrateMangaUseCase = Injekt.get(),
|
private val migrateManga: MigrateMangaUseCase = Injekt.get(),
|
||||||
) : StateScreenModel<MigrateDialogScreenModel.State>(State()) {
|
) : StateScreenModel<MigrateDialogScreenModel.State>(State()) {
|
||||||
|
|
||||||
init {
|
fun init(current: Manga, target: Manga) {
|
||||||
val applicableFlags = buildList {
|
val applicableFlags = buildList {
|
||||||
MigrationFlag.entries.forEach {
|
MigrationFlag.entries.forEach {
|
||||||
val applicable = when (it) {
|
val applicable = when (it) {
|
||||||
|
|
@ -149,7 +153,14 @@ private class MigrateDialogScreenModel(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val selectedFlags = sourcePreference.migrationFlags().get()
|
val selectedFlags = sourcePreference.migrationFlags().get()
|
||||||
mutableState.update { it.copy(applicableFlags = applicableFlags, selectedFlags = selectedFlags) }
|
mutableState.update {
|
||||||
|
it.copy(
|
||||||
|
current = current,
|
||||||
|
target = target,
|
||||||
|
applicableFlags = applicableFlags,
|
||||||
|
selectedFlags = selectedFlags,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toggleSelection(flag: MigrationFlag) {
|
fun toggleSelection(flag: MigrationFlag) {
|
||||||
|
|
@ -162,17 +173,23 @@ private class MigrateDialogScreenModel(
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun migrateManga(replace: Boolean) {
|
suspend fun migrateManga(replace: Boolean) {
|
||||||
|
val state = state.value
|
||||||
|
val current = state.current ?: return
|
||||||
|
val target = state.target ?: return
|
||||||
// KMK -->
|
// KMK -->
|
||||||
// sourcePreference.migrationFlags().set(state.value.selectedFlags)
|
// sourcePreference.migrationFlags().set(state.selectedFlags)
|
||||||
// KMK <--
|
// KMK <--
|
||||||
mutableState.update { it.copy(isMigrating = true) }
|
mutableState.update { it.copy(isMigrating = true) }
|
||||||
migrateManga(current, target, replace, /* KMK --> */ state.value.selectedFlags /* KMK <-- */)
|
migrateManga(current, target, replace, /* KMK --> */ state.selectedFlags /* KMK <-- */)
|
||||||
mutableState.update { it.copy(isMigrating = false) }
|
mutableState.update { it.copy(isMigrating = false, isMigrated = true) }
|
||||||
}
|
}
|
||||||
|
|
||||||
data class State(
|
data class State(
|
||||||
|
val current: Manga? = null,
|
||||||
|
val target: Manga? = null,
|
||||||
val applicableFlags: List<MigrationFlag> = emptyList(),
|
val applicableFlags: List<MigrationFlag> = emptyList(),
|
||||||
val selectedFlags: Set<MigrationFlag> = emptySet(),
|
val selectedFlags: Set<MigrationFlag> = emptySet(),
|
||||||
val isMigrating: Boolean = false,
|
val isMigrating: Boolean = false,
|
||||||
|
val isMigrated: Boolean = false,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue