resolve PR comment

This commit is contained in:
Jason Yao
2024-07-24 22:47:23 -07:00
parent f896183309
commit b3c9f5b03b
18 changed files with 121 additions and 88 deletions
@@ -61,7 +61,7 @@ object AppConst {
appInfo.versionName = it.versionName
// TODO: 增加测试版还是正式版的检查
if (it.packageName.contains("releaseA")) {
appInfo.appVariant = AppVariant.COMPATIBLE
appInfo.appVariant = AppVariant.BETA_RELEASEA
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
@@ -145,7 +145,7 @@ object PreferKey {
const val mouseWheelPage = "mouseWheelPage"
const val recordHeapDump = "recordHeapDump"
const val optimizeRender = "optimizeRender"
const val updateToBeta = "updateToBeta"
const val updateToVariant = "updateToVariant"
const val cPrimary = "colorPrimary"
const val cAccent = "colorAccent"
@@ -23,8 +23,6 @@ object AppUpdate {
fun check(scope: CoroutineScope): Coroutine<UpdateInfo>
fun checkBeta(scope: CoroutineScope): Coroutine<UpdateInfo>
}
}
@@ -4,33 +4,45 @@ import androidx.annotation.Keep
import com.google.gson.Gson
import io.legado.app.constant.AppConst
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.config.AppConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.okHttpClient
import io.legado.app.model.AppReleaseInfo
import io.legado.app.model.AppVariant
import io.legado.app.model.GithubRelease
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject
import kotlinx.coroutines.CoroutineScope
@Keep
@Suppress("unused")
object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
// TODO:在关于->其他,添加其他版本选项,使用该函数
private suspend fun getRecentReleases(): List<AppReleaseInfo> {
val lastReleaseUrl =
"https://api.github.com/repos/gedoor/legado/releases?page=1?per_page=1"
private val checkVariant: AppVariant
get() = when (AppConfig.updateToVariant) {
"official_version" -> AppVariant.OFFICIAL
"beta_release_version" -> AppVariant.BETA_RELEASE
"beta_releaseA_version" -> AppVariant.BETA_RELEASEA
else -> AppConst.appInfo.appVariant
}
private suspend fun getLatestRelease(): List<AppReleaseInfo> {
val lastReleaseUrl = if (checkVariant.name.lowercase().contains("beta")) {
"https://api.github.com/repos/gedoor/legado/releases/tags/beta"
} else {
"https://api.github.com/repos/gedoor/legado/releases/latest"
}
val body = okHttpClient.newCallStrResponse {
url(lastReleaseUrl)
}.body
if (body.isNullOrBlank()) {
throw NoStackTraceException("获取新版本出错")
}
return Gson().fromJsonArray<GithubRelease>(body)
return Gson().fromJsonObject<GithubRelease>(body)
.getOrElse {
throw NoStackTraceException("获取新版本出错 " + it.localizedMessage)
}
.flatMap { it.gitReleaseToAppReleaseInfo() }
.gitReleaseToAppReleaseInfo()
.sortedByDescending { it.createdAt }
}
@@ -38,9 +50,8 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
scope: CoroutineScope,
): Coroutine<AppUpdate.UpdateInfo> {
return Coroutine.async(scope) {
val recentReleases = getRecentReleases()
recentReleases
.filter { it.appVariant == AppConst.appInfo.appVariant }
getLatestRelease()
.filter { it.appVariant == checkVariant }
.firstOrNull { it.versionName > AppConst.appInfo.versionName }
?.let {
return@async AppUpdate.UpdateInfo(
@@ -49,41 +60,8 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
it.downloadUrl,
it.name
)
} ?: throw NoStackTraceException("已是最新版本")
}
?: throw NoStackTraceException("已是最新版本")
}.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 name = rootDoc.readString("$.name")
?: throw NoStackTraceException("获取新版本出错")
val tagName = name.replace("legado_app_", "")
if (tagName > AppConst.appInfo.versionName) {
val assetsMap = rootDoc.read<List<Any>>(path)
.firstOrNull()
?: throw NoStackTraceException("获取新版本出错")
val assets = jsonPath.parse(assetsMap)
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,7 +471,7 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
val defaultHomePage get() = appCtx.getPrefString(PreferKey.defaultHomePage, "bookshelf")
val updateToBeta get() = appCtx.getPrefBoolean(PreferKey.updateToBeta, false)
val updateToVariant get() = appCtx.getPrefString(PreferKey.updateToVariant, "default_version")
val doublePageHorizontal: String?
get() = appCtx.getPrefString(PreferKey.doublePageHorizontal)
@@ -16,7 +16,7 @@ data class AppReleaseInfo(
enum class AppVariant {
OFFICIAL,
COMPATIBLE,
BETA_RELEASEA,
BETA_RELEASE,
UNKNOWN
}
@@ -58,7 +58,7 @@ data class Asset(
return when {
preRelease && name.contains("releaseA") ->
AppReleaseInfo(
AppVariant.COMPATIBLE,
AppVariant.BETA_RELEASEA,
timestamp,
note,
name,
@@ -76,19 +76,9 @@ data class Asset(
url
)
!preRelease && name.contains("release") ->
AppReleaseInfo(
AppVariant.OFFICIAL,
timestamp,
note,
name,
apkUrl,
url
)
else ->
AppReleaseInfo(
AppVariant.UNKNOWN,
AppVariant.OFFICIAL,
timestamp,
note,
name,
@@ -88,20 +88,16 @@ class AboutFragment : PreferenceFragmentCompat() {
private fun checkUpdate() {
waitDialog.show()
AppUpdate.gitHubUpdate?.run {
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()
}
check(lifecycleScope)
.onSuccess {
showDialogFragment(
UpdateDialog(it)
)
}.onError {
appCtx.toastOnUi("${getString(R.string.check_update)}\n${it.localizedMessage}")
}.onFinally {
waitDialog.dismiss()
}
}
}