Refactor checkNewVersion function to use LatestVersionInfo data class (#1733)
- Updated the checkNewVersion function to return a LatestVersionInfo data class instead of a Triple. - Defined default null value for LatestVersionInfo in case of failure. - Improved readability and maintainability by replacing the Triple with a data class. - Included version code, download URL, and changelog in the LatestVersionInfo data class. --------- Co-authored-by: weishu <twsxtd@gmail.com>
This commit is contained in:
@@ -38,6 +38,7 @@ import me.weishu.kernelsu.ui.component.rememberConfirmDialog
|
||||
import me.weishu.kernelsu.ui.screen.destinations.InstallScreenDestination
|
||||
import me.weishu.kernelsu.ui.screen.destinations.SettingScreenDestination
|
||||
import me.weishu.kernelsu.ui.util.*
|
||||
import me.weishu.kernelsu.ui.util.module.LatestVersionInfo
|
||||
|
||||
@RootNavGraph(start = true)
|
||||
@Destination
|
||||
@@ -100,13 +101,18 @@ fun HomeScreen(navigator: DestinationsNavigator) {
|
||||
@Composable
|
||||
fun UpdateCard() {
|
||||
val context = LocalContext.current
|
||||
val newVersion by produceState(initialValue = Triple(0, "", "")) {
|
||||
value = withContext(Dispatchers.IO) { checkNewVersion() }
|
||||
val latestVersionInfo = LatestVersionInfo()
|
||||
val newVersion by produceState(initialValue = latestVersionInfo) {
|
||||
value = withContext(Dispatchers.IO){
|
||||
checkNewVersion()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val currentVersionCode = getManagerVersion(context).second
|
||||
val newVersionCode = newVersion.first
|
||||
val newVersionUrl = newVersion.second
|
||||
val changelog = newVersion.third
|
||||
val newVersionCode = newVersion.versionCode
|
||||
val newVersionUrl = newVersion.downloadUrl
|
||||
val changelog = newVersion.changelog
|
||||
|
||||
val uriHandler = LocalUriHandler.current
|
||||
val title = stringResource(id = R.string.module_changelog)
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.os.Build
|
||||
import android.os.Environment
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import me.weishu.kernelsu.ui.util.module.LatestVersionInfo
|
||||
|
||||
/**
|
||||
* @author weishu
|
||||
@@ -61,9 +62,10 @@ fun download(
|
||||
downloadManager.enqueue(request)
|
||||
}
|
||||
|
||||
fun checkNewVersion(): Triple<Int, String, String> {
|
||||
fun checkNewVersion(): LatestVersionInfo {
|
||||
val url = "https://api.github.com/repos/tiann/KernelSU/releases/latest"
|
||||
val defaultValue = Triple(0, "", "")
|
||||
// default null value if failed
|
||||
val defaultValue = LatestVersionInfo()
|
||||
runCatching {
|
||||
okhttp3.OkHttpClient().newCall(okhttp3.Request.Builder().url(url).build()).execute()
|
||||
.use { response ->
|
||||
@@ -88,7 +90,11 @@ fun checkNewVersion(): Triple<Int, String, String> {
|
||||
val versionCode = matchResult.groupValues[2].toInt()
|
||||
val downloadUrl = asset.getString("browser_download_url")
|
||||
|
||||
return Triple(versionCode, downloadUrl, changelog)
|
||||
return LatestVersionInfo(
|
||||
versionCode,
|
||||
downloadUrl,
|
||||
changelog
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package me.weishu.kernelsu.ui.util.module
|
||||
|
||||
data class LatestVersionInfo(
|
||||
val versionCode : Int = 0,
|
||||
val downloadUrl : String = "",
|
||||
val changelog : String = ""
|
||||
)
|
||||
@@ -1,3 +1,4 @@
|
||||
android.experimental.enableNewResourceShrinker.preciseShrinking=true
|
||||
android.enableAppCompileTimeRClass=true
|
||||
android.useAndroidX=true
|
||||
org.gradle.jvmargs=-Xmx3072m
|
||||
1
manager/gradlew
vendored
1
manager/gradlew
vendored
@@ -1,5 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Copyright © 2015-2021 the original authors.
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user