From 13705f98726e8f6ab87e0a9e698d8cf818fa8b2c Mon Sep 17 00:00:00 2001 From: tarsin Date: Sat, 22 Apr 2023 15:20:40 +0800 Subject: [PATCH] Kill ProgressScene GetGalleryTokenListener --- .../com/hippo/ehviewer/client/EhClient.kt | 7 -- .../hippo/ehviewer/ui/scene/ProgressScene.kt | 79 +++++++------------ 2 files changed, 30 insertions(+), 56 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 28f0e2507..f2e454023 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt @@ -56,12 +56,6 @@ object EhClient { params[2] as String?, ) - METHOD_GET_GALLERY_TOKEN -> EhEngine.getGalleryToken( - params[0] as Long, - params[1] as String?, - params[2] as Int, - ) - METHOD_GET_FAVORITES -> EhEngine.getFavorites( params[0] as String, ) @@ -104,7 +98,6 @@ object EhClient { const val METHOD_GET_GALLERY_DETAIL = 3 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_RANGE = 10 const val METHOD_MODIFY_FAVORITES = 11 diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt index b79641091..b8e24c729 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt @@ -15,20 +15,22 @@ */ package com.hippo.ehviewer.ui.scene -import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import com.hippo.ehviewer.R -import com.hippo.ehviewer.client.EhClient -import com.hippo.ehviewer.client.EhRequest +import com.hippo.ehviewer.client.EhEngine import com.hippo.util.ExceptionUtils import com.hippo.view.ViewTransition import com.hippo.yorozuya.ViewUtils +import eu.kanade.tachiyomi.util.lang.launchIO +import eu.kanade.tachiyomi.util.lang.withUIContext +import moe.tarsin.coroutines.runSuspendCatching /** * Only show a progress with jobs in background @@ -65,15 +67,31 @@ class ProgressScene : BaseScene(), View.OnClickListener { if (mGid == -1L || mPToken == null || mPage == -1) { return false } - val request = EhRequest() - .setMethod(EhClient.METHOD_GET_GALLERY_TOKEN) - .setArgs(mGid, mPToken!!, mPage) - .setCallback( - GetGalleryTokenListener( - context, - ), - ) - request.enqueue() + lifecycleScope.launchIO { + runSuspendCatching { + EhEngine.getGalleryToken(mGid, mPToken, mPage) + }.onSuccess { + val arg = Bundle() + arg.putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GID_TOKEN) + arg.putLong(GalleryDetailScene.KEY_GID, mGid) + arg.putString(GalleryDetailScene.KEY_TOKEN, it) + arg.putInt(GalleryDetailScene.KEY_PAGE, mPage) + withUIContext { + findNavController().popBackStack() + navigate(R.id.galleryDetailScene, arg) + } + }.onFailure { + mValid = false + if (null != mViewTransition && null != mTip) { + // Show tip + mError = ExceptionUtils.getReadableString(it) + withUIContext { + mViewTransition!!.showView(1) + mTip!!.text = mError + } + } + } + } return true } return false @@ -162,43 +180,6 @@ class ProgressScene : BaseScene(), View.OnClickListener { } } - private fun onGetGalleryTokenSuccess(result: String) { - val arg = Bundle() - arg.putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GID_TOKEN) - arg.putLong(GalleryDetailScene.KEY_GID, mGid) - arg.putString(GalleryDetailScene.KEY_TOKEN, result) - arg.putInt(GalleryDetailScene.KEY_PAGE, mPage) - findNavController().popBackStack() - navigate(R.id.galleryDetailScene, arg) - } - - private fun onGetGalleryTokenFailure(e: Exception) { - mValid = false - val context = context - if (null != context && null != mViewTransition && null != mTip) { - // Show tip - mError = ExceptionUtils.getReadableString(e) - mViewTransition!!.showView(1) - mTip!!.text = mError - } - } - - private inner class GetGalleryTokenListener( - context: Context, - ) : EhCallback(context) { - override fun onSuccess(result: String) { - val scene = this@ProgressScene - scene.onGetGalleryTokenSuccess(result) - } - - override fun onFailure(e: Exception) { - val scene = this@ProgressScene - scene.onGetGalleryTokenFailure(e) - } - - override fun onCancel() {} - } - companion object { const val KEY_ACTION = "action" const val ACTION_GALLERY_TOKEN = "gallery_token"