Show current funds in archive dialog

This commit is contained in:
FooIbar authored and tarsin committed 2023-01-15 22:19:41 +08:00
1 parent 52fa8f64cc
commit 2afe5fe5b2
4 files changed
+50 -11

No files matched your search

@@ -24,6 +24,8 @@ import java.util.regex.Pattern
object ArchiveParser {
private val PATTERN_ARCHIVE_URL =
Pattern.compile("<strong>(.*)</strong>.*<a href=\"([^\"]*)\">Click Here To Start Downloading</a>")
private val PATTERN_CURRENT_FUNDS =
Pattern.compile("<p>([\\d,]+) GP \\[[^]]*] &nbsp; ([\\d,]+) Credits \\[[^]]*]</p>")
private val PATTERN_HATH_FORM =
Pattern.compile("<form id=\"hathdl_form\" action=\"[^\"]*?or=([^=\"]*?)\" method=\"post\">")
private val PATTERN_HATH_ARCHIVE =
@@ -42,18 +44,17 @@ object ArchiveParser {
val d = Jsoup.parse(body)
val es = d.select("#db>div>div")
for (e in es) {
try {
val btn = e.selectFirst("form>div>input")
if (!btn!!.hasAttr("disabled")) {
if (!e.attr("style").contains("color:#CCCCCC")) {
try {
val res = e.selectFirst("form>input")!!.attr("value")
val name = btn.attr("value")
val name = e.selectFirst("form>div>input")!!.attr("value")
val size = e.selectFirst("p>strong")!!.text()
val cost = e.selectFirst("div>strong")!!.text().replace(",", "")
val item = Archive(res, name, size, cost, false)
archiveList.add(item)
} catch (ex: NullPointerException) {
ex.printStackTrace()
}
} catch (ex: NullPointerException) {
ex.printStackTrace()
}
}
m = PATTERN_HATH_ARCHIVE.matcher(body)
@@ -65,7 +66,14 @@ object ArchiveParser {
val item = Archive(res, name, size, cost, true)
archiveList.add(item)
}
return Result(paramOr, archiveList)
var funds: Funds? = null
m = PATTERN_CURRENT_FUNDS.matcher(body)
if (m.find()) {
val fundsGP = ParserUtils.parseInt(m.group(1), 0)
val fundsC = ParserUtils.parseInt(m.group(2), 0)
funds = Funds(fundsGP, fundsC)
}
return Result(paramOr, archiveList, funds)
}
@Throws(NoHAtHClientException::class)
@@ -104,5 +112,7 @@ object ArchiveParser {
}
}
class Result(val paramOr: String?, val archiveList: List<Archive>)
class Funds(var fundsGP: Int, var fundsC: Int)
class Result(val paramOr: String?, val archiveList: List<Archive>, val funds: Funds?)
}
@@ -78,6 +78,7 @@ import com.hippo.ehviewer.client.data.GalleryTagGroup
import com.hippo.ehviewer.client.data.ListUrlBuilder
import com.hippo.ehviewer.client.exception.NoHAtHClientException
import com.hippo.ehviewer.client.parser.ArchiveParser
import com.hippo.ehviewer.client.parser.ParserUtils
import com.hippo.ehviewer.client.parser.RateGalleryParser
import com.hippo.ehviewer.client.parser.TorrentParser
import com.hippo.ehviewer.client.parser.VoteTagParser
@@ -148,6 +149,7 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
}
private var mArchiveFormParamOr: String? = null
private var mArchiveList: List<ArchiveParser.Archive>? = null
private var mCurrentFunds: ArchiveParser.Funds? = null
@State
private var mState = STATE_INIT
@@ -1438,12 +1440,12 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
.setCallback(this)
mRequest!!.enqueue(this@GalleryDetailScene)
} else {
bind(mArchiveList)
bind(mArchiveList, mCurrentFunds)
}
}
}
private fun bind(data: List<ArchiveParser.Archive>?) {
private fun bind(data: List<ArchiveParser.Archive>?, funds: ArchiveParser.Funds?) {
if (null == mDialog || null == mProgressView || null == mErrorText || null == mListView) {
return
}
@@ -1463,6 +1465,15 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
mListView!!.visibility = View.VISIBLE
mListView!!.adapter =
ArrayAdapter(mDialog!!.context, R.layout.item_select_dialog, nameArray)
if (funds != null) {
mDialog!!.setTitle(
getString(
R.string.current_funds,
funds.fundsGP,
funds.fundsC
)
)
}
}
}
@@ -1470,6 +1481,19 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
val context = context
val activity = mainActivity
if (null != context && null != activity && null != mArchiveList && position < mArchiveList!!.size) {
if (mCurrentFunds != null) {
val cost = ParserUtils.parseInt(
mArchiveList!![position].cost.removeSuffix("GP").removeSuffix("Credits"), 0
)
if (cost <= mCurrentFunds!!.fundsGP) {
mCurrentFunds!!.fundsGP -= cost
} else if (cost <= mCurrentFunds!!.fundsC) {
mCurrentFunds!!.fundsC -= cost
} else {
showTip(R.string.insufficient_funds, LENGTH_SHORT)
return
}
}
val res = mArchiveList!![position].res
val isHAtH = mArchiveList!![position].isHAtH
val request = EhRequest()
@@ -1506,7 +1530,8 @@ class GalleryDetailScene : CollapsingToolbarScene(), View.OnClickListener, Downl
mRequest = null
mArchiveFormParamOr = result.paramOr
mArchiveList = result.archiveList
bind(result.archiveList)
mCurrentFunds = result.funds
bind(result.archiveList, result.funds)
}
}
@@ -531,6 +531,8 @@
<string name="archive_original">原始档案</string>
<string name="archive_resample">重采样档案</string>
<string name="download_archive_failure_no_hath">下载压缩包需要 H@H 客户端</string>
<string name="current_funds">资金:%d GP,%d Credits</string>
<string name="insufficient_funds">余额不足</string>
<string name="settings_privacy">隐私</string>
<string name="settings_privacy_secure">不允许屏幕抓取</string>
<string name="settings_privacy_secure_summary">启用后,将不能截取该应用的屏幕截图,同时,将不会在系统任务切换器中显示该应用的内容预览,重新加载应用以生效此更改</string>
+2
View File
@@ -598,6 +598,8 @@
<string name="archive_original">Original archive</string>
<string name="archive_resample">Resample archive</string>
<string name="download_archive_failure_no_hath">Need H@H client for archive download</string>
<string name="current_funds">Funds: %d GP, %d Credits</string>
<string name="insufficient_funds">Insufficient Funds</string>
<string name="settings_privacy">Privacy</string>
<string name="settings_privacy_secure">Prevent screenshots</string>
<string name="settings_privacy_secure_summary">Prevent the content of the app from being taken screenshots of or shown in the \"Recent Apps\" list.</string>