pull/1352/head
gedoor 3 years ago
parent 353746e024
commit 51bdceb836
  1. 48
      app/src/main/java/io/legado/app/help/AppUpdate.kt
  2. 12
      app/src/main/java/io/legado/app/ui/about/AboutFragment.kt
  3. 71
      app/src/main/java/io/legado/app/ui/about/UpdateDialog.kt
  4. 23
      app/src/main/res/layout/dialog_update.xml
  5. 10
      app/src/main/res/menu/app_update.xml

@ -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}")
}
}
}

@ -5,11 +5,13 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.AppConst.appInfo import io.legado.app.constant.AppConst.appInfo
import io.legado.app.help.AppConfig 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.alert
import io.legado.app.lib.dialogs.selector import io.legado.app.lib.dialogs.selector
import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.ui.widget.dialog.TextDialog
@ -56,7 +58,7 @@ class AboutFragment : PreferenceFragmentCompat() {
when (preference?.key) { when (preference?.key) {
"contributors" -> openUrl(R.string.contributors_url) "contributors" -> openUrl(R.string.contributors_url)
"update_log" -> showUpdateLog() "update_log" -> showUpdateLog()
"check_update" -> openUrl(R.string.latest_release_url) "check_update" -> checkUpdate()
"mail" -> requireContext().sendMail("kunfei.ge@gmail.com") "mail" -> requireContext().sendMail("kunfei.ge@gmail.com")
"sourceRuleSummary" -> openUrl(R.string.source_rule_url) "sourceRuleSummary" -> openUrl(R.string.source_rule_url)
"git" -> openUrl(R.string.this_github_url) "git" -> openUrl(R.string.this_github_url)
@ -82,6 +84,14 @@ class AboutFragment : PreferenceFragmentCompat() {
TextDialog.show(childFragmentManager, log, TextDialog.MD) TextDialog.show(childFragmentManager, log, TextDialog.MD)
} }
private fun checkUpdate() {
AppUpdate.checkUpdate(lifecycleScope) { newVersion, updateBody, url ->
childFragmentManager.showDialog(
UpdateDialog(newVersion, updateBody, url)
)
}
}
private fun showQqGroups() { private fun showQqGroups() {
alert(titleResource = R.string.join_qq_group) { alert(titleResource = R.string.join_qq_group) {
val names = arrayListOf<String>() val names = arrayListOf<String>()

@ -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)
}
}
}

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="?attr/actionBarStyle"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:titleTextAppearance="@style/ToolbarTitle" />
<io.legado.app.ui.widget.text.InertiaScrollTextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp"
android:textIsSelectable="true" />
</LinearLayout>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_download"
android:title="@string/action_download"
app:showAsAction="always" />
</menu>
Loading…
Cancel
Save