manager: show changelog when upgrade manager

This commit is contained in:
weishu
2023-09-11 00:44:24 +08:00
parent ad1dbf77a1
commit eac6fd0484
3 changed files with 30 additions and 7 deletions

View File

@@ -29,9 +29,12 @@ import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.weishu.kernelsu.*
import me.weishu.kernelsu.R
import me.weishu.kernelsu.ui.component.ConfirmDialog
import me.weishu.kernelsu.ui.component.ConfirmResult
import me.weishu.kernelsu.ui.screen.destinations.SettingScreenDestination
import me.weishu.kernelsu.ui.util.*
@@ -71,6 +74,7 @@ fun HomeScreen(navigator: DestinationsNavigator) {
DonateCard()
LearnMoreCard()
Spacer(Modifier)
ConfirmDialog()
}
}
}
@@ -78,23 +82,38 @@ fun HomeScreen(navigator: DestinationsNavigator) {
@Composable
fun UpdateCard() {
val context = LocalContext.current
val newVersion by produceState(initialValue = 0 to "") {
val newVersion by produceState(initialValue = Triple(0, "", "")) {
value = withContext(Dispatchers.IO) { checkNewVersion() }
}
val currentVersionCode = getManagerVersion(context).second
val newVersionCode = newVersion.first
val newVersionUrl = newVersion.second
val changelog = newVersion.third
if (newVersionCode <= currentVersionCode) {
return
}
val uriHandler = LocalUriHandler.current
val dialogHost = LocalDialogHost.current
val title = stringResource(id = R.string.module_changelog)
val updateText = stringResource(id = R.string.module_update)
val scope = rememberCoroutineScope()
WarningCard(
message = stringResource(id = R.string.new_version_available).format(newVersionCode),
MaterialTheme.colorScheme.outlineVariant
) {
scope.launch {
if (changelog.isEmpty() || dialogHost.showConfirm(
title = title,
content = changelog,
markdown = true,
confirm = updateText,
) == ConfirmResult.Confirmed
) {
uriHandler.openUri(newVersionUrl)
}
}
}
}
@Composable
@@ -364,6 +383,9 @@ private fun StatusCardPreview() {
private fun WarningCardPreview() {
Column {
WarningCard(message = "Warning message")
WarningCard(message = "Warning message ", MaterialTheme.colorScheme.outlineVariant, onClick = {})
WarningCard(
message = "Warning message ",
MaterialTheme.colorScheme.outlineVariant,
onClick = {})
}
}

View File

@@ -60,9 +60,9 @@ fun download(
downloadManager.enqueue(request)
}
fun checkNewVersion(): Pair<Int, String> {
fun checkNewVersion(): Triple<Int, String, String> {
val url = "https://api.github.com/repos/tiann/KernelSU/releases/latest"
val defaultValue = 0 to ""
val defaultValue = Triple(0, "", "")
runCatching {
okhttp3.OkHttpClient().newCall(okhttp3.Request.Builder().url(url).build()).execute()
.use { response ->
@@ -71,6 +71,7 @@ fun checkNewVersion(): Pair<Int, String> {
}
val body = response.body?.string() ?: return defaultValue
val json = org.json.JSONObject(body)
val changelog = json.optString("body")
val assets = json.getJSONArray("assets")
for (i in 0 until assets.length()) {
@@ -86,7 +87,7 @@ fun checkNewVersion(): Pair<Int, String> {
val versionCode = matchResult.groupValues[2].toInt()
val downloadUrl = asset.getString("browser_download_url")
return versionCode to downloadUrl
return Triple(versionCode, downloadUrl, changelog)
}
}

View File

@@ -79,7 +79,7 @@
<string name="module_update">Update</string>
<string name="module_downloading">Downloading module: %s</string>
<string name="module_start_downloading">Start downloading: %s</string>
<string name="new_version_available">New version: %s is available, click to download</string>
<string name="new_version_available">New version: %s is available, click to upgrade</string>
<string name="launch_app">Launch</string>
<string name="force_stop_app">Force Stop</string>
<string name="restart_app">Restart</string>