[DO NOT USE] Introduce WorkPreference

This commit is contained in:
tarsin committed 2023-06-06 00:46:33 +08:00
1 parent 563f6810be
commit 6f1a08f2c7
2 files changed
+55 -44

No files matched your search

@@ -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()
}
}
}
@@ -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() }
}
}