From 6ce54eb845d5333cc3fc202f59585816b61b29da Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Thu, 10 Nov 2022 18:59:31 +0600 Subject: [PATCH] Fix clearing database freezes the app (#8492) --- .../settings/screen/ClearDatabaseScreen.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/ClearDatabaseScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/ClearDatabaseScreen.kt index a1442f7115..394aa1dd79 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/ClearDatabaseScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/ClearDatabaseScreen.kt @@ -20,6 +20,7 @@ import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -47,6 +48,8 @@ import eu.kanade.presentation.util.selectedBackground import eu.kanade.tachiyomi.Database import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.lang.launchIO +import eu.kanade.tachiyomi.util.lang.launchUI +import eu.kanade.tachiyomi.util.lang.withNonCancellableContext import eu.kanade.tachiyomi.util.system.toast import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.update @@ -61,6 +64,7 @@ class ClearDatabaseScreen : Screen { val navigator = LocalNavigator.currentOrThrow val model = rememberScreenModel { ClearDatabaseScreenModel() } val state by model.state.collectAsState() + val scope = rememberCoroutineScope() when (val s = state) { is ClearDatabaseScreenModel.State.Loading -> LoadingScreen() @@ -71,10 +75,12 @@ class ClearDatabaseScreen : Screen { confirmButton = { TextButton( onClick = { - model.removeMangaBySourceId() - model.clearSelection() - model.hideConfirmation() - context.toast(R.string.clear_database_completed) + scope.launchUI { + model.removeMangaBySourceId() + model.clearSelection() + model.hideConfirmation() + context.toast(R.string.clear_database_completed) + } }, ) { Text(text = stringResource(android.R.string.ok)) @@ -217,8 +223,8 @@ private class ClearDatabaseScreenModel : StateScreenModel