Fix clearing database freezes the app (#8492)

This commit is contained in:
AntsyLich 2022-11-10 18:59:31 +06:00 committed by GitHub
parent d0236aaecf
commit 6ce54eb845
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
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.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext 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.Database
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.lang.launchIO 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 eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
@ -61,6 +64,7 @@ class ClearDatabaseScreen : Screen {
val navigator = LocalNavigator.currentOrThrow val navigator = LocalNavigator.currentOrThrow
val model = rememberScreenModel { ClearDatabaseScreenModel() } val model = rememberScreenModel { ClearDatabaseScreenModel() }
val state by model.state.collectAsState() val state by model.state.collectAsState()
val scope = rememberCoroutineScope()
when (val s = state) { when (val s = state) {
is ClearDatabaseScreenModel.State.Loading -> LoadingScreen() is ClearDatabaseScreenModel.State.Loading -> LoadingScreen()
@ -71,10 +75,12 @@ class ClearDatabaseScreen : Screen {
confirmButton = { confirmButton = {
TextButton( TextButton(
onClick = { onClick = {
scope.launchUI {
model.removeMangaBySourceId() model.removeMangaBySourceId()
model.clearSelection() model.clearSelection()
model.hideConfirmation() model.hideConfirmation()
context.toast(R.string.clear_database_completed) context.toast(R.string.clear_database_completed)
}
}, },
) { ) {
Text(text = stringResource(android.R.string.ok)) Text(text = stringResource(android.R.string.ok))
@ -217,8 +223,8 @@ private class ClearDatabaseScreenModel : StateScreenModel<ClearDatabaseScreenMod
} }
} }
fun removeMangaBySourceId() { suspend fun removeMangaBySourceId() = withNonCancellableContext {
val state = state.value as? State.Ready ?: return val state = state.value as? State.Ready ?: return@withNonCancellableContext
database.mangasQueries.deleteMangasNotInLibraryBySourceIds(state.selection) database.mangasQueries.deleteMangasNotInLibraryBySourceIds(state.selection)
database.historyQueries.removeResettedHistory() database.historyQueries.removeResettedHistory()
} }