Fix clearing database freezes the app (#8492)
This commit is contained in:
parent
d0236aaecf
commit
6ce54eb845
1 changed files with 12 additions and 6 deletions
|
@ -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 = {
|
||||||
model.removeMangaBySourceId()
|
scope.launchUI {
|
||||||
model.clearSelection()
|
model.removeMangaBySourceId()
|
||||||
model.hideConfirmation()
|
model.clearSelection()
|
||||||
context.toast(R.string.clear_database_completed)
|
model.hideConfirmation()
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue