manager: show changelog when upgrade manager
This commit is contained in:
@@ -29,9 +29,12 @@ import com.ramcosta.composedestinations.annotation.Destination
|
|||||||
import com.ramcosta.composedestinations.annotation.RootNavGraph
|
import com.ramcosta.composedestinations.annotation.RootNavGraph
|
||||||
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
|
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import me.weishu.kernelsu.*
|
import me.weishu.kernelsu.*
|
||||||
import me.weishu.kernelsu.R
|
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.screen.destinations.SettingScreenDestination
|
||||||
import me.weishu.kernelsu.ui.util.*
|
import me.weishu.kernelsu.ui.util.*
|
||||||
|
|
||||||
@@ -71,6 +74,7 @@ fun HomeScreen(navigator: DestinationsNavigator) {
|
|||||||
DonateCard()
|
DonateCard()
|
||||||
LearnMoreCard()
|
LearnMoreCard()
|
||||||
Spacer(Modifier)
|
Spacer(Modifier)
|
||||||
|
ConfirmDialog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,22 +82,37 @@ fun HomeScreen(navigator: DestinationsNavigator) {
|
|||||||
@Composable
|
@Composable
|
||||||
fun UpdateCard() {
|
fun UpdateCard() {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val newVersion by produceState(initialValue = 0 to "") {
|
val newVersion by produceState(initialValue = Triple(0, "", "")) {
|
||||||
value = withContext(Dispatchers.IO) { checkNewVersion() }
|
value = withContext(Dispatchers.IO) { checkNewVersion() }
|
||||||
}
|
}
|
||||||
val currentVersionCode = getManagerVersion(context).second
|
val currentVersionCode = getManagerVersion(context).second
|
||||||
val newVersionCode = newVersion.first
|
val newVersionCode = newVersion.first
|
||||||
val newVersionUrl = newVersion.second
|
val newVersionUrl = newVersion.second
|
||||||
|
val changelog = newVersion.third
|
||||||
if (newVersionCode <= currentVersionCode) {
|
if (newVersionCode <= currentVersionCode) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val uriHandler = LocalUriHandler.current
|
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(
|
WarningCard(
|
||||||
message = stringResource(id = R.string.new_version_available).format(newVersionCode),
|
message = stringResource(id = R.string.new_version_available).format(newVersionCode),
|
||||||
MaterialTheme.colorScheme.outlineVariant
|
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() {
|
private fun WarningCardPreview() {
|
||||||
Column {
|
Column {
|
||||||
WarningCard(message = "Warning message")
|
WarningCard(message = "Warning message")
|
||||||
WarningCard(message = "Warning message ", MaterialTheme.colorScheme.outlineVariant, onClick = {})
|
WarningCard(
|
||||||
|
message = "Warning message ",
|
||||||
|
MaterialTheme.colorScheme.outlineVariant,
|
||||||
|
onClick = {})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,9 +60,9 @@ fun download(
|
|||||||
downloadManager.enqueue(request)
|
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 url = "https://api.github.com/repos/tiann/KernelSU/releases/latest"
|
||||||
val defaultValue = 0 to ""
|
val defaultValue = Triple(0, "", "")
|
||||||
runCatching {
|
runCatching {
|
||||||
okhttp3.OkHttpClient().newCall(okhttp3.Request.Builder().url(url).build()).execute()
|
okhttp3.OkHttpClient().newCall(okhttp3.Request.Builder().url(url).build()).execute()
|
||||||
.use { response ->
|
.use { response ->
|
||||||
@@ -71,6 +71,7 @@ fun checkNewVersion(): Pair<Int, String> {
|
|||||||
}
|
}
|
||||||
val body = response.body?.string() ?: return defaultValue
|
val body = response.body?.string() ?: return defaultValue
|
||||||
val json = org.json.JSONObject(body)
|
val json = org.json.JSONObject(body)
|
||||||
|
val changelog = json.optString("body")
|
||||||
|
|
||||||
val assets = json.getJSONArray("assets")
|
val assets = json.getJSONArray("assets")
|
||||||
for (i in 0 until assets.length()) {
|
for (i in 0 until assets.length()) {
|
||||||
@@ -86,7 +87,7 @@ fun checkNewVersion(): Pair<Int, String> {
|
|||||||
val versionCode = matchResult.groupValues[2].toInt()
|
val versionCode = matchResult.groupValues[2].toInt()
|
||||||
val downloadUrl = asset.getString("browser_download_url")
|
val downloadUrl = asset.getString("browser_download_url")
|
||||||
|
|
||||||
return versionCode to downloadUrl
|
return Triple(versionCode, downloadUrl, changelog)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@
|
|||||||
<string name="module_update">Update</string>
|
<string name="module_update">Update</string>
|
||||||
<string name="module_downloading">Downloading module: %s</string>
|
<string name="module_downloading">Downloading module: %s</string>
|
||||||
<string name="module_start_downloading">Start downloading: %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="launch_app">Launch</string>
|
||||||
<string name="force_stop_app">Force Stop</string>
|
<string name="force_stop_app">Force Stop</string>
|
||||||
<string name="restart_app">Restart</string>
|
<string name="restart_app">Restart</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user