From eb5f39def7e6a5e5d11e10cd93ee8e8ed10e6d78 Mon Sep 17 00:00:00 2001 From: tarsin Date: Sat, 22 Apr 2023 15:52:32 +0800 Subject: [PATCH] Kill EhCallback --- .../com/hippo/ehviewer/client/EhClient.kt | 7 -- .../com/hippo/ehviewer/ui/scene/EhCallback.kt | 67 ------------- .../hippo/ehviewer/ui/scene/FavoritesScene.kt | 99 ++++++++----------- 3 files changed, 41 insertions(+), 132 deletions(-) delete mode 100644 app/src/main/java/com/hippo/ehviewer/ui/scene/EhCallback.kt 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 37a85f0ed..c0c5683ad 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.kt @@ -50,12 +50,6 @@ object EhClient { params[0] as String, ) - METHOD_MODIFY_FAVORITES -> EhEngine.modifyFavorites( - params[0] as String, - params[1] as LongArray, - params[2] as Int, - ) - METHOD_GET_TORRENT_LIST -> EhEngine.getTorrentList( params[0] as String, params[1] as Long, @@ -79,7 +73,6 @@ object EhClient { } const val METHOD_GET_FAVORITES = 8 - const val METHOD_MODIFY_FAVORITES = 11 const val METHOD_GET_TORRENT_LIST = 12 const val METHOD_ARCHIVE_LIST = 17 } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/EhCallback.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/EhCallback.kt deleted file mode 100644 index e6c10f4d8..000000000 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/EhCallback.kt +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.hippo.ehviewer.ui.scene - -import android.content.Context -import android.widget.Toast -import androidx.annotation.StringRes -import androidx.fragment.app.Fragment -import com.hippo.ehviewer.EhApplication -import com.hippo.ehviewer.client.EhClient -import com.hippo.ehviewer.ui.MainActivity - -abstract class EhCallback(context: Context) : EhClient.Callback { - val application: EhApplication - - init { - application = context.applicationContext as EhApplication - } - - val content: Context - get() { - var context: Context? = application.topActivity - if (context == null) { - context = application - } - return context - } - - fun showTip(@StringRes id: Int, length: Int) { - val activity = application.topActivity - if (activity is MainActivity) { - activity.showTip(id, length) - } else { - Toast.makeText( - application, - id, - if (length == BaseScene.LENGTH_LONG) Toast.LENGTH_LONG else Toast.LENGTH_SHORT, - ).show() - } - } - - fun showTip(tip: String, length: Int) { - val activity = application.topActivity - if (activity is MainActivity) { - activity.showTip(tip, length) - } else { - Toast.makeText( - application, - tip, - if (length == BaseScene.LENGTH_LONG) Toast.LENGTH_LONG else Toast.LENGTH_SHORT, - ).show() - } - } -} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt index 16833d4b5..ddd65276f 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt @@ -17,7 +17,6 @@ package com.hippo.ehviewer.ui.scene import android.annotation.SuppressLint import android.app.Activity -import android.content.Context import android.content.DialogInterface import android.content.res.Resources import android.os.Bundle @@ -53,7 +52,6 @@ import com.hippo.ehviewer.Settings import com.hippo.ehviewer.WindowInsetsAnimationHelper import com.hippo.ehviewer.client.EhClient import com.hippo.ehviewer.client.EhEngine -import com.hippo.ehviewer.client.EhRequest import com.hippo.ehviewer.client.EhUrl import com.hippo.ehviewer.client.data.FavListUrlBuilder import com.hippo.ehviewer.client.data.GalleryInfo @@ -656,7 +654,7 @@ class FavoritesScene : } } - private fun onGetFavoritesFailure(e: Exception, taskId: Int) { + private fun onGetFavoritesFailure(e: Throwable, taskId: Int) { if (mHelper != null && mHelper!!.isCurrentTask(taskId)) { mHelper!!.onGetException(taskId, e) } @@ -694,37 +692,6 @@ class FavoritesScene : } } - private inner class GetFavoritesListener( - context: Context, - private val mTaskId: Int, // Local fav is shown now, but operation need be done for cloud fav - private val mLocal: Boolean, - private val mKeyword: String?, - ) : EhCallback(context) { - override fun onSuccess(result: FavoritesParser.Result) { - // Put fav cat - Settings.putFavCat(result.catArray) - Settings.putFavCount(result.countArray) - val scene = this@FavoritesScene - if (mLocal) { - scene.onGetFavoritesLocal(mKeyword, mTaskId) - } else { - scene.onGetFavoritesSuccess(result, mTaskId) - } - } - - override fun onFailure(e: Exception) { - val scene = this@FavoritesScene - if (mLocal) { - e.printStackTrace() - scene.onGetFavoritesLocal(mKeyword, mTaskId) - } else { - scene.onGetFavoritesFailure(e, mTaskId) - } - } - - override fun onCancel() {} - } - private inner class FavDrawerAdapter(private val mInflater: LayoutInflater) : RecyclerView.Adapter() { override fun getItemViewType(position: Int): Int { @@ -943,18 +910,31 @@ class FavoritesScene : mUrlBuilder!!.build() } mUrlBuilder!!.setIndex(index, true) - val request = EhRequest() - request.setMethod(EhClient.METHOD_MODIFY_FAVORITES) - request.setCallback( - GetFavoritesListener( - context, - taskId, - local, - mUrlBuilder!!.keyword, - ), - ) - request.setArgs(url, gidArray, mModifyFavCat) - request.enqueue(this@FavoritesScene) + lifecycleScope.launchIO { + runSuspendCatching { + EhEngine.modifyFavorites(url, gidArray, mModifyFavCat) + }.onSuccess { result -> + // Put fav cat + Settings.putFavCat(result.catArray) + Settings.putFavCount(result.countArray) + withUIContext { + if (local) { + onGetFavoritesLocal(mUrlBuilder?.keyword, taskId) + } else { + onGetFavoritesSuccess(result, taskId) + } + } + }.onFailure { + it.printStackTrace() + withUIContext { + if (local) { + onGetFavoritesLocal(mUrlBuilder?.keyword, taskId) + } else { + onGetFavoritesFailure(it, taskId) + } + } + } + } } } else if (mUrlBuilder!!.favCat == FavListUrlBuilder.FAV_CAT_LOCAL) { val keyword = mUrlBuilder!!.keyword @@ -963,18 +943,21 @@ class FavoritesScene : mUrlBuilder!!.setIndex(index, isNext) mUrlBuilder!!.jumpTo = jumpTo val url = mUrlBuilder!!.build() - val request = EhRequest() - request.setMethod(EhClient.METHOD_GET_FAVORITES) - request.setCallback( - GetFavoritesListener( - context, - taskId, - false, - mUrlBuilder!!.keyword, - ), - ) - request.setArgs(url) - request.enqueue(this@FavoritesScene) + lifecycleScope.launchIO { + runSuspendCatching { + EhEngine.getFavorites(url) + }.onSuccess { result -> + Settings.putFavCat(result.catArray) + Settings.putFavCount(result.countArray) + withUIContext { + onGetFavoritesSuccess(result, taskId) + } + }.onFailure { + withUIContext { + onGetFavoritesFailure(it, taskId) + } + } + } } }