mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-06 17:18:56 +01:00
Fix migration dialog migrating to wrong entry (#2631)
This commit is contained in:
@@ -12,6 +12,7 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
@@ -49,9 +50,14 @@ internal fun Screen.MigrateMangaDialog(
|
||||
) {
|
||||
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()
|
||||
|
||||
if (state.isMigrated) return
|
||||
|
||||
if (state.isMigrating) {
|
||||
LoadingScreen(
|
||||
modifier = Modifier.background(MaterialTheme.colorScheme.background.copy(alpha = 0.7f)),
|
||||
@@ -118,15 +124,13 @@ internal fun Screen.MigrateMangaDialog(
|
||||
}
|
||||
|
||||
private class MigrateDialogScreenModel(
|
||||
private val current: Manga,
|
||||
private val target: Manga,
|
||||
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()) {
|
||||
|
||||
init {
|
||||
fun init(current: Manga, target: Manga) {
|
||||
val applicableFlags = buildList {
|
||||
MigrationFlag.entries.forEach {
|
||||
val applicable = when (it) {
|
||||
@@ -140,7 +144,14 @@ private class MigrateDialogScreenModel(
|
||||
}
|
||||
}
|
||||
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) {
|
||||
@@ -153,15 +164,21 @@ private class MigrateDialogScreenModel(
|
||||
}
|
||||
|
||||
suspend fun migrateManga(replace: Boolean) {
|
||||
sourcePreference.migrationFlags().set(state.value.selectedFlags)
|
||||
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) }
|
||||
migrateManga(current, target, replace)
|
||||
mutableState.update { it.copy(isMigrating = false) }
|
||||
mutableState.update { it.copy(isMigrating = false, isMigrated = true) }
|
||||
}
|
||||
|
||||
data class State(
|
||||
val current: Manga? = null,
|
||||
val target: Manga? = null,
|
||||
val applicableFlags: List<MigrationFlag> = emptyList(),
|
||||
val selectedFlags: Set<MigrationFlag> = emptySet(),
|
||||
val isMigrating: Boolean = false,
|
||||
val isMigrated: Boolean = false,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user