Skip to content

Commit

Permalink
manager: show changelog when upgrade manager
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Sep 10, 2023
1 parent ad1dbf7 commit eac6fd0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
28 changes: 25 additions & 3 deletions manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -71,29 +74,45 @@ fun HomeScreen(navigator: DestinationsNavigator) {
DonateCard()
LearnMoreCard()
Spacer(Modifier)
ConfirmDialog()
}
}
}

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

Expand Down Expand Up @@ -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 = {})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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()) {
Expand All @@ -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)
}

}
Expand Down
2 changes: 1 addition & 1 deletion manager/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit eac6fd0

Please sign in to comment.