From e9a1bf22b1e737b3938279c8c1062500001a3461 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Thu, 13 Jul 2023 17:53:23 +0800 Subject: [PATCH] Tweak archive download and image limit reset --- .../com/hippo/ehviewer/client/EhEngine.kt | 14 +++------ .../ehviewer/client/exception/EhException.kt | 4 +++ .../exception/InsufficientFundsException.kt | 5 +++ .../ehviewer/client/parser/ArchiveParser.kt | 13 ++++++-- .../ehviewer/client/parser/HomeParser.kt | 11 +++---- .../ehviewer/ui/scene/GalleryDetailScene.kt | 31 ++++++++----------- .../hippo/ehviewer/ui/settings/EhScreen.kt | 11 ++++--- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 10 files changed, 51 insertions(+), 44 deletions(-) create mode 100644 app/src/main/java/com/hippo/ehviewer/client/exception/InsufficientFundsException.kt diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt index f273ade05..570127a5a 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt @@ -35,7 +35,6 @@ import com.hippo.ehviewer.client.parser.GalleryPageApiParser import com.hippo.ehviewer.client.parser.GalleryPageParser import com.hippo.ehviewer.client.parser.GalleryTokenApiParser import com.hippo.ehviewer.client.parser.HomeParser -import com.hippo.ehviewer.client.parser.Limits import com.hippo.ehviewer.client.parser.ProfileParser import com.hippo.ehviewer.client.parser.RateGalleryResult import com.hippo.ehviewer.client.parser.SignInParser @@ -306,14 +305,11 @@ object EhEngine { return null } - suspend fun resetImageLimits(): Limits? { - return ehRequest(EhUrl.URL_HOME) { - formBody { - add("act", "limits") - add("reset", "Reset Limit") - } - }.executeAndParsingWith(HomeParser::parseResetLimits) - } + suspend fun resetImageLimits() = ehRequest(EhUrl.URL_HOME) { + formBody { + add("reset_imagelimit", "Reset Limit") + } + }.executeAndParsingWith(HomeParser::parseResetLimits) suspend fun modifyFavorites(url: String, gidArray: LongArray, dstCat: Int): FavoritesParser.Result { val catStr: String = when (dstCat) { diff --git a/app/src/main/java/com/hippo/ehviewer/client/exception/EhException.kt b/app/src/main/java/com/hippo/ehviewer/client/exception/EhException.kt index fec2cbb93..7d5124fb9 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/exception/EhException.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/exception/EhException.kt @@ -15,7 +15,11 @@ */ package com.hippo.ehviewer.client.exception +import androidx.annotation.StringRes +import splitties.init.appCtx + open class EhException : Exception { constructor(detailMessage: String?) : super(detailMessage) constructor(detailMessage: String?, cause: Throwable?) : super(detailMessage, cause) + constructor(@StringRes message: Int) : super(appCtx.getString(message)) } diff --git a/app/src/main/java/com/hippo/ehviewer/client/exception/InsufficientFundsException.kt b/app/src/main/java/com/hippo/ehviewer/client/exception/InsufficientFundsException.kt new file mode 100644 index 000000000..fdc8b1fdb --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/exception/InsufficientFundsException.kt @@ -0,0 +1,5 @@ +package com.hippo.ehviewer.client.exception + +import com.hippo.ehviewer.R + +class InsufficientFundsException : EhException(R.string.insufficient_funds) diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.kt index effed80a8..c195faa57 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ArchiveParser.kt @@ -15,6 +15,7 @@ */ package com.hippo.ehviewer.client.parser +import com.hippo.ehviewer.client.exception.InsufficientFundsException import com.hippo.ehviewer.client.exception.NoHAtHClientException import org.jsoup.Jsoup @@ -27,8 +28,10 @@ object ArchiveParser { Regex("
") private val PATTERN_HATH_ARCHIVE = Regex("

([^<]+)

\\s*

([\\w. ]+)

\\s*

([\\w. ]+)

") - private val PATTERN_NEED_HATH_CLIENT = - Regex("You must have a H@H client assigned to your account to use this feature\\.") + private const val ERROR_NEED_HATH_CLIENT = + "You must have a H@H client assigned to your account to use this feature." + private const val ERROR_INSUFFICIENT_FUNDS = + "You do not have enough funds to download this archive." fun parse(body: String): Result { val m = PATTERN_HATH_FORM.find(body)!! @@ -66,7 +69,11 @@ object ArchiveParser { } fun parseArchiveUrl(body: String): String? { - if (PATTERN_NEED_HATH_CLIENT.containsMatchIn(body)) throw NoHAtHClientException("No H@H client") + if (body.contains(ERROR_NEED_HATH_CLIENT)) { + throw NoHAtHClientException("No H@H client") + } else if (body.contains(ERROR_INSUFFICIENT_FUNDS)) { + throw InsufficientFundsException() + } return Jsoup.parse(body).selectFirst("#continue>a[href]") ?.let { it.attr("href") + "?start=1" } // TODO: Check more errors diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/HomeParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/HomeParser.kt index b877740fc..3926d389c 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/HomeParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/HomeParser.kt @@ -1,20 +1,19 @@ package com.hippo.ehviewer.client.parser import android.os.Parcelable +import com.hippo.ehviewer.client.exception.InsufficientFundsException import com.hippo.ehviewer.client.exception.ParseException import kotlinx.parcelize.Parcelize object HomeParser { private val PATTERN_FUNDS = Regex("Available: ([\\d,]+) Credits.*Available: ([\\d,]+) kGP", RegexOption.DOT_MATCHES_ALL) - private const val RESET_SUCCEED = "Image limit was successfully reset." + private const val INSUFFICIENT_FUNDS = "Insufficient funds." fun parse(body: String) = parseLimit(body) - fun parseResetLimits(body: String): Limits? { - return if (body.contains(RESET_SUCCEED)) { - null - } else { - parse(body) + fun parseResetLimits(body: String) { + if (body.contains(INSUFFICIENT_FUNDS)) { + throw InsufficientFundsException() } } 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 1a46eca9d..72ac0b4d4 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 @@ -116,10 +116,10 @@ import com.hippo.ehviewer.client.data.GalleryInfo import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED import com.hippo.ehviewer.client.data.GalleryTagGroup import com.hippo.ehviewer.client.data.ListUrlBuilder +import com.hippo.ehviewer.client.exception.EhException import com.hippo.ehviewer.client.exception.NoHAtHClientException import com.hippo.ehviewer.client.parser.ArchiveParser import com.hippo.ehviewer.client.parser.HomeParser -import com.hippo.ehviewer.client.parser.ParserUtils import com.hippo.ehviewer.client.parser.TorrentResult import com.hippo.ehviewer.coil.justDownload import com.hippo.ehviewer.dao.DownloadInfo @@ -1366,19 +1366,6 @@ class GalleryDetailScene : BaseScene() { val context = context val activity = mainActivity if (null != context && null != activity && null != mArchiveList && position < mArchiveList!!.size) { - if (mArchiveList!![position].name == "Insufficient Funds") { - showTip(R.string.insufficient_funds, LENGTH_SHORT) - return - } else if (mCurrentFunds != null) { - val cost = ParserUtils.parseInt( - mArchiveList!![position].cost.removeSuffix("GP").removeSuffix("Credits"), - 0, - ) - if (cost > maxOf(mCurrentFunds!!.fundsGP, mCurrentFunds!!.fundsC)) { - showTip(R.string.insufficient_funds, LENGTH_SHORT) - return - } - } val res = mArchiveList!![position].res val isHAtH = mArchiveList!![position].isHAtH composeBindingGD?.run { @@ -1400,10 +1387,18 @@ class GalleryDetailScene : BaseScene() { } showTip(R.string.download_archive_started, LENGTH_SHORT) }.onFailure { - if (it is NoHAtHClientException) { - showTip(R.string.download_archive_failure_no_hath, LENGTH_LONG) - } else { - showTip(R.string.download_archive_failure, LENGTH_LONG) + when (it) { + is NoHAtHClientException -> { + showTip(R.string.download_archive_failure_no_hath, LENGTH_LONG) + } + + is EhException -> { + showTip(ExceptionUtils.getReadableString(it), LENGTH_LONG) + } + + else -> { + showTip(R.string.download_archive_failure, LENGTH_LONG) + } } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/settings/EhScreen.kt b/app/src/main/java/com/hippo/ehviewer/ui/settings/EhScreen.kt index 27ace6b81..580711fd3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/settings/EhScreen.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/settings/EhScreen.kt @@ -127,8 +127,10 @@ fun EhScreen() { val placeholder = stringResource(id = R.string.please_wait) val resetImageLimitSucceed = stringResource(id = R.string.reset_limits_succeed) val noImageLimits = stringResource(id = R.string.image_limits_summary, 0, 0) + var result by remember { mutableStateOf(null) } var summary by rememberSaveable { mutableStateOf(noImageLimits) } suspend fun getImageLimits() = EhEngine.getImageLimits().also { + result = it summary = context.getString(R.string.image_limits_summary, it.limits.current, it.limits.maximum) } val deferredResult = remember { coroutineScope.async { runSuspendCatching { getImageLimits() } } } @@ -144,15 +146,14 @@ fun EhScreen() { fun bind(result: HomeParser.Result) { val (current, maximum, resetCost) = result.limits val (fundsGP, fundsC) = result.funds - val cost = if (fundsGP >= resetCost) "$resetCost GP" else "$resetCost Credits" - val message = context.getString(R.string.current_limits, "$current / $maximum", cost) + "\n" + context.getString(R.string.current_funds, "$fundsGP+", fundsC) + val message = context.getString(R.string.current_limits, "$current / $maximum", resetCost) + "\n" + context.getString(R.string.current_funds, "$fundsGP+", fundsC) dialog.setMessage(message) - resetButton.isEnabled = resetCost in 1..maxOf(fundsGP, fundsC) + resetButton.isEnabled = resetCost != 0 } coroutineScope.launch { runSuspendCatching { - val result = deferredResult.await().getOrNull() ?: getImageLimits() - withUIContext { bind(result) } + result ?: deferredResult.await().getOrNull() ?: getImageLimits() + withUIContext { bind(result!!) } }.onFailure { withUIContext { dialog.setMessage(it.localizedMessage) } } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index e025d5536..7d8696f12 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -455,7 +455,7 @@ 余额不足 图片配额 %1$d / %2$d(点击刷新) - 当前:%1$s\n重置花费:%2$s + 当前:%1$s\n重置花费:%2$d GP 成功重置图片配额 重置 隐私 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index a20113fdc..74cd95b3c 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -446,7 +446,7 @@ 餘額不足 圖片配額 %1$d / %2$d(點擊以重新整理) - 目前:%1$s\n重置花費:%2$s + 目前:%1$s\n重置花費:%2$d GP 成功重置圖片配額 重置 隱私 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e56a6e3fb..ed79087b4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -520,7 +520,7 @@ Insufficient Funds Image Limits %1$d / %2$d (Click to refresh) - Current: %1$s\nReset Cost: %2$s + Current: %1$s\nReset Cost: %2$d GP Image limit was successfully reset Reset Privacy