Kill CheckBoxDialogBuilder

This commit is contained in:
GG Bond authored and Vladimir Dostoyevskiy committed 2023-11-25 15:53:36 +08:00
1 parent 04818e5afb
commit 2800a7bc15
5 files changed
+37 -150

No files matched your search

@@ -50,6 +50,7 @@ import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.dao.DownloadInfo
import com.hippo.ehviewer.download.DownloadManager
import com.hippo.ehviewer.download.DownloadService
import com.hippo.ehviewer.download.downloadDir
import com.hippo.ehviewer.download.downloadLocation
import com.hippo.ehviewer.ui.scene.BaseScene
import com.hippo.ehviewer.ui.tools.DialogState
@@ -334,6 +335,36 @@ suspend fun DialogState.confirmRemoveDownload(info: GalleryInfo, onDismiss: () -
}
}
suspend fun DialogState.confirmRemoveDownloadRange(list: List<DownloadInfo>) {
var checked by mutableStateOf(Settings.removeImageFiles)
awaitPermissionOrCancel(
title = R.string.download_remove_dialog_title,
text = {
Column {
Text(text = stringResource(id = R.string.download_remove_dialog_message_2, list.size))
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(checked = checked, onCheckedChange = { checked = it })
Text(text = stringResource(id = R.string.download_remove_dialog_check_text))
}
}
},
)
Settings.removeImageFiles = checked
withIOContext {
// Delete
DownloadManager.deleteRangeDownload(list.mapToLongArray(DownloadInfo::gid))
// Delete image files
if (checked) {
list.forEach { info ->
// Delete file
info.downloadDir?.delete()
// Remove download path
EhDB.removeDownloadDirname(info.gid)
}
}
}
}
suspend fun DialogState.showMoveDownloadLabel(info: GalleryInfo) {
val defaultLabel = appCtx.getString(R.string.default_download_label_name)
val labels = DownloadManager.labelList.map { it.label }.toTypedArray()
@@ -1,44 +0,0 @@
/*
* Copyright 2016 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.legacy
import android.content.Context
import android.view.LayoutInflater
import android.widget.CheckBox
import com.hippo.ehviewer.R
class CheckBoxDialogBuilder(
context: Context,
message: String?,
checkText: String?,
checked: Boolean,
) : BaseDialogBuilder(
context,
) {
private val mCheckBox: CheckBox
val isChecked: Boolean
get() = mCheckBox.isChecked
init {
val view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_checkbox_builder, null)
setView(view)
setMessage(message)
mCheckBox = view.findViewById(R.id.checkbox)
mCheckBox.text = checkText
mCheckBox.isChecked = checked
view.setOnClickListener { mCheckBox.toggle() }
}
}
@@ -97,9 +97,9 @@ import com.hippo.ehviewer.download.DownloadService.Companion.clear
import com.hippo.ehviewer.download.downloadDir
import com.hippo.ehviewer.ktbuilder.imageRequest
import com.hippo.ehviewer.ui.confirmRemoveDownload
import com.hippo.ehviewer.ui.confirmRemoveDownloadRange
import com.hippo.ehviewer.ui.legacy.AutoStaggeredGridLayoutManager
import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder
import com.hippo.ehviewer.ui.legacy.CheckBoxDialogBuilder
import com.hippo.ehviewer.ui.legacy.EditTextDialogBuilder
import com.hippo.ehviewer.ui.legacy.HandlerDrawable
import com.hippo.ehviewer.ui.legacy.ViewTransition
@@ -366,20 +366,7 @@ class DownloadsScene : SearchBarScene() {
},
Icons.Default.Delete to {
val downloadInfoList = tracker.getAndClearSelection()
val builder = CheckBoxDialogBuilder(
context,
getString(R.string.download_remove_dialog_message_2, downloadInfoList.size),
getString(R.string.download_remove_dialog_check_text),
Settings.removeImageFiles,
)
val helper = DeleteRangeDialogHelper(
downloadInfoList,
downloadInfoList.mapToLongArray(DownloadInfo::gid),
builder,
)
builder.setTitle(R.string.download_remove_dialog_title)
.setPositiveButton(android.R.string.ok, helper)
.apply { withUIContext { show() } }
dialogState.confirmRemoveDownloadRange(downloadInfoList)
},
Icons.AutoMirrored.Default.DriveFileMove to {
val downloadInfoList = tracker.getAndClearSelection()
@@ -656,34 +643,6 @@ class DownloadsScene : SearchBarScene() {
}
}
private inner class DeleteRangeDialogHelper(
private val mDownloadInfoList: List<DownloadInfo>,
private val mGidList: LongArray,
private val mBuilder: CheckBoxDialogBuilder,
) : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface, which: Int) {
if (which != DialogInterface.BUTTON_POSITIVE) {
return
}
lifecycleScope.launchIO {
// Delete
DownloadManager.deleteRangeDownload(mGidList)
// Delete image files
val checked = mBuilder.isChecked
Settings.removeImageFiles = checked
if (checked) {
mDownloadInfoList.forEach { info ->
// Delete file
info.downloadDir?.delete()
// Remove download path
EhDB.removeDownloadDirname(info.gid)
}
}
}
}
}
private inner class MoveDialogHelper(
private val mLabels: Array<String>,
private val mDownloadInfoList: List<DownloadInfo>,
@@ -139,10 +139,10 @@ import com.hippo.ehviewer.spider.SpiderQueen
import com.hippo.ehviewer.spider.SpiderQueen.Companion.MODE_READ
import com.hippo.ehviewer.ui.GalleryInfoBottomSheet
import com.hippo.ehviewer.ui.MainActivity
import com.hippo.ehviewer.ui.confirmRemoveDownload
import com.hippo.ehviewer.ui.getFavoriteIcon
import com.hippo.ehviewer.ui.jumpToReaderByPage
import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder
import com.hippo.ehviewer.ui.legacy.CheckBoxDialogBuilder
import com.hippo.ehviewer.ui.legacy.CoilImageGetter
import com.hippo.ehviewer.ui.legacy.GalleryRatingBar.OnUserRateListener
import com.hippo.ehviewer.ui.main.EhPreviewItem
@@ -178,6 +178,7 @@ import eu.kanade.tachiyomi.util.lang.withUIContext
import kotlin.math.roundToInt
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.parcelize.Parcelize
@@ -995,35 +996,9 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: NavController)
dialogState.startDownload(activity, false, galleryDetail.galleryInfo)
}
} else {
class DeleteDialogHelper(
private val mGalleryInfo: GalleryInfo,
private val mBuilder: CheckBoxDialogBuilder,
) : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface, which: Int) {
if (which != DialogInterface.BUTTON_POSITIVE) return
coroutineScope.launchIO {
val checked = mBuilder.isChecked
Settings.removeImageFiles = checked
EhDownloadManager.deleteDownload(mGalleryInfo.gid, checked)
}
}
coroutineScope.launch {
dialogState.confirmRemoveDownload(galleryDetail)
}
val builder = CheckBoxDialogBuilder(
context,
context.getString(
R.string.download_remove_dialog_message,
galleryDetail.title,
),
context.getString(R.string.download_remove_dialog_check_text),
Settings.removeImageFiles,
)
val helper = DeleteDialogHelper(
galleryDetail,
builder,
)
builder.setTitle(R.string.download_remove_dialog_title)
.setPositiveButton(android.R.string.ok, helper)
.show()
}
}
@@ -1,34 +0,0 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2016 Hippo Seven
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?selectableItemBackground"
android:minHeight="?listPreferredItemHeightSmall"
android:orientation="vertical"
android:paddingHorizontal="?attr/dialogPreferredPadding">
<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@null"
android:clickable="false"
android:focusable="false" />
</FrameLayout>