修改可以获取所有更新
This commit is contained in:
@@ -19,6 +19,7 @@ data class AppReleaseInfo(
|
|||||||
enum class AppVariant {
|
enum class AppVariant {
|
||||||
OFFICIAL,
|
OFFICIAL,
|
||||||
BETA_RELEASE,
|
BETA_RELEASE,
|
||||||
|
ALL,
|
||||||
UNKNOWN;
|
UNKNOWN;
|
||||||
|
|
||||||
fun isBeta(): Boolean {
|
fun isBeta(): Boolean {
|
||||||
|
|||||||
@@ -18,34 +18,49 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
|
|||||||
get() = when (AppConfig.updateToVariant) {
|
get() = when (AppConfig.updateToVariant) {
|
||||||
"official_version" -> AppVariant.OFFICIAL
|
"official_version" -> AppVariant.OFFICIAL
|
||||||
"beta_release_version" -> AppVariant.BETA_RELEASE
|
"beta_release_version" -> AppVariant.BETA_RELEASE
|
||||||
|
"all_version" -> AppVariant.ALL
|
||||||
else -> AppConst.appInfo.appVariant
|
else -> AppConst.appInfo.appVariant
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun getLatestRelease(): List<AppReleaseInfo> {
|
private suspend fun getLatestRelease(): List<AppReleaseInfo> {
|
||||||
val lastReleaseUrl = if (checkVariant.isBeta()) {
|
val url = if (checkVariant == AppVariant.OFFICIAL)
|
||||||
"https://api.github.com/repos/HapeLee/legado-with-MD3/releases"
|
|
||||||
} else {
|
|
||||||
"https://api.github.com/repos/HapeLee/legado-with-MD3/releases/latest"
|
"https://api.github.com/repos/HapeLee/legado-with-MD3/releases/latest"
|
||||||
}
|
else
|
||||||
|
"https://api.github.com/repos/HapeLee/legado-with-MD3/releases"
|
||||||
|
|
||||||
val res = okHttpClient.newCallResponse { url(lastReleaseUrl) }
|
val res = okHttpClient.newCallResponse { url(url) }
|
||||||
if (!res.isSuccessful) throw NoStackTraceException("获取新版本出错(${res.code})")
|
if (!res.isSuccessful) throw NoStackTraceException("获取新版本出错(${res.code})")
|
||||||
|
|
||||||
val body = res.body.text()
|
val body = res.body.text()
|
||||||
if (body.isBlank()) throw NoStackTraceException("获取新版本出错")
|
if (body.isBlank()) throw NoStackTraceException("获取新版本出错")
|
||||||
|
|
||||||
return if (checkVariant.isBeta()) {
|
return when (checkVariant) {
|
||||||
val releases = GSON.fromJsonArray<GithubRelease>(body)
|
AppVariant.BETA_RELEASE -> {
|
||||||
.getOrElse { throw NoStackTraceException("获取新版本出错 ${it.localizedMessage}") }
|
val releases = GSON.fromJsonArray<GithubRelease>(body)
|
||||||
|
.getOrElse { throw NoStackTraceException("解析失败 ${it.localizedMessage}") }
|
||||||
|
|
||||||
releases.filter { it.isPreRelease }
|
releases.filter { it.isPreRelease }
|
||||||
.flatMap { it.gitReleaseToAppReleaseInfo() }
|
.flatMap { it.gitReleaseToAppReleaseInfo() }
|
||||||
.sortedByDescending { it.createdAt }
|
.sortedByDescending { it.createdAt }
|
||||||
} else {
|
}
|
||||||
val release = GSON.fromJsonObject<GithubRelease>(body)
|
|
||||||
.getOrElse { throw NoStackTraceException("获取新版本出错 ${it.localizedMessage}") }
|
|
||||||
|
|
||||||
release.gitReleaseToAppReleaseInfo()
|
AppVariant.OFFICIAL -> {
|
||||||
.sortedByDescending { it.createdAt }
|
val release = GSON.fromJsonObject<GithubRelease>(body)
|
||||||
|
.getOrElse { throw NoStackTraceException("解析失败 ${it.localizedMessage}") }
|
||||||
|
|
||||||
|
release.gitReleaseToAppReleaseInfo()
|
||||||
|
.sortedByDescending { it.createdAt }
|
||||||
|
}
|
||||||
|
|
||||||
|
AppVariant.ALL -> {
|
||||||
|
val releases = GSON.fromJsonArray<GithubRelease>(body)
|
||||||
|
.getOrElse { throw NoStackTraceException("解析失败 ${it.localizedMessage}") }
|
||||||
|
|
||||||
|
releases.flatMap { it.gitReleaseToAppReleaseInfo() }
|
||||||
|
.sortedByDescending { it.createdAt }
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> emptyList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,12 +68,17 @@ object AppUpdateGitHub : AppUpdate.AppUpdateInterface {
|
|||||||
return Coroutine.async(scope) {
|
return Coroutine.async(scope) {
|
||||||
val currentVersion = AppConst.appInfo.versionName
|
val currentVersion = AppConst.appInfo.versionName
|
||||||
val releases = getLatestRelease()
|
val releases = getLatestRelease()
|
||||||
.filter { it.appVariant == checkVariant }
|
|
||||||
|
|
||||||
val latest = releases.firstOrNull { r ->
|
val filtered = if (checkVariant == AppVariant.ALL) {
|
||||||
|
releases
|
||||||
|
} else {
|
||||||
|
releases.filter { it.appVariant == checkVariant }
|
||||||
|
}
|
||||||
|
|
||||||
|
val latest = filtered.firstOrNull { r ->
|
||||||
try {
|
try {
|
||||||
r.versionName.versionCompare(currentVersion) > 0
|
r.versionName.versionCompare(currentVersion) > 0
|
||||||
} catch (e: Exception) {
|
} catch (_: Exception) {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ class UpdateDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_update) {
|
|||||||
get() = when (AppConfig.updateToVariant) {
|
get() = when (AppConfig.updateToVariant) {
|
||||||
"official_version" -> AppVariant.OFFICIAL
|
"official_version" -> AppVariant.OFFICIAL
|
||||||
"beta_release_version" -> AppVariant.BETA_RELEASE
|
"beta_release_version" -> AppVariant.BETA_RELEASE
|
||||||
|
"all_version" -> AppVariant.ALL
|
||||||
else -> AppConst.appInfo.appVariant
|
else -> AppConst.appInfo.appVariant
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user