From d36e365921fcec0b1ca82b30ebd0160763171e0d Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 16 May 2024 13:05:14 +0530 Subject: [PATCH] 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 --- .../java/me/weishu/kernelsu/ui/screen/Home.kt | 16 +++++++++++----- .../me/weishu/kernelsu/ui/util/Downloader.kt | 12 +++++++++--- .../kernelsu/ui/util/module/LatestVersionInfo.kt | 7 +++++++ manager/gradle.properties | 1 + manager/gradlew | 1 - 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 manager/app/src/main/java/me/weishu/kernelsu/ui/util/module/LatestVersionInfo.kt 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 3425826ef946..7f1d483f14bf 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 @@ -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) 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 e72d70b2d074..954cac50da30 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 @@ -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 { +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 { val versionCode = matchResult.groupValues[2].toInt() val downloadUrl = asset.getString("browser_download_url") - return Triple(versionCode, downloadUrl, changelog) + return LatestVersionInfo( + versionCode, + downloadUrl, + changelog + ) } } diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/module/LatestVersionInfo.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/module/LatestVersionInfo.kt new file mode 100644 index 000000000000..374b3853ab1a --- /dev/null +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/module/LatestVersionInfo.kt @@ -0,0 +1,7 @@ +package me.weishu.kernelsu.ui.util.module + +data class LatestVersionInfo( + val versionCode : Int = 0, + val downloadUrl : String = "", + val changelog : String = "" +) diff --git a/manager/gradle.properties b/manager/gradle.properties index 387da396166f..6b24dafe1b15 100644 --- a/manager/gradle.properties +++ b/manager/gradle.properties @@ -1,3 +1,4 @@ android.experimental.enableNewResourceShrinker.preciseShrinking=true android.enableAppCompileTimeRClass=true android.useAndroidX=true +org.gradle.jvmargs=-Xmx3072m \ No newline at end of file diff --git a/manager/gradlew b/manager/gradlew index 1aa94a426907..4b6a9cfc46fa 100755 --- a/manager/gradlew +++ b/manager/gradlew @@ -1,5 +1,4 @@ #!/bin/sh - # # Copyright © 2015-2021 the original authors. #