pull/2723/head
kunfei 2 years ago
parent 1a9a5770fe
commit 542a116afd
  1. 46
      app/src/app/java/io/legado/app/help/AppUpdateGitHub.kt
  2. 114
      app/src/app/res/xml/about.xml
  3. 43
      app/src/main/java/io/legado/app/help/AppUpdate.kt
  4. 26
      app/src/main/java/io/legado/app/ui/about/AboutFragment.kt
  5. 7
      app/src/main/res/xml/about.xml

@ -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<AppUpdate.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 AppUpdate.UpdateInfo(tagName, updateBody, downloadUrl, fileName)
} else {
throw NoStackTraceException("已是最新版本")
}
}.timeout(10000)
}
}

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<io.legado.app.lib.prefs.Preference
android:key="contributors"
android:summary="@string/contributors_summary"
android:title="@string/contributors"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="update_log"
android:title="@string/update_log"
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="check_update"
android:title="@string/check_update"
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.PreferenceCategory
android:title="@string/contact"
app:allowDividerAbove="true"
app:allowDividerBelow="false"
app:iconSpaceReserved="false"
app:layout="@layout/view_preference_category">
<io.legado.app.lib.prefs.Preference
android:key="gzGzh"
android:summary="@string/official_account"
android:title="@string/follow_official_account"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="qqChannel"
android:title="@string/join_qq_channel"
android:summary="@string/qq_channel_summary"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="qq"
android:summary="@string/click_to_apply"
android:title="@string/join_qq_group"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="mail"
android:summary="@string/email"
android:title="@string/send_mail"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="git"
android:summary="@string/this_github_url"
android:title="@string/git_hub"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="discord"
android:summary="@string/discord_url"
android:title="Discord"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="tg"
android:summary="@string/tg_url"
android:title="TG"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="sourceRuleSummary"
android:summary="@string/source_rule_url"
android:title="@string/source_rule_s"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="home_page"
android:summary="@string/home_page_url"
android:title="@string/home_page"
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false" />
</io.legado.app.lib.prefs.PreferenceCategory>
<io.legado.app.lib.prefs.PreferenceCategory
android:title="@string/other"
app:allowDividerAbove="true"
app:allowDividerBelow="false"
app:iconSpaceReserved="false"
app:layout="@layout/view_preference_category">
<io.legado.app.lib.prefs.Preference
android:key="crashLog"
android:title="@string/crash_log"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="license"
android:title="@string/license"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="disclaimer"
android:title="@string/disclaimer"
app:iconSpaceReserved="false" />
</io.legado.app.lib.prefs.PreferenceCategory>
</androidx.preference.PreferenceScreen>

@ -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()
}
}
}
/**

@ -15,13 +15,6 @@
app:allowDividerBelow="false"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.Preference
android:key="check_update"
android:title="@string/check_update"
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:iconSpaceReserved="false" />
<io.legado.app.lib.prefs.PreferenceCategory
android:title="@string/contact"
app:allowDividerAbove="true"

Loading…
Cancel
Save