From 542a116afd6477cf22b510799b0537d899291114 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 22 Jan 2023 22:30:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/AppUpdateGitHub.kt | 46 +++++++ app/src/app/res/xml/about.xml | 114 ++++++++++++++++++ .../main/java/io/legado/app/help/AppUpdate.kt | 43 ++----- .../io/legado/app/ui/about/AboutFragment.kt | 26 ++-- app/src/main/res/xml/about.xml | 7 -- 5 files changed, 181 insertions(+), 55 deletions(-) create mode 100644 app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt create mode 100644 app/src/app/res/xml/about.xml diff --git a/app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt b/app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt new file mode 100644 index 000000000..3e8572993 --- /dev/null +++ b/app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt @@ -0,0 +1,46 @@ +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 + +@Suppress("unused") +object AppUpdateGitHub: AppUpdate.AppUpdateInterface { + + override fun check( + scope: CoroutineScope, + ): Coroutine { + 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 AppUpdate.UpdateInfo(tagName, updateBody, downloadUrl, fileName) + } else { + throw NoStackTraceException("已是最新版本") + } + }.timeout(10000) + } + + +} \ No newline at end of file diff --git a/app/src/app/res/xml/about.xml b/app/src/app/res/xml/about.xml new file mode 100644 index 000000000..b62f4a2f4 --- /dev/null +++ b/app/src/app/res/xml/about.xml @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/AppUpdate.kt b/app/src/main/java/io/legado/app/help/AppUpdate.kt index 805036363..dcb4272b0 100644 --- a/app/src/main/java/io/legado/app/help/AppUpdate.kt +++ b/app/src/main/java/io/legado/app/help/AppUpdate.kt @@ -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 { - 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 + + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt b/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt index d7abf763b..cacb63949 100644 --- a/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt +++ b/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt @@ -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("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() + } + } } /** diff --git a/app/src/main/res/xml/about.xml b/app/src/main/res/xml/about.xml index b62f4a2f4..2ba8835cc 100644 --- a/app/src/main/res/xml/about.xml +++ b/app/src/main/res/xml/about.xml @@ -15,13 +15,6 @@ app:allowDividerBelow="false" app:iconSpaceReserved="false" /> - -