From b864eba08f60a8853e616248065feff85ba05dae Mon Sep 17 00:00:00 2001 From: tarsin Date: Wed, 12 Apr 2023 14:20:15 +0800 Subject: [PATCH] Suspendize removeFromFavorites --- .../com/hippo/ehviewer/client/EhClient.kt | 8 --- .../com/hippo/ehviewer/ui/CommonOperations.kt | 48 +++---------- .../ehviewer/ui/scene/GalleryDetailScene.kt | 72 ++++++------------- .../ehviewer/ui/scene/GalleryListScene.kt | 27 +++---- .../hippo/ehviewer/ui/scene/HistoryScene.kt | 28 +++----- 5 files changed, 48 insertions(+), 135 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt b/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt index 8424bebff..c214eeb99 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt @@ -83,13 +83,6 @@ object EhClient { params[0] as String, ) - METHOD_ADD_FAVORITES -> EhEngine.addFavorites( - params[0] as Long, - params[1] as String?, - params[2] as Int, - params[3] as String?, - ) - METHOD_ADD_FAVORITES_RANGE -> @Suppress("UNCHECKED_CAST") EhEngine.addFavoritesRange( @@ -166,7 +159,6 @@ object EhClient { const val METHOD_GET_COMMENT_GALLERY = 6 const val METHOD_GET_GALLERY_TOKEN = 7 const val METHOD_GET_FAVORITES = 8 - const val METHOD_ADD_FAVORITES = 9 const val METHOD_ADD_FAVORITES_RANGE = 10 const val METHOD_MODIFY_FAVORITES = 11 const val METHOD_GET_TORRENT_LIST = 12 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 7b70f5aef..2bb7bac1e 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.kt @@ -16,7 +16,6 @@ package com.hippo.ehviewer.ui import android.Manifest -import android.app.Activity import android.content.Context import android.content.Intent import android.os.Build @@ -27,9 +26,7 @@ import com.hippo.ehviewer.EhApplication.Companion.favouriteStatusRouter import com.hippo.ehviewer.EhDB import com.hippo.ehviewer.R import com.hippo.ehviewer.Settings -import com.hippo.ehviewer.client.EhClient import com.hippo.ehviewer.client.EhEngine -import com.hippo.ehviewer.client.EhRequest import com.hippo.ehviewer.client.data.GalleryInfo import com.hippo.ehviewer.client.exception.EhException import com.hippo.ehviewer.download.DownloadService @@ -46,19 +43,6 @@ import kotlin.coroutines.resume import com.hippo.ehviewer.download.DownloadManager as downloadManager object CommonOperations { - fun removeFromFavorites( - activity: Activity?, - galleryInfo: GalleryInfo, - listener: EhClient.Callback, - ) { - EhDB.removeLocalFavorites(galleryInfo.gid) - val request = EhRequest() - request.setMethod(EhClient.METHOD_ADD_FAVORITES) - request.setArgs(galleryInfo.gid, galleryInfo.token, -1, "") - request.setCallback(DelegateFavoriteCallback(listener, galleryInfo, null, -2)) - request.enqueue(activity!!) - } - fun startDownload(activity: MainActivity?, galleryInfo: GalleryInfo, forceDefault: Boolean) { startDownload(activity!!, listOf(galleryInfo), forceDefault) } @@ -168,28 +152,6 @@ object CommonOperations { .show() } } - - private class DelegateFavoriteCallback( - private val delegate: EhClient.Callback, - private val info: GalleryInfo, - private val newFavoriteName: String?, - private val slot: Int, - ) : EhClient.Callback { - override fun onSuccess(result: Unit) { - info.favoriteName = newFavoriteName - info.favoriteSlot = slot - delegate.onSuccess(result) - favouriteStatusRouter.modifyFavourites(info.gid, slot) - } - - override fun onFailure(e: Exception) { - delegate.onFailure(e) - } - - override fun onCancel() { - delegate.onCancel() - } - } } private fun removeNoMediaFile(downloadDir: UniFile) { @@ -262,3 +224,13 @@ suspend fun Context.addToFavorites(galleryInfo: GalleryInfo, select: Boolean = f } } } + +suspend fun removeFromFavorites(galleryInfo: GalleryInfo) { + EhDB.removeLocalFavorites(galleryInfo.gid) + EhEngine.addFavorites(galleryInfo.gid, galleryInfo.token, -1, "") + galleryInfo.favoriteName = null + galleryInfo.favoriteSlot = -2 + withUIContext { + favouriteStatusRouter.modifyFavourites(galleryInfo.gid, -2) + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index 2444dfc00..336550617 100755 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -157,6 +157,7 @@ import com.hippo.ehviewer.ui.CommonOperations import com.hippo.ehviewer.ui.GalleryInfoBottomSheet import com.hippo.ehviewer.ui.MainActivity import com.hippo.ehviewer.ui.addToFavorites +import com.hippo.ehviewer.ui.removeFromFavorites import com.hippo.ehviewer.ui.scene.GalleryListScene.Companion.toStartArgs import com.hippo.ehviewer.ui.widget.CrystalCard import com.hippo.ehviewer.ui.widget.EhAsyncPreview @@ -969,15 +970,17 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener { lifecycleScope.launchIO { if (!mModifyingFavorites) { var remove = false - val containLocalFavorites = - EhDB.containLocalFavorites(galleryDetail.gid) + val containLocalFavorites = EhDB.containLocalFavorites(galleryDetail.gid) if (containLocalFavorites || galleryDetail.isFavorited) { mModifyingFavorites = true - CommonOperations.removeFromFavorites( - activity, - galleryDetail, - ModifyFavoritesListener(requireContext(), true), - ) + runCatching { + removeFromFavorites(galleryDetail) + showTip(R.string.remove_from_favorite_success, LENGTH_SHORT) + onModifyFavoritesSuccess(true) + }.onFailure { + if (it !is CancellationException) showTip(R.string.remove_from_favorite_failure, LENGTH_LONG) + } + mModifyingFavorites = false remove = true } if (!remove) { @@ -990,8 +993,8 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener { } }.onFailure { if (it !is CancellationException) showTip(R.string.add_to_favorite_failure, LENGTH_LONG) - mModifyingFavorites = false } + mModifyingFavorites = false } withUIContext { // Update UI @@ -1442,11 +1445,14 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener { var remove = false if (EhDB.containLocalFavorites(composeBindingGD!!.gid) || composeBindingGD!!.isFavorited) { mModifyingFavorites = true - CommonOperations.removeFromFavorites( - activity, - composeBindingGD!!, - ModifyFavoritesListener(requireActivity(), true), - ) + runCatching { + removeFromFavorites(composeBindingGD!!) + showTip(R.string.remove_from_favorite_success, LENGTH_SHORT) + onModifyFavoritesSuccess(true) + }.onFailure { + if (it !is CancellationException) showTip(R.string.remove_from_favorite_failure, LENGTH_LONG) + } + mModifyingFavorites = false remove = true } if (!remove) { @@ -1459,8 +1465,8 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener { } }.onFailure { if (it !is CancellationException) showTip(R.string.add_to_favorite_failure, LENGTH_LONG) - mModifyingFavorites = false } + mModifyingFavorites = false } withUIContext { // Update UI @@ -1533,21 +1539,12 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener { } private fun onModifyFavoritesSuccess(addOrRemove: Boolean) { - mModifyingFavorites = false if (composeBindingGD != null) { composeBindingGD!!.isFavorited = !addOrRemove && composeBindingGD!!.favoriteName != null updateFavoriteDrawable() } } - private fun onModifyFavoritesFailure() { - mModifyingFavorites = false - } - - private fun onModifyFavoritesCancel() { - mModifyingFavorites = false - } - private class VoteTagListener(context: Context) : EhCallback(context) { override fun onSuccess(result: String) { @@ -1645,35 +1642,6 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener { override fun onCancel() {} } - private inner class ModifyFavoritesListener( - context: Context, - private val mAddOrRemove: Boolean, - ) : - EhCallback(context) { - override fun onSuccess(result: Unit) { - showTip( - if (mAddOrRemove) R.string.remove_from_favorite_success else R.string.add_to_favorite_success, - LENGTH_SHORT, - ) - val scene = this@GalleryDetailScene - scene.onModifyFavoritesSuccess(mAddOrRemove) - } - - override fun onFailure(e: Exception) { - showTip( - if (mAddOrRemove) R.string.remove_from_favorite_failure else R.string.add_to_favorite_failure, - LENGTH_LONG, - ) - val scene = this@GalleryDetailScene - scene.onModifyFavoritesFailure() - } - - override fun onCancel() { - val scene = this@GalleryDetailScene - scene.onModifyFavoritesCancel() - } - } - private inner class ArchiveListDialogHelper : AdapterView.OnItemClickListener, DialogInterface.OnDismissListener, 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 6b55f5e6d..a5bb2985c 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 @@ -94,6 +94,7 @@ import com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener import com.hippo.ehviewer.ui.CommonOperations import com.hippo.ehviewer.ui.addToFavorites import com.hippo.ehviewer.ui.dialog.SelectItemWithIconAdapter +import com.hippo.ehviewer.ui.removeFromFavorites import com.hippo.ehviewer.widget.GalleryInfoContentHelper import com.hippo.ehviewer.widget.SearchLayout import com.hippo.util.getParcelableCompat @@ -794,11 +795,14 @@ class GalleryListScene : } 2 -> if (favourited) { - CommonOperations.removeFromFavorites( - activity, - gi, - RemoveFromFavoriteListener(context), - ) + 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 { @@ -1125,19 +1129,6 @@ class GalleryListScene : override fun onCancel() {} } - private class RemoveFromFavoriteListener(context: Context) : - EhCallback(context) { - override fun onSuccess(result: Unit) { - showTip(R.string.remove_from_favorite_success, LENGTH_SHORT) - } - - override fun onFailure(e: Exception) { - showTip(R.string.remove_from_favorite_failure, LENGTH_LONG) - } - - override fun onCancel() {} - } - private class QsDrawerHolder(val binding: ItemDrawerListBinding) : RecyclerView.ViewHolder(binding.root) 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 e08020c26..b9adaa7a1 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 @@ -16,7 +16,6 @@ package com.hippo.ehviewer.ui.scene import android.annotation.SuppressLint -import android.content.Context import android.content.DialogInterface import android.content.Intent import android.os.Bundle @@ -58,6 +57,7 @@ import com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener import com.hippo.ehviewer.ui.CommonOperations import com.hippo.ehviewer.ui.addToFavorites import com.hippo.ehviewer.ui.dialog.SelectItemWithIconAdapter +import com.hippo.ehviewer.ui.removeFromFavorites import com.hippo.ehviewer.ui.widget.ListInfoCard import com.hippo.view.ViewTransition import com.hippo.widget.recyclerview.AutoStaggeredGridLayoutManager @@ -304,11 +304,14 @@ class HistoryScene : BaseToolbarScene() { } 2 -> if (favourite) { - CommonOperations.removeFromFavorites( - activity, - gi, - RemoveFromFavoriteListener(context), - ) + 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 { @@ -341,19 +344,6 @@ class HistoryScene : BaseToolbarScene() { }.show() } - private class RemoveFromFavoriteListener(context: Context) : - EhCallback(context) { - override fun onSuccess(result: Unit) { - showTip(R.string.remove_from_favorite_success, LENGTH_SHORT) - } - - override fun onFailure(e: Exception) { - showTip(R.string.remove_from_favorite_failure, LENGTH_LONG) - } - - override fun onCancel() {} - } - private class ComposeHolder(val composeView: ComposeView) : RecyclerView.ViewHolder(composeView) private inner class MoveDialogHelper(