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:
Alex
2024-05-16 13:05:14 +05:30
committed by GitHub
parent 0576495b4b
commit d36e365921
5 changed files with 28 additions and 9 deletions

View File

@@ -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.InstallScreenDestination
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.*
import me.weishu.kernelsu.ui.util.module.LatestVersionInfo
@RootNavGraph(start = true) @RootNavGraph(start = true)
@Destination @Destination
@@ -100,13 +101,18 @@ fun HomeScreen(navigator: DestinationsNavigator) {
@Composable @Composable
fun UpdateCard() { fun UpdateCard() {
val context = LocalContext.current val context = LocalContext.current
val newVersion by produceState(initialValue = Triple(0, "", "")) { val latestVersionInfo = LatestVersionInfo()
value = withContext(Dispatchers.IO) { checkNewVersion() } val newVersion by produceState(initialValue = latestVersionInfo) {
value = withContext(Dispatchers.IO){
checkNewVersion()
}
} }
val currentVersionCode = getManagerVersion(context).second val currentVersionCode = getManagerVersion(context).second
val newVersionCode = newVersion.first val newVersionCode = newVersion.versionCode
val newVersionUrl = newVersion.second val newVersionUrl = newVersion.downloadUrl
val changelog = newVersion.third val changelog = newVersion.changelog
val uriHandler = LocalUriHandler.current val uriHandler = LocalUriHandler.current
val title = stringResource(id = R.string.module_changelog) val title = stringResource(id = R.string.module_changelog)

View File

@@ -11,6 +11,7 @@ import android.os.Build
import android.os.Environment import android.os.Environment
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.DisposableEffect
import me.weishu.kernelsu.ui.util.module.LatestVersionInfo
/** /**
* @author weishu * @author weishu
@@ -61,9 +62,10 @@ fun download(
downloadManager.enqueue(request) downloadManager.enqueue(request)
} }
fun checkNewVersion(): Triple<Int, String, String> { fun checkNewVersion(): LatestVersionInfo {
val url = "https://api.github.com/repos/tiann/KernelSU/releases/latest" val url = "https://api.github.com/repos/tiann/KernelSU/releases/latest"
val defaultValue = Triple(0, "", "") // default null value if failed
val defaultValue = LatestVersionInfo()
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 ->
@@ -88,7 +90,11 @@ fun checkNewVersion(): Triple<Int, String, 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 Triple(versionCode, downloadUrl, changelog) return LatestVersionInfo(
versionCode,
downloadUrl,
changelog
)
} }
} }

View File

@@ -0,0 +1,7 @@
package me.weishu.kernelsu.ui.util.module
data class LatestVersionInfo(
val versionCode : Int = 0,
val downloadUrl : String = "",
val changelog : String = ""
)

View File

@@ -1,3 +1,4 @@
android.experimental.enableNewResourceShrinker.preciseShrinking=true android.experimental.enableNewResourceShrinker.preciseShrinking=true
android.enableAppCompileTimeRClass=true android.enableAppCompileTimeRClass=true
android.useAndroidX=true android.useAndroidX=true
org.gradle.jvmargs=-Xmx3072m

1
manager/gradlew vendored
View File

@@ -1,5 +1,4 @@
#!/bin/sh #!/bin/sh
# #
# Copyright © 2015-2021 the original authors. # Copyright © 2015-2021 the original authors.
# #