From c20bc215a840d9f4636fb645b8ef5c49f2041a4c Mon Sep 17 00:00:00 2001 From: tarsin Date: Sat, 22 Apr 2023 15:29:25 +0800 Subject: [PATCH] Kill GalleryCommentsScene RefreshCommentListener --- .../com/hippo/ehviewer/client/EhClient.kt | 5 -- .../ehviewer/ui/scene/GalleryCommentsScene.kt | 53 +++++++------------ 2 files changed, 18 insertions(+), 40 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 f2e454023..a894f088b 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt @@ -46,10 +46,6 @@ object EhClient { suspend fun execute(method: Int, vararg params: Any?): Any? { return when (method) { - METHOD_GET_GALLERY_DETAIL -> EhEngine.getGalleryDetail( - params[0] as String, - ) - METHOD_GET_COMMENT_GALLERY -> EhEngine.commentGallery( params[0] as String?, params[1] as String, @@ -96,7 +92,6 @@ object EhClient { fun onCancel() } - const val METHOD_GET_GALLERY_DETAIL = 3 const val METHOD_GET_COMMENT_GALLERY = 6 const val METHOD_GET_FAVORITES = 8 const val METHOD_ADD_FAVORITES_RANGE = 10 diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.kt index 083ccc45d..7d16cf1d3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.kt @@ -76,7 +76,6 @@ import com.hippo.ehviewer.client.data.GalleryDetail import com.hippo.ehviewer.client.data.ListUrlBuilder import com.hippo.ehviewer.client.parser.VoteCommentParser import com.hippo.ehviewer.dao.Filter -import com.hippo.ehviewer.ui.MainActivity import com.hippo.ehviewer.ui.scene.GalleryListScene.Companion.toStartArgs import com.hippo.text.URLImageGetter import com.hippo.util.ExceptionUtils @@ -493,19 +492,27 @@ class GalleryCommentsScene : BaseToolbarScene(), View.OnClickListener, OnRefresh mRefreshingComments = true mShowAllComments = true mAdapter!!.notifyItemChanged(position) - val url = galleryDetailUrl - if (url != null) { - // Request - val request = EhRequest() - .setMethod(EhClient.METHOD_GET_GALLERY_DETAIL) - .setArgs(url) - .setCallback(RefreshCommentListener(activity)) - request.enqueue(this) - } + galleryDetailUrl?.let { refreshComment(it) } } return true } + private fun refreshComment(url: String) { + lifecycleScope.launchIO { + runSuspendCatching { + EhEngine.getGalleryDetail(url) + }.onSuccess { + withUIContext { + onRefreshGallerySuccess(it.comments) + } + }.onFailure { + withUIContext { + onRefreshGalleryFailure() + } + } + } + } + private fun updateView(animation: Boolean) { if (null == mViewTransition) { return @@ -764,17 +771,8 @@ class GalleryCommentsScene : BaseToolbarScene(), View.OnClickListener, OnRefresh override fun onRefresh() { if (!mRefreshingComments && mAdapter != null) { - val activity = requireActivity() as MainActivity mRefreshingComments = true - val url = galleryDetailUrl - if (url != null) { - // Request - val request = EhRequest() - .setMethod(EhClient.METHOD_GET_GALLERY_DETAIL) - .setArgs(url) - .setCallback(RefreshCommentListener(activity)) - request.enqueue(this) - } + galleryDetailUrl?.let { refreshComment(it) } } } @@ -799,21 +797,6 @@ class GalleryCommentsScene : BaseToolbarScene(), View.OnClickListener, OnRefresh private class ProgressCommentHolder(inflater: LayoutInflater, parent: ViewGroup?) : CommentHolder(inflater, R.layout.item_gallery_comment_progress, parent) - private inner class RefreshCommentListener(context: Context) : - EhCallback(context) { - override fun onSuccess(result: GalleryDetail) { - val scene = this@GalleryCommentsScene - scene.onRefreshGallerySuccess(result.comments) - } - - override fun onFailure(e: Exception) { - val scene = this@GalleryCommentsScene - scene.onRefreshGalleryFailure() - } - - override fun onCancel() {} - } - private inner class CommentGalleryListener(context: Context, private val mCommentId: Long) : EhCallback(context) { override fun onSuccess(result: GalleryCommentList) {