From eac6fd048467c3f845d3f28306caa7d25657bcfa Mon Sep 17 00:00:00 2001 From: weishu Date: Mon, 11 Sep 2023 00:44:24 +0800 Subject: [PATCH] manager: show changelog when upgrade manager --- .../java/me/weishu/kernelsu/ui/screen/Home.kt | 28 +++++++++++++++++-- .../me/weishu/kernelsu/ui/util/Downloader.kt | 7 +++-- manager/app/src/main/res/values/strings.xml | 2 +- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Home.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Home.kt index c59576280aa9..9d3f5fac18cc 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Home.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Home.kt @@ -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,22 +82,37 @@ 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 ) { - uriHandler.openUri(newVersionUrl) + scope.launch { + if (changelog.isEmpty() || dialogHost.showConfirm( + title = title, + content = changelog, + markdown = true, + confirm = updateText, + ) == ConfirmResult.Confirmed + ) { + uriHandler.openUri(newVersionUrl) + } + } } } @@ -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 = {}) } } \ No newline at end of file diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt index 7fb0720dc5b4..cfe98fae5827 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt @@ -60,9 +60,9 @@ fun download( downloadManager.enqueue(request) } -fun checkNewVersion(): Pair { +fun checkNewVersion(): Triple { 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 { } 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 { val versionCode = matchResult.groupValues[2].toInt() val downloadUrl = asset.getString("browser_download_url") - return versionCode to downloadUrl + return Triple(versionCode, downloadUrl, changelog) } } diff --git a/manager/app/src/main/res/values/strings.xml b/manager/app/src/main/res/values/strings.xml index d174ff602218..5c1fdbdd0412 100644 --- a/manager/app/src/main/res/values/strings.xml +++ b/manager/app/src/main/res/values/strings.xml @@ -79,7 +79,7 @@ Update Downloading module: %s Start downloading: %s - New version: %s is available, click to download + New version: %s is available, click to upgrade Launch Force Stop Restart