diff --git a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.kt b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.kt index 7b8e0e770..d0c6c91f3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.kt @@ -32,6 +32,7 @@ import androidx.compose.ui.res.stringResource import androidx.core.content.ContextCompat import com.hippo.ehviewer.EhApplication.Companion.favouriteStatusRouter import com.hippo.ehviewer.EhDB +import com.hippo.ehviewer.GetText import com.hippo.ehviewer.R import com.hippo.ehviewer.Settings import com.hippo.ehviewer.client.EhEngine @@ -54,7 +55,6 @@ import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlin.coroutines.resume -import com.hippo.ehviewer.download.DownloadManager as downloadManager object CommonOperations { fun startDownload(activity: MainActivity?, galleryInfo: GalleryInfo, forceDefault: Boolean) { @@ -79,11 +79,10 @@ object CommonOperations { galleryInfos: List, forceDefault: Boolean, ) { - val dm = downloadManager val toStart = LongList() val toAdd: MutableList = ArrayList() for (gi in galleryInfos) { - if (dm.containDownloadInfo(gi.gid)) { + if (DownloadManager.containDownloadInfo(gi.gid)) { toStart.add(gi.gid) } else { toAdd.add(gi) @@ -104,10 +103,10 @@ object CommonOperations { // Get default download label if (!justStart && Settings.hasDefaultDownloadLabel) { label = Settings.defaultDownloadLabel - justStart = label == null || dm.containLabel(label) + justStart = label == null || DownloadManager.containLabel(label) } // If there is no other label, just use null label - if (!justStart && dm.labelList.isEmpty()) { + if (!justStart && DownloadManager.labelList.isEmpty()) { justStart = true label = null } @@ -124,7 +123,7 @@ object CommonOperations { activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT) } else { // Let use chose label - val list = dm.labelList + val list = DownloadManager.labelList val items = mutableListOf() items.add(activity.getString(R.string.default_download_label_name)) items.addAll(list.mapNotNull { it.label }) @@ -137,7 +136,7 @@ object CommonOperations { label1 = null } else { label1 = items[position] - if (!dm.containLabel(label1)) { + if (!DownloadManager.containLabel(label1)) { label1 = null } } @@ -286,3 +285,12 @@ suspend fun DialogState.confirmRemoveDownload(info: GalleryInfo): Boolean = show title = R.string.download_remove_dialog_title, text = { Text(text = stringResource(id = R.string.download_remove_dialog_message, info.title.orEmpty())) }, ) + +suspend fun DialogState.showMoveDownloadLabel(info: GalleryInfo) { + val defaultLabel = GetText.getString(R.string.default_download_label_name) + val labels = DownloadManager.labelList.mapNotNull { it.label }.toTypedArray() + val selected = showSelectItem(defaultLabel, *labels, title = R.string.download_move_dialog_title) + val downloadInfo = DownloadManager.getDownloadInfo(info.gid) ?: return + val label = if (selected == 0) null else labels[selected - 1] + withUIContext { DownloadManager.changeLabel(listOf(downloadInfo), label) } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/compose/Dialog.kt b/app/src/main/java/com/hippo/ehviewer/ui/compose/Dialog.kt index bdf114056..5efc921d4 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/compose/Dialog.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/compose/Dialog.kt @@ -12,11 +12,13 @@ import androidx.compose.foundation.layout.width import androidx.compose.material3.AlertDialog import androidx.compose.material3.AlertDialogDefaults import androidx.compose.material3.Icon +import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -141,6 +143,21 @@ class DialogState { } } + suspend fun showSelectItem( + vararg items: String, + @StringRes title: Int, + ): Int = showNoButton { + Column(modifier = Modifier.padding(24.dp)) { + Text(text = stringResource(id = title), style = MaterialTheme.typography.headlineSmall) + Spacer(modifier = Modifier.size(16.dp)) + CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.tertiary) { + items.forEachIndexed { index, text -> + Text(text = text, modifier = Modifier.clickable { dismissWith(index) }.fillMaxWidth().padding(vertical = 8.dp), style = MaterialTheme.typography.titleMedium) + } + } + } + } + suspend fun showSelectItemWithIcon( vararg items: Pair, title: String, diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt index 9a8aad338..13cbccfff 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt @@ -101,6 +101,7 @@ import com.hippo.ehviewer.ui.legacy.easyrecyclerview.FastScroller.OnDragHandlerL import com.hippo.ehviewer.ui.navToReader import com.hippo.ehviewer.ui.removeFromFavorites import com.hippo.ehviewer.ui.selectGalleryInfoAction +import com.hippo.ehviewer.ui.showMoveDownloadLabel import com.hippo.ehviewer.util.getParcelableCompat import com.hippo.ehviewer.yorozuya.AnimationUtils import com.hippo.ehviewer.yorozuya.SimpleAnimatorListener @@ -732,7 +733,6 @@ class GalleryListScene : SearchBarScene(), OnDragHandlerListener, SearchLayout.H } else { CommonOperations.startDownload(activity as MainActivity, info, false) } - 2 -> if (favourite) { lifecycleScope.launchIO { runSuspendCatching { @@ -752,24 +752,7 @@ class GalleryListScene : SearchBarScene(), OnDragHandlerListener, SearchLayout.H } } } - - 3 -> { - val labelRawList = downloadManager.labelList - val labelList: MutableList = ArrayList(labelRawList.size + 1) - labelList.add(getString(R.string.default_download_label_name)) - var i = 0 - val n = labelRawList.size - while (i < n) { - labelRawList[i].label?.let { labelList.add(it) } - i++ - } - val labels = labelList.toTypedArray() - val helper = MoveDialogHelper(labels, info) - BaseDialogBuilder(context) - .setTitle(R.string.download_move_dialog_title) - .setItems(labels, helper) - .show() - } + 3 -> dialogState.showMoveDownloadLabel(info) } } } @@ -1036,23 +1019,7 @@ class GalleryListScene : SearchBarScene(), OnDragHandlerListener, SearchLayout.H @Retention(AnnotationRetention.SOURCE) private annotation class State - private class QsDrawerHolder(val binding: ItemDrawerListBinding) : - RecyclerView.ViewHolder(binding.root) - - private inner class MoveDialogHelper( - private val mLabels: Array, - private val mGi: GalleryInfo, - ) : DialogInterface.OnClickListener { - override fun onClick(dialog: DialogInterface, which: Int) { - // Cancel check mode - _binding ?: return - binding.contentLayout.recyclerView.outOfCustomChoiceMode() - val downloadManager = downloadManager - val downloadInfo = downloadManager.getDownloadInfo(mGi.gid) ?: return - val label = if (which == 0) null else mLabels[which] - downloadManager.changeLabel(listOf(downloadInfo), label) - } - } + private class QsDrawerHolder(val binding: ItemDrawerListBinding) : RecyclerView.ViewHolder(binding.root) private inner class QsDrawerAdapter(private val mInflater: LayoutInflater) : RecyclerView.Adapter() { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt index a793ccab1..74748dbc4 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt @@ -15,7 +15,6 @@ */ package com.hippo.ehviewer.ui.scene -import android.content.DialogInterface import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -77,10 +76,10 @@ import com.hippo.ehviewer.ui.compose.data.GalleryInfoListItem import com.hippo.ehviewer.ui.compose.rememberDialogState import com.hippo.ehviewer.ui.compose.setMD3Content import com.hippo.ehviewer.ui.confirmRemoveDownload -import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder import com.hippo.ehviewer.ui.navToReader import com.hippo.ehviewer.ui.removeFromFavorites import com.hippo.ehviewer.ui.selectGalleryInfoAction +import com.hippo.ehviewer.ui.showMoveDownloadLabel import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.system.pxToDp @@ -229,7 +228,6 @@ class HistoryScene : BaseScene() { } else { CommonOperations.startDownload(activity as MainActivity, gi, false) } - 2 -> if (favourite) { lifecycleScope.launchIO { runSuspendCatching { @@ -249,33 +247,7 @@ class HistoryScene : BaseScene() { } } } - - 3 -> { - val labelRawList = DownloadManager.labelList - val labelList: MutableList = ArrayList(labelRawList.size + 1) - labelList.add(getString(R.string.default_download_label_name)) - var i = 0 - val n = labelRawList.size - while (i < n) { - labelRawList[i].label?.let { labelList.add(it) } - i++ - } - val labels = labelList.toTypedArray() - val helper = MoveDialogHelper(labels, gi) - BaseDialogBuilder(requireContext()).setTitle(R.string.download_move_dialog_title).setItems(labels, helper).show() - } - } - } - - private inner class MoveDialogHelper( - private val mLabels: Array, - private val mGi: GalleryInfo, - ) : DialogInterface.OnClickListener { - override fun onClick(dialog: DialogInterface, which: Int) { - val downloadManager = DownloadManager - val downloadInfo = downloadManager.getDownloadInfo(mGi.gid) ?: return - val label = if (which == 0) null else mLabels[which] - downloadManager.changeLabel(listOf(downloadInfo), label) + 3 -> dialogState.showMoveDownloadLabel(gi) } }