diff --git a/app/src/main/java/io/legado/app/help/AppUpdate.kt b/app/src/main/java/io/legado/app/help/AppUpdate.kt new file mode 100644 index 000000000..96196f26f --- /dev/null +++ b/app/src/main/java/io/legado/app/help/AppUpdate.kt @@ -0,0 +1,48 @@ +package io.legado.app.help + +import io.legado.app.constant.AppConst +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.model.NoStackTraceException +import io.legado.app.utils.jsonPath +import io.legado.app.utils.readString +import io.legado.app.utils.toastOnUi +import kotlinx.coroutines.CoroutineScope +import splitties.init.appCtx + +object AppUpdate { + + fun checkUpdate( + scope: CoroutineScope, + showErrorMsg: Boolean = true, + callback: (newVersion: String, updateBody: String, url: String) -> Unit + ) { + Coroutine.async(scope) { + val lastReleaseUrl = "https://api.github.com/repos/gedoor/legado/releases/latest" + 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("获取新版本出错") + return@async Triple(tagName, updateBody, downloadUrl) + } else { + throw NoStackTraceException("已是最新版本") + } + }.onSuccess { + callback.invoke(it.first, it.second, it.third) + }.onError { + appCtx.toastOnUi("检测更新\n${it.localizedMessage}") + } + } + +} \ 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 fdf797777..3e8b86471 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 @@ -5,11 +5,13 @@ import android.net.Uri import android.os.Bundle import android.view.View import androidx.annotation.StringRes +import androidx.lifecycle.lifecycleScope import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import io.legado.app.R import io.legado.app.constant.AppConst.appInfo import io.legado.app.help.AppConfig +import io.legado.app.help.AppUpdate import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.selector import io.legado.app.ui.widget.dialog.TextDialog @@ -56,7 +58,7 @@ class AboutFragment : PreferenceFragmentCompat() { when (preference?.key) { "contributors" -> openUrl(R.string.contributors_url) "update_log" -> showUpdateLog() - "check_update" -> openUrl(R.string.latest_release_url) + "check_update" -> checkUpdate() "mail" -> requireContext().sendMail("kunfei.ge@gmail.com") "sourceRuleSummary" -> openUrl(R.string.source_rule_url) "git" -> openUrl(R.string.this_github_url) @@ -82,6 +84,14 @@ class AboutFragment : PreferenceFragmentCompat() { TextDialog.show(childFragmentManager, log, TextDialog.MD) } + private fun checkUpdate() { + AppUpdate.checkUpdate(lifecycleScope) { newVersion, updateBody, url -> + childFragmentManager.showDialog( + UpdateDialog(newVersion, updateBody, url) + ) + } + } + private fun showQqGroups() { alert(titleResource = R.string.join_qq_group) { val names = arrayListOf() diff --git a/app/src/main/java/io/legado/app/ui/about/UpdateDialog.kt b/app/src/main/java/io/legado/app/ui/about/UpdateDialog.kt new file mode 100644 index 000000000..3205ebac6 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/about/UpdateDialog.kt @@ -0,0 +1,71 @@ +package io.legado.app.ui.about + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import io.legado.app.R +import io.legado.app.base.BaseDialogFragment +import io.legado.app.databinding.DialogUpdateBinding +import io.legado.app.help.AppConfig +import io.legado.app.lib.theme.primaryColor +import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding +import io.legado.app.utils.windowSize +import io.noties.markwon.Markwon +import io.noties.markwon.ext.tables.TablePlugin +import io.noties.markwon.html.HtmlPlugin +import io.noties.markwon.image.glide.GlideImagesPlugin + +class UpdateDialog() : BaseDialogFragment() { + + constructor(newVersion: String, updateBody: String, url: String) : this() { + arguments = Bundle().apply { + putString("newVersion", newVersion) + putString("updateBody", updateBody) + putString("url", url) + } + } + + val binding by viewBinding(DialogUpdateBinding::bind) + + override fun onStart() { + super.onStart() + val dm = requireActivity().windowSize + dialog?.window?.setLayout( + (dm.widthPixels * 0.9).toInt(), + ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.dialog_update, container) + } + + override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { + binding.toolBar.setBackgroundColor(primaryColor) + binding.toolBar.title = arguments?.getString("newVersion") + val updateBody = arguments?.getString("updateBody") + if (updateBody == null) { + toastOnUi("没有数据") + dismiss() + return + } + binding.textView.post { + Markwon.builder(requireContext()) + .usePlugin(GlideImagesPlugin.create(requireContext())) + .usePlugin(HtmlPlugin.create()) + .usePlugin(TablePlugin.create(requireContext())) + .build() + .setMarkdown(binding.textView, updateBody) + } + if (!AppConfig.isGooglePlay) { + binding.toolBar.inflateMenu(R.menu.app_update) + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_update.xml b/app/src/main/res/layout/dialog_update.xml new file mode 100644 index 000000000..4f9fdef42 --- /dev/null +++ b/app/src/main/res/layout/dialog_update.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/app_update.xml b/app/src/main/res/menu/app_update.xml new file mode 100644 index 000000000..1e6a13fd2 --- /dev/null +++ b/app/src/main/res/menu/app_update.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file