From 6f1a08f2c7c4fc15834dbaf068102eb2c2403525 Mon Sep 17 00:00:00 2001 From: tarsin Date: Tue, 6 Jun 2023 00:46:33 +0800 Subject: [PATCH] [DO NOT USE] Introduce WorkPreference --- .../ehviewer/ui/settings/DownloadScreen.kt | 83 +++++++++---------- .../ehviewer/ui/settings/PreferenceWidgets.kt | 16 ++++ 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/ui/settings/DownloadScreen.kt b/app/src/main/java/com/hippo/ehviewer/ui/settings/DownloadScreen.kt index 188d6d660..9f4576193 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/settings/DownloadScreen.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/settings/DownloadScreen.kt @@ -43,7 +43,6 @@ import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder import com.hippo.ehviewer.ui.login.LocalNavController import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.util.lang.launchNonCancellable -import eu.kanade.tachiyomi.util.lang.withUIContext import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import moe.tarsin.coroutines.runSuspendCatching @@ -151,7 +150,7 @@ fun DownloadScreen() { value = Settings::downloadOriginImage, ) val restoreFailed = stringResource(id = R.string.settings_download_restore_failed) - Preference( + WorkPreference( title = stringResource(id = R.string.settings_download_restore_download_items), summary = stringResource(id = R.string.settings_download_restore_download_items_summary), ) { @@ -180,57 +179,53 @@ fun DownloadScreen() { it.printStackTrace() }.getOrNull() } - coroutineScope.launch { - val alertDialog = withUIContext { BaseDialogBuilder(context).setCancelable(false).setView(R.layout.preference_dialog_task).show() } - context.runCatching { - val result = downloadLocation.listFiles()?.mapNotNull { getRestoreItem(it) }?.apply { - runSuspendCatching { - fillGalleryListByApi(this, EhUrl.referer) - }.onFailure { - it.printStackTrace() - } - } - if (result == null) { - launchSnackBar(restoreFailed) - } else { - if (result.isEmpty()) { - launchSnackBar(RESTORE_COUNT_MSG(restoreDirCount)) - } else { - val count = result.filterNot { it.title.isNullOrBlank() }.map { - DownloadManager.addDownload(it, null) - EhDB.putDownloadDirname(it.gid, it.dirname) - }.size - launchSnackBar(RESTORE_COUNT_MSG(count + restoreDirCount)) - } + runCatching { + val result = downloadLocation.listFiles()?.mapNotNull { getRestoreItem(it) }?.apply { + runSuspendCatching { + fillGalleryListByApi(this, EhUrl.referer) + }.onFailure { + it.printStackTrace() } } - if (alertDialog.isShowing) alertDialog.dismiss() + if (result == null) { + launchSnackBar(restoreFailed) + } else { + if (result.isEmpty()) { + launchSnackBar(RESTORE_COUNT_MSG(restoreDirCount)) + } else { + val count = result.filterNot { it.title.isNullOrBlank() }.map { + DownloadManager.addDownload(it, null) + EhDB.putDownloadDirname(it.gid, it.dirname) + }.size + launchSnackBar(RESTORE_COUNT_MSG(count + restoreDirCount)) + } + } + }.onFailure { + it.printStackTrace() } } - Preference( + WorkPreference( title = stringResource(id = R.string.settings_download_clean_redundancy), summary = stringResource(id = R.string.settings_download_clean_redundancy_summary), ) { - coroutineScope.launch { - fun clearFile(file: UniFile): Boolean { - var name = file.name ?: return false - val index = name.indexOf('-') - if (index >= 0) { - name = name.substring(0, index) - } - val gid = name.toLongOrNull() ?: return false - if (DownloadManager.containDownloadInfo(gid)) { - return false - } - file.delete() - return true + fun clearFile(file: UniFile): Boolean { + var name = file.name ?: return false + val index = name.indexOf('-') + if (index >= 0) { + name = name.substring(0, index) } - val alertDialog = withUIContext { BaseDialogBuilder(context).setCancelable(false).setView(R.layout.preference_dialog_task).show() } - context.runCatching { - val cnt = downloadLocation.listFiles()?.sumOf { clearFile(it).compareTo(false) } ?: 0 - launchSnackBar(FINAL_CLEAR_REDUNDANCY_MSG(cnt)) + val gid = name.toLongOrNull() ?: return false + if (DownloadManager.containDownloadInfo(gid)) { + return false } - if (alertDialog.isShowing) alertDialog.dismiss() + file.delete() + return true + } + runSuspendCatching { + val cnt = downloadLocation.listFiles()?.sumOf { clearFile(it).compareTo(false) } ?: 0 + launchSnackBar(FINAL_CLEAR_REDUNDANCY_MSG(cnt)) + }.onFailure { + it.printStackTrace() } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/settings/PreferenceWidgets.kt b/app/src/main/java/com/hippo/ehviewer/ui/settings/PreferenceWidgets.kt index cfd4cb4d7..76e485235 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/settings/PreferenceWidgets.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/settings/PreferenceWidgets.kt @@ -18,6 +18,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -29,6 +30,8 @@ import androidx.compose.ui.res.stringArrayResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.unit.dp +import com.hippo.ehviewer.R +import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder import com.hippo.ehviewer.ui.login.LocalNavController import com.hippo.ehviewer.ui.openBrowser import com.hippo.ehviewer.ui.settings.PreferenceTokens.PreferenceTextPadding @@ -37,6 +40,9 @@ import com.jamal.composeprefs3.ui.prefs.SliderPref import com.jamal.composeprefs3.ui.prefs.SpannedTextPref import com.jamal.composeprefs3.ui.prefs.SwitchPref import com.jamal.composeprefs3.ui.prefs.TextPref +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import kotlin.math.roundToInt import kotlin.reflect.KMutableProperty0 @@ -121,3 +127,13 @@ fun SimpleMenuPreference(title: String, @ArrayRes entry: Int, @ArrayRes entryVal check(entryArray.size == valuesArray.size) DropDownPref(title = title, defaultValue = v, onValueChange = ::set, useSelectedAsSummary = true, entries = map) } + +@Composable +fun WorkPreference(title: String, summary: String? = null, work: suspend CoroutineScope.() -> Unit) { + val coroutineScope = rememberCoroutineScope { Dispatchers.IO } + val context = LocalContext.current + Preference(title = title, summary = summary) { + val alertDialog = BaseDialogBuilder(context).setCancelable(false).setView(R.layout.preference_dialog_task).show() + coroutineScope.launch(block = work).invokeOnCompletion { if (alertDialog.isShowing) alertDialog.dismiss() } + } +}