Skip to content

Commit

Permalink
Add confirmation dialog for TaskPreference
Browse files Browse the repository at this point in the history
  • Loading branch information
xb2016 committed Sep 29, 2024
1 parent a3e329b commit 10fb822
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,21 @@ class CleanRedundancyPreference(
return Settings.downloadLocation?.listFiles()?.sumOf { clearFile(it).compareTo(false) } ?: 0
}

override val jobTitle = GetText.getString(R.string.settings_download_clean_redundancy)

override fun launchJob() {
if (singletonJob?.isActive == true) {
singletonJob?.invokeOnCompletion {
launchUI {
dialog?.dismiss()
mDialog.dismiss()
}
}
} else {
singletonJob = launch {
val cnt = doRealWork()
withUIContext {
showTip(FINAL_CLEAR_REDUNDANCY_MSG(cnt))
dialog?.dismiss()
mDialog.dismiss()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.hippo.ehviewer.preference

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import com.hippo.ehviewer.EhDB
Expand Down Expand Up @@ -42,8 +43,11 @@ class RestoreDownloadPreference(
context: Context,
attrs: AttributeSet? = null,
) : TaskPreference(context, attrs) {
private val mManager = downloadManager
private var restoreDirCount = 0

@SuppressLint("ParcelCreator")
private class RestoreItem(val dirname: String, gid: Long, token: String) : BaseGalleryInfo(gid, token)

private fun getRestoreItem(dir: UniFile): RestoreItem? {
if (!dir.isDirectory) return null
return runCatching {
Expand All @@ -58,8 +62,8 @@ class RestoreDownloadPreference(
} ?: return null
val gid = result.gid
val dirname = dir.name!!
if (mManager.containDownloadInfo(gid)) {
// Restore download dir to avoid redownload
if (downloadManager.containDownloadInfo(gid)) {
// Restore download dir to avoid re-download
val dbDirName = EhDB.getDownloadDirname(gid)
if (null == dbDirName || dirname != dbDirName) {
EhDB.putDownloadDirname(gid, dirname)
Expand All @@ -85,12 +89,13 @@ class RestoreDownloadPreference(
}.getOrNull()
}

private class RestoreItem(val dirname: String, gid: Long, token: String) : BaseGalleryInfo(gid, token)
override val jobTitle = GetText.getString(R.string.settings_download_restore_download_items)

override fun launchJob() {
if (singletonJob?.isActive == true) {
singletonJob?.invokeOnCompletion {
launchUI {
dialog?.dismiss()
mDialog.dismiss()
}
}
} else {
Expand All @@ -111,7 +116,7 @@ class RestoreDownloadPreference(
// Avoid failed gallery info
if (null != item.title) {
// Put to download
mManager.addDownload(item, null)
downloadManager.addDownload(item, null)
// Put download dir to DB
EhDB.putDownloadDirname(item.gid, item.dirname)
count++
Expand All @@ -121,7 +126,7 @@ class RestoreDownloadPreference(
showTip(RESTORE_COUNT_MSG(count + restoreDirCount))
}
}
dialog?.dismiss()
mDialog.dismiss()
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions app/src/main/java/com/hippo/ehviewer/preference/TaskPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.hippo.ehviewer.preference

import android.content.Context
import android.content.DialogInterface
import android.util.AttributeSet
import androidx.appcompat.app.AlertDialog
import com.google.android.material.snackbar.Snackbar
Expand All @@ -30,18 +31,27 @@ abstract class TaskPreference(
context: Context,
attrs: AttributeSet? = null,
) : DialogPreference(context, attrs), CoroutineScope {
lateinit var mDialog: AlertDialog

override val coroutineContext = Dispatchers.IO + Job()

override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) {
builder.setTitle(null)
builder.setView(R.layout.preference_dialog_task)
builder.setCancelable(false)
builder.setTitle(jobTitle)
.setMessage(R.string.settings_download_task_confirm)
.setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int ->
mDialog = AlertDialog.Builder(context)
.setTitle(null)
.setView(R.layout.preference_dialog_task)
.setCancelable(false)
.show()
launchJob()
}
.setNegativeButton(android.R.string.cancel, null)
}

abstract fun launchJob()
abstract val jobTitle: String?

override fun onDialogCreated(dialog: AlertDialog) {
launchJob()
}
abstract fun launchJob()

protected fun showTip(msg: String) {
(context as SettingsActivity).showTip(
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@
<string name="settings_download_download_origin_image_never">从不启用</string>
<string name="settings_download_download_origin_image_force">总是启用</string>
<string name="settings_download_download_origin_image_only">仅下载时启用</string>
<string name="settings_download_task_confirm">确定要执行操作?</string>
<string name="settings_download_restore_download_items">恢复下载项</string>
<string name="settings_download_restore_download_items_summary">恢复下载目录里的所有下载项</string>
<string name="settings_download_restore_not_found">未找到可恢复下载项</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@
<string name="settings_download_download_origin_image_never">從不啓用</string>
<string name="settings_download_download_origin_image_force">總是啓用</string>
<string name="settings_download_download_origin_image_only">僅下載時啓用</string>
<string name="settings_download_task_confirm">確定要執行操作?</string>
<string name="settings_download_restore_download_items">恢復下載項</string>
<string name="settings_download_restore_download_items_summary">恢復下載目錄裏的所有下載項</string>
<string name="settings_download_restore_not_found">未找到可恢復下載項</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@
<string name="settings_download_download_origin_image_never">從不啟用</string>
<string name="settings_download_download_origin_image_force">總是啟用</string>
<string name="settings_download_download_origin_image_only">僅下載時啟用</string>
<string name="settings_download_task_confirm">確定要執行操作?</string>
<string name="settings_download_restore_download_items">回復下載的檔案</string>
<string name="settings_download_restore_download_items_summary">回復下載路徑中的所有下載的檔案</string>
<string name="settings_download_restore_not_found">未尋獲可回復的已下載檔案</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@
<string name="settings_download_download_origin_image_never">Never</string>
<string name="settings_download_download_origin_image_force">Always</string>
<string name="settings_download_download_origin_image_only">Download only</string>
<string name="settings_download_task_confirm">Are you sure you want to perform this action?</string>
<string name="settings_download_restore_download_items">Restore download items</string>
<string name="settings_download_restore_download_items_summary">Restore all download items in download location</string>
<string name="settings_download_restore_not_found">Not found download items to restore</string>
Expand Down

0 comments on commit 10fb822

Please sign in to comment.