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 d0c6c91f3..14d08891b 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.kt @@ -54,6 +54,8 @@ import eu.kanade.tachiyomi.util.lang.withUIContext import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock +import moe.tarsin.coroutines.runSuspendCatching +import rikka.core.util.ContextUtils.requireActivity import kotlin.coroutines.resume object CommonOperations { @@ -256,19 +258,18 @@ fun Context.navToReader(info: GalleryInfo, page: Int = -1) { startActivity(intent) } -suspend fun DialogState.selectGalleryInfoAction(info: GalleryInfo): Int { +suspend fun DialogState.doGalleryInfoAction(info: GalleryInfo, context: Context) { val downloaded = DownloadManager.getDownloadState(info.gid) != DownloadInfo.STATE_INVALID val favourite = info.favoriteSlot != -2 - val selected: Int - if (!downloaded) { - selected = showSelectItemWithIcon( + val selected = if (!downloaded) { + showSelectItemWithIcon( Icons.Default.MenuBook to R.string.read, Icons.Default.Download to R.string.download, if (!favourite) Icons.Default.Favorite to R.string.add_to_favourites else Icons.Default.HeartBroken to R.string.remove_from_favourites, title = EhUtils.getSuitableTitle(info), ) } else { - selected = showSelectItemWithIcon( + showSelectItemWithIcon( Icons.Default.MenuBook to R.string.read, Icons.Default.Delete to R.string.delete_downloads, if (!favourite) Icons.Default.Favorite to R.string.add_to_favourites else Icons.Default.HeartBroken to R.string.remove_from_favourites, @@ -276,7 +277,33 @@ suspend fun DialogState.selectGalleryInfoAction(info: GalleryInfo): Int { title = EhUtils.getSuitableTitle(info), ) } - return selected + with(requireActivity(context)) { + when (selected) { + 0 -> navToReader(info) + 1 -> if (downloaded) { + if (confirmRemoveDownload(info)) DownloadManager.deleteDownload(info.gid) + } else { + CommonOperations.startDownload(this, info, false) + } + 2 -> if (favourite) { + runSuspendCatching { + removeFromFavorites(info) + showTip(R.string.remove_from_favorite_success, BaseScene.LENGTH_SHORT) + }.onFailure { + showTip(R.string.remove_from_favorite_failure, BaseScene.LENGTH_LONG) + } + } else { + runSuspendCatching { + addToFavorites(info) + showTip(R.string.add_to_favorite_success, BaseScene.LENGTH_SHORT) + }.onFailure { + showTip(R.string.add_to_favorite_failure, BaseScene.LENGTH_LONG) + } + } + 3 -> showMoveDownloadLabel(info) + } + true + } } suspend fun DialogState.confirmRemoveDownload(info: GalleryInfo): Boolean = show( 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 13cbccfff..b84697108 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 @@ -79,12 +79,9 @@ import com.hippo.ehviewer.databinding.ItemDrawerListBinding import com.hippo.ehviewer.databinding.SceneGalleryListBinding import com.hippo.ehviewer.download.DownloadManager import com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener -import com.hippo.ehviewer.ui.CommonOperations -import com.hippo.ehviewer.ui.MainActivity -import com.hippo.ehviewer.ui.addToFavorites import com.hippo.ehviewer.ui.compose.DialogState import com.hippo.ehviewer.ui.compose.setMD3Content -import com.hippo.ehviewer.ui.confirmRemoveDownload +import com.hippo.ehviewer.ui.doGalleryInfoAction import com.hippo.ehviewer.ui.legacy.AddDeleteDrawable import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder import com.hippo.ehviewer.ui.legacy.BringOutTransition @@ -98,10 +95,6 @@ import com.hippo.ehviewer.ui.legacy.ViewTransition import com.hippo.ehviewer.ui.legacy.WindowInsetsAnimationHelper import com.hippo.ehviewer.ui.legacy.easyrecyclerview.EasyRecyclerView import com.hippo.ehviewer.ui.legacy.easyrecyclerview.FastScroller.OnDragHandlerListener -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 @@ -722,39 +715,7 @@ class GalleryListScene : SearchBarScene(), OnDragHandlerListener, SearchLayout.H val context = context ?: return true val info = mHelper?.getDataAtEx(position) ?: return true lifecycleScope.launchIO { - val downloaded = DownloadManager.getDownloadState(info.gid) != DownloadInfo.STATE_INVALID - val favourite = info.favoriteSlot != -2 - val selected = dialogState.selectGalleryInfoAction(info) - withUIContext { - when (selected) { - 0 -> context.navToReader(info) - 1 -> if (downloaded) { - if (dialogState.confirmRemoveDownload(info)) mDownloadManager.deleteDownload(info.gid) - } else { - CommonOperations.startDownload(activity as MainActivity, info, false) - } - 2 -> if (favourite) { - lifecycleScope.launchIO { - runSuspendCatching { - removeFromFavorites(info) - showTip(R.string.remove_from_favorite_success, LENGTH_SHORT) - }.onFailure { - showTip(R.string.remove_from_favorite_failure, LENGTH_LONG) - } - } - } else { - lifecycleScope.launchIO { - runSuspendCatching { - context.addToFavorites(info) - showTip(R.string.add_to_favorite_success, LENGTH_SHORT) - }.onFailure { - showTip(R.string.add_to_favorite_failure, LENGTH_LONG) - } - } - } - 3 -> dialogState.showMoveDownloadLabel(info) - } - } + dialogState.doGalleryInfoAction(info, context) } return true } 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 74748dbc4..f8036830d 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 @@ -65,26 +65,14 @@ import com.hippo.ehviewer.EhDB import com.hippo.ehviewer.R import com.hippo.ehviewer.Settings import com.hippo.ehviewer.client.data.GalleryInfo -import com.hippo.ehviewer.dao.DownloadInfo -import com.hippo.ehviewer.download.DownloadManager -import com.hippo.ehviewer.ui.CommonOperations -import com.hippo.ehviewer.ui.MainActivity -import com.hippo.ehviewer.ui.addToFavorites import com.hippo.ehviewer.ui.compose.Deferred -import com.hippo.ehviewer.ui.compose.DialogState 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.navToReader -import com.hippo.ehviewer.ui.removeFromFavorites -import com.hippo.ehviewer.ui.selectGalleryInfoAction -import com.hippo.ehviewer.ui.showMoveDownloadLabel +import com.hippo.ehviewer.ui.doGalleryInfoAction import eu.kanade.tachiyomi.util.lang.launchIO -import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.system.pxToDp import kotlinx.coroutines.delay -import moe.tarsin.coroutines.runSuspendCatching import my.nanihadesuka.compose.InternalLazyColumnScrollbar class HistoryScene : BaseScene() { @@ -162,8 +150,7 @@ class HistoryScene : BaseScene() { onClick = ::onItemClick.partially1(it), onLongClick = { coroutineScope.launchIO { - val selected = dialogState.selectGalleryInfoAction(info) - withUIContext { handleLongClick(selected, info, dialogState) } + dialogState.doGalleryInfoAction(info, context) } }, info = it, @@ -218,38 +205,5 @@ class HistoryScene : BaseScene() { navigate(R.id.galleryDetailScene, args) } - private suspend fun handleLongClick(which: Int, gi: GalleryInfo, dialogState: DialogState) { - val downloaded = DownloadManager.getDownloadState(gi.gid) != DownloadInfo.STATE_INVALID - val favourite = gi.favoriteSlot != -2 - when (which) { - 0 -> context?.navToReader(gi) - 1 -> if (downloaded) { - if (dialogState.confirmRemoveDownload(gi)) DownloadManager.deleteDownload(gi.gid) - } else { - CommonOperations.startDownload(activity as MainActivity, gi, false) - } - 2 -> if (favourite) { - lifecycleScope.launchIO { - runSuspendCatching { - removeFromFavorites(gi) - showTip(R.string.remove_from_favorite_success, LENGTH_SHORT) - }.onFailure { - showTip(R.string.remove_from_favorite_failure, LENGTH_LONG) - } - } - } else { - lifecycleScope.launchIO { - runSuspendCatching { - requireContext().addToFavorites(gi) - showTip(R.string.add_to_favorite_success, LENGTH_SHORT) - }.onFailure { - showTip(R.string.add_to_favorite_failure, LENGTH_LONG) - } - } - } - 3 -> dialogState.showMoveDownloadLabel(gi) - } - } - private val cardHeight = (Settings.listThumbSize * 3).pxToDp.dp }