Skip to content

Commit

Permalink
Refactor checkNewVersion function to use LatestVersionInfo data class (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
ALEX5402 and tiann authored May 16, 2024
1 parent 0576495 commit d36e365
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
16 changes: 11 additions & 5 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 @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand All @@ -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
)
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions manager/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
android.experimental.enableNewResourceShrinker.preciseShrinking=true
android.enableAppCompileTimeRClass=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx3072m
1 change: 0 additions & 1 deletion manager/gradlew
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/sh

#
# Copyright © 2015-2021 the original authors.
#
Expand Down

0 comments on commit d36e365

Please sign in to comment.