修复测试版获取问题

This commit is contained in:
HapeLee
2025-09-10 19:31:26 +08:00
parent b75c8f3ce4
commit 7e97a6be05
2 changed files with 60 additions and 15 deletions
@@ -36,6 +36,11 @@ data class GithubRelease(
val body: String,
@SerializedName("prerelease")
val isPreRelease: Boolean,
@SerializedName("tag_name")
val tagName: String,
val name: String?,
@SerializedName("created_at")
val createdAt: String?
) {
fun gitReleaseToAppReleaseInfo(): List<AppReleaseInfo> {
assets ?: throw NoStackTraceException("获取新版本出错")
@@ -45,6 +50,7 @@ data class GithubRelease(
}
}
@Keep
data class Asset(
@SerializedName("browser_download_url")
@@ -67,11 +73,13 @@ data class Asset(
val instant = Instant.parse(createdAt)
val timestamp: Long = instant.toEpochMilli()
val appVariant = when {
preRelease && name.contains("release") -> AppVariant.BETA_RELEASE
else -> AppVariant.OFFICIAL
val appVariant = if (preRelease) {
AppVariant.BETA_RELEASE
} else {
AppVariant.OFFICIAL
}
return AppReleaseInfo(appVariant, timestamp, note, name, apkUrl, url)
}
}
@@ -1,6 +1,6 @@
package io.legato.kazusa.help.update
import androidx.annotation.Keep
import android.util.Log
import io.legato.kazusa.constant.AppConst
import io.legato.kazusa.exception.NoStackTraceException
import io.legato.kazusa.help.config.AppConfig
@@ -13,8 +13,6 @@ import io.legato.kazusa.utils.fromJsonArray
import io.legato.kazusa.utils.fromJsonObject
import kotlinx.coroutines.CoroutineScope
@Keep
@Suppress("unused")
object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
private val checkVariant: AppVariant
@@ -31,6 +29,8 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
"https://api.github.com/repos/HapeLee/legado-with-MD3/releases/latest"
}
Log.d("AppUpdate", "checkVariant=$checkVariant, url=$lastReleaseUrl")
val res = okHttpClient.newCallResponse {
url(lastReleaseUrl)
}
@@ -41,18 +41,33 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
if (body.isBlank()) {
throw NoStackTraceException("获取新版本出错")
}
return if (checkVariant.isBeta()) {
GSON.fromJsonArray<GithubRelease>(body)
val releases = GSON.fromJsonArray<GithubRelease>(body)
.getOrElse { throw NoStackTraceException("获取新版本出错 ${it.localizedMessage}") }
.filter { it.isPreRelease }
Log.d("AppUpdate", "beta releases.size=${releases.size}")
releases.forEach {
Log.d(
"AppUpdate",
"beta release: tag=${it.tagName}, preRelease=${it.isPreRelease}, name=${it.name}"
)
}
releases.filter { it.isPreRelease }
.flatMap { it.gitReleaseToAppReleaseInfo() }
.sortedByDescending { it.createdAt }
.also { Log.d("AppUpdate", "filtered beta releases.size=${it.size}") }
} else {
listOf(
GSON.fromJsonObject<GithubRelease>(body)
.getOrElse { throw NoStackTraceException("获取新版本出错 ${it.localizedMessage}") }
.gitReleaseToAppReleaseInfo()
).flatten()
val release = GSON.fromJsonObject<GithubRelease>(body)
.getOrElse { throw NoStackTraceException("获取新版本出错 ${it.localizedMessage}") }
Log.d(
"AppUpdate",
"official release: tag=${release.tagName}, preRelease=${release.isPreRelease}, name=${release.name}"
)
release.gitReleaseToAppReleaseInfo()
.sortedByDescending { it.createdAt }
}
}
@@ -60,11 +75,22 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
override fun check(scope: CoroutineScope): Coroutine<AppUpdate.UpdateInfo> {
return Coroutine.async(scope) {
val currentVersion = AppConst.appInfo.versionName
Log.d("AppUpdate", "currentVersion=$currentVersion, checkVariant=$checkVariant")
val releases = getLatestRelease()
.filter { it.appVariant == checkVariant }
val latest = releases
.firstOrNull { it.versionName > currentVersion }
Log.d("AppUpdate", "after variant filter releases.size=${releases.size}")
releases.forEach {
Log.d(
"AppUpdate",
"release: version=${it.versionName}, variant=${it.appVariant}, createdAt=${it.createdAt}"
)
}
val latest = releases.firstOrNull { it.versionName.versionCompare(currentVersion) > 0 }
Log.d("AppUpdate", "latest=${latest?.versionName}")
if (latest != null) {
return@async AppUpdate.UpdateInfo(
@@ -79,4 +105,15 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
}.timeout(10000)
}
fun String.versionCompare(other: String): Int {
val thisParts = this.split(".")
val otherParts = other.split(".")
val maxLength = maxOf(thisParts.size, otherParts.size)
for (i in 0 until maxLength) {
val thisPart = thisParts.getOrNull(i)?.toIntOrNull() ?: 0
val otherPart = otherParts.getOrNull(i)?.toIntOrNull() ?: 0
if (thisPart != otherPart) return thisPart - otherPart
}
return 0
}
}