pull/1352/head
parent
353746e024
commit
51bdceb836
@ -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}") |
||||
} |
||||
} |
||||
|
||||
} |
@ -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…
Reference in new issue