Kill EhClient and EhRequest

This commit is contained in:
tarsin committed 2023-04-22 16:49:20 +08:00
1 parent 78d8ed922c
commit ed673b5bc1
3 files changed
+28 -221

No files matched your search

@@ -1,64 +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.client
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.withUIContext
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
object EhClient {
internal fun enqueue(request: EhRequest, scope: CoroutineScope) {
check(!request.isActive) // Abort if attempt to execute an active request
request.job = scope.launchIO {
val callback: Callback<Any?>? = request.callback
try {
val result: Any = request.run {
if (args == null) {
execute(method)
} else {
execute(method, *args!!)
}
}
withUIContext { callback?.onSuccess(result) }
} catch (e: Exception) {
if (e is CancellationException) {
throw e // Don't catch coroutine CancellationException
}
e.printStackTrace()
withUIContext { callback?.onFailure(e) }
}
}
}
suspend fun execute(method: Int, vararg params: Any?): Any {
return when (method) {
METHOD_GET_FAVORITES -> EhEngine.getFavorites(
params[0] as String,
)
else -> throw IllegalStateException("Can't detect method $method")
}
}
interface Callback<E> {
fun onSuccess(result: E)
fun onFailure(e: Exception)
fun onCancel()
}
const val METHOD_GET_FAVORITES = 8
}
@@ -1,80 +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.client
import android.app.Activity
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
class EhRequest {
internal var job: Job? = null
val isActive
get() = job?.isActive ?: false
var method = 0
private set
var args: Array<out Any?>? = null
private set
var callback: EhClient.Callback<Any?>? = null
private set
fun setMethod(method: Int): EhRequest {
this.method = method
return this
}
fun setArgs(vararg args: Any?): EhRequest {
this.args = args
return this
}
@Suppress("UNCHECKED_CAST")
fun setCallback(callback: EhClient.Callback<*>?): EhRequest {
this.callback = callback as EhClient.Callback<Any?>?
return this
}
fun enqueue(scope: CoroutineScope) {
EhClient.enqueue(this, scope)
}
@DelicateCoroutinesApi
fun enqueue() {
EhClient.enqueue(this, GlobalScope)
}
fun enqueue(fragment: Fragment) {
enqueue(fragment.viewLifecycleOwner.lifecycleScope)
}
fun enqueue(activity: Activity) {
check(activity is FragmentActivity)
enqueue(activity.lifecycleScope)
}
@MainThread
fun cancel() {
if (isActive) {
job?.cancel()
callback?.onCancel()
}
}
}
@@ -25,7 +25,6 @@ import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import com.hippo.app.BaseDialogBuilder
@@ -34,10 +33,8 @@ import com.hippo.ehviewer.BuildConfig
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.GetText
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.ehviewer.client.data.FavListUrlBuilder
import com.hippo.ehviewer.client.parser.FavoritesParser
import com.hippo.ehviewer.ui.scene.BaseScene
import com.hippo.util.ExceptionUtils
import com.hippo.util.LogCat
@@ -46,8 +43,7 @@ import com.hippo.yorozuya.IOUtils
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.withUIContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import moe.tarsin.coroutines.runSuspendCatching
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File
@@ -55,7 +51,6 @@ import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import kotlin.math.ceil
import com.hippo.ehviewer.Settings as AppSettings
class AdvancedFragment : BasePreferenceFragment() {
@@ -232,8 +227,7 @@ class AdvancedFragment : BasePreferenceFragment() {
}
}
}
private var favTotal = 0
private var favIndex = 0
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.advanced_settings)
val dumpLogcat = findPreference<Preference>(KEY_DUMP_LOGCAT)
@@ -316,79 +310,36 @@ class AdvancedFragment : BasePreferenceFragment() {
}
private fun backupFavorite() {
val mClient = EhClient
val favListUrlBuilder = FavListUrlBuilder()
favTotal = 0
favIndex = 1
val request = EhRequest()
request.setMethod(EhClient.METHOD_GET_FAVORITES)
request.setCallback(object : EhClient.Callback<FavoritesParser.Result> {
override fun onSuccess(result: FavoritesParser.Result) {
try {
if (result.galleryInfoList.isEmpty()) {
showTip(
R.string.settings_advanced_backup_favorite_nothing,
BaseScene.LENGTH_SHORT,
)
} else {
if (favTotal == 0 && result.countArray != null) {
var totalFav = 0
for (i in 0..9) {
totalFav += result.countArray[i]
}
favTotal =
ceil(totalFav.toDouble() / result.galleryInfoList.size).toInt()
}
val status = "($favIndex/$favTotal)"
showTip(
GetText.getString(
R.string.settings_advanced_backup_favorite_start,
status,
),
BaseScene.LENGTH_SHORT,
)
Log.d("LocalFavorites", "now backup page $status")
EhDB.putLocalFavorites(result.galleryInfoList)
if (result.next != null) {
try {
runBlocking {
delay(AppSettings.downloadDelay.toLong())
}
} catch (e: InterruptedException) {
e.printStackTrace()
}
favIndex++
favListUrlBuilder.setIndex(result.next, true)
request.setArgs(favListUrlBuilder.build())
viewLifecycleOwner.lifecycleScope.launch {
delay(100)
request.enqueue(this@AdvancedFragment)
}
} else {
showTip(
R.string.settings_advanced_backup_favorite_success,
BaseScene.LENGTH_SHORT,
)
}
}
} catch (e: Exception) {
showTip(
R.string.settings_advanced_backup_favorite_failed,
BaseScene.LENGTH_SHORT,
)
var favTotal = 0
var favIndex = 0
tailrec suspend fun doBackup() {
val result = EhEngine.getFavorites(favListUrlBuilder.build())
if (result.galleryInfoList.isEmpty()) {
showTip(R.string.settings_advanced_backup_favorite_nothing, BaseScene.LENGTH_SHORT)
} else {
if (favTotal == 0) favTotal = result.countArray.sum()
favIndex += result.galleryInfoList.size
val status = "($favIndex/$favTotal)"
Log.d("LocalFavorites", "now backup page $status")
EhDB.putLocalFavorites(result.galleryInfoList)
showTip(getString(R.string.settings_advanced_backup_favorite_start, status), BaseScene.LENGTH_SHORT)
if (result.next != null) {
delay(AppSettings.downloadDelay.toLong())
favListUrlBuilder.setIndex(result.next, true)
doBackup()
}
}
override fun onFailure(e: Exception) {
}
viewLifecycleOwner.lifecycleScope.launchIO {
runSuspendCatching {
doBackup()
}.onSuccess {
showTip(R.string.settings_advanced_backup_favorite_success, BaseScene.LENGTH_SHORT)
}.onFailure {
showTip(R.string.settings_advanced_backup_favorite_failed, BaseScene.LENGTH_SHORT)
}
override fun onCancel() {
showTip(R.string.settings_advanced_backup_favorite_failed, BaseScene.LENGTH_SHORT)
}
})
request.setArgs(favListUrlBuilder.build())
mClient.enqueue(request, viewLifecycleOwner.lifecycle.coroutineScope)
}
}
override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean {