优化
This commit is contained in:
@@ -1,44 +1,13 @@
|
||||
package io.legado.app.help
|
||||
|
||||
import io.legado.app.R
|
||||
import io.legado.app.constant.AppConst
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
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.utils.jsonPath
|
||||
import io.legado.app.utils.readString
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import splitties.init.appCtx
|
||||
|
||||
object AppUpdate {
|
||||
|
||||
fun checkFromGitHub(
|
||||
scope: CoroutineScope,
|
||||
): Coroutine<UpdateInfo> {
|
||||
return Coroutine.async(scope) {
|
||||
val lastReleaseUrl = appCtx.getString(R.string.latest_release_api)
|
||||
val body = okHttpClient.newCallStrResponse {
|
||||
url(lastReleaseUrl)
|
||||
}.body
|
||||
if (body.isNullOrBlank()) {
|
||||
throw NoStackTraceException("获取新版本出错")
|
||||
}
|
||||
val rootDoc = jsonPath.parse(body)
|
||||
val tagName = rootDoc.readString("$.tag_name")
|
||||
?: throw NoStackTraceException("获取新版本出错")
|
||||
if (tagName > AppConst.appInfo.versionName) {
|
||||
val updateBody = rootDoc.readString("$.body")
|
||||
?: throw NoStackTraceException("获取新版本出错")
|
||||
val downloadUrl = rootDoc.readString("$.assets[0].browser_download_url")
|
||||
?: throw NoStackTraceException("获取新版本出错")
|
||||
val fileName = rootDoc.readString("$.assets[0].name")
|
||||
?: throw NoStackTraceException("获取新版本出错")
|
||||
return@async UpdateInfo(tagName, updateBody, downloadUrl, fileName)
|
||||
} else {
|
||||
throw NoStackTraceException("已是最新版本")
|
||||
}
|
||||
}.timeout(10000)
|
||||
val gitHubUpdate by lazy {
|
||||
Class.forName("io.legado.app.help.AppUpdateGitHub").kotlin.objectInstance
|
||||
as? AppUpdateInterface
|
||||
}
|
||||
|
||||
data class UpdateInfo(
|
||||
@@ -48,4 +17,10 @@ object AppUpdate {
|
||||
val fileName: String
|
||||
)
|
||||
|
||||
interface AppUpdateInterface {
|
||||
|
||||
fun check(scope: CoroutineScope): Coroutine<UpdateInfo>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import androidx.preference.PreferenceFragmentCompat
|
||||
import io.legado.app.R
|
||||
import io.legado.app.constant.AppConst.appInfo
|
||||
import io.legado.app.help.AppUpdate
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.lib.dialogs.alert
|
||||
import io.legado.app.lib.dialogs.selector
|
||||
import io.legado.app.ui.widget.dialog.TextDialog
|
||||
@@ -45,9 +44,6 @@ class AboutFragment : PreferenceFragmentCompat() {
|
||||
addPreferencesFromResource(R.xml.about)
|
||||
findPreference<Preference>("update_log")?.summary =
|
||||
"${getString(R.string.version)} ${appInfo.versionName}"
|
||||
if (AppConfig.isGooglePlay) {
|
||||
preferenceScreen.removePreferenceRecursively("check_update")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
@@ -94,16 +90,18 @@ class AboutFragment : PreferenceFragmentCompat() {
|
||||
*/
|
||||
private fun checkUpdate() {
|
||||
waitDialog.show()
|
||||
AppUpdate.checkFromGitHub(lifecycleScope)
|
||||
.onSuccess {
|
||||
showDialogFragment(
|
||||
UpdateDialog(it)
|
||||
)
|
||||
}.onError {
|
||||
appCtx.toastOnUi("${getString(R.string.check_update)}\n${it.localizedMessage}")
|
||||
}.onFinally {
|
||||
waitDialog.hide()
|
||||
}
|
||||
AppUpdate.gitHubUpdate?.run {
|
||||
check(lifecycleScope)
|
||||
.onSuccess {
|
||||
showDialogFragment(
|
||||
UpdateDialog(it)
|
||||
)
|
||||
}.onError {
|
||||
appCtx.toastOnUi("${getString(R.string.check_update)}\n${it.localizedMessage}")
|
||||
}.onFinally {
|
||||
waitDialog.hide()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user