This commit is contained in:
Horis
2024-07-24 15:08:42 +08:00
parent d82c09dfc7
commit 4590215d90
14 changed files with 73 additions and 10 deletions
@@ -145,6 +145,7 @@ object PreferKey {
const val mouseWheelPage = "mouseWheelPage"
const val recordHeapDump = "recordHeapDump"
const val optimizeRender = "optimizeRender"
const val updateToBeta = "updateToBeta"
const val cPrimary = "colorPrimary"
const val cAccent = "colorAccent"
@@ -23,6 +23,8 @@ object AppUpdate {
fun check(scope: CoroutineScope): Coroutine<UpdateInfo>
fun checkBeta(scope: CoroutineScope): Coroutine<UpdateInfo>
}
}
@@ -47,5 +47,37 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
}.timeout(10000)
}
override fun checkBeta(scope: CoroutineScope): Coroutine<AppUpdate.UpdateInfo> {
return Coroutine.async(scope) {
val preReleaseUrl = "https://api.github.com/repos/gedoor/legado/releases/tags/beta"
val body = okHttpClient.newCallStrResponse {
url(preReleaseUrl)
}.body
if (body.isNullOrBlank()) {
throw NoStackTraceException("获取新版本出错")
}
val rootDoc = jsonPath.parse(body)
val path = "\$.assets[?(@.name =~ /legado_${appCtx.channel}_.*?apk\$/)]"
val assetsMap = rootDoc.read<List<Any>>(path)
.firstOrNull()
?: throw NoStackTraceException("获取新版本出错")
val assets = jsonPath.parse(assetsMap)
val name = assets.readString("$.name")
?: throw NoStackTraceException("获取新版本出错")
val tagName = name.replace("legado_${appCtx.channel}_|\\.apk".toRegex(), "")
if (tagName > AppConst.appInfo.versionName) {
val updateBody = rootDoc.readString("$.body")
?: throw NoStackTraceException("获取新版本出错")
val downloadUrl = assets.readString("$.browser_download_url")
?: throw NoStackTraceException("获取新版本出错")
val fileName = assets.readString("$.name")
?: throw NoStackTraceException("获取新版本出错")
return@async AppUpdate.UpdateInfo(tagName, updateBody, downloadUrl, fileName)
} else {
throw NoStackTraceException("已是最新版本")
}
}.timeout(10000)
}
}
@@ -471,6 +471,8 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
val defaultHomePage get() = appCtx.getPrefString(PreferKey.defaultHomePage, "bookshelf")
val updateToBeta get() = appCtx.getPrefBoolean(PreferKey.updateToBeta, false)
val doublePageHorizontal: String?
get() = appCtx.getPrefString(PreferKey.doublePageHorizontal)
@@ -88,16 +88,20 @@ class AboutFragment : PreferenceFragmentCompat() {
private fun checkUpdate() {
waitDialog.show()
AppUpdate.gitHubUpdate?.run {
check(lifecycleScope)
.onSuccess {
showDialogFragment(
UpdateDialog(it)
)
}.onError {
appCtx.toastOnUi("${getString(R.string.check_update)}\n${it.localizedMessage}")
}.onFinally {
waitDialog.dismiss()
}
val job = if (AppConfig.updateToBeta) {
checkBeta(lifecycleScope)
} else {
check(lifecycleScope)
}
job.onSuccess {
showDialogFragment(
UpdateDialog(it)
)
}.onError {
appCtx.toastOnUi("${getString(R.string.check_update)}\n${it.localizedMessage}")
}.onFinally {
waitDialog.dismiss()
}
}
}