fix(app): update checker crash on release builds

This commit is contained in:
rhunk 2024-08-11 00:24:43 +02:00
parent 39525c9dca
commit b2587593cd
2 changed files with 11 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package me.rhunk.snapenhance.ui.manager.data
import com.google.gson.JsonParser import com.google.gson.JsonParser
import me.rhunk.snapenhance.common.BuildConfig import me.rhunk.snapenhance.common.BuildConfig
import me.rhunk.snapenhance.common.logger.AbstractLogger
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
@ -12,7 +13,7 @@ object Updater {
val releaseUrl: String val releaseUrl: String
) )
fun checkForLatestRelease(): LatestRelease? { private fun fetchLatestRelease() = runCatching {
val endpoint = Request.Builder().url("https://api.github.com/repos/rhunk/SnapEnhance/releases").build() val endpoint = Request.Builder().url("https://api.github.com/repos/rhunk/SnapEnhance/releases").build()
val response = OkHttpClient().newCall(endpoint).execute() val response = OkHttpClient().newCall(endpoint).execute()
@ -24,8 +25,14 @@ object Updater {
val latestRelease = releases.get(0).asJsonObject val latestRelease = releases.get(0).asJsonObject
val latestVersion = latestRelease.getAsJsonPrimitive("tag_name").asString val latestVersion = latestRelease.getAsJsonPrimitive("tag_name").asString
if (latestVersion.removePrefix("v") == BuildConfig.VERSION_NAME) return null if (latestVersion.removePrefix("v") == BuildConfig.VERSION_NAME) return@runCatching null
return LatestRelease(latestVersion, endpoint.url.toString().replace("api.", "").replace("repos/", "")) LatestRelease(latestVersion, endpoint.url.toString().replace("api.", "").replace("repos/", ""))
}.onFailure {
AbstractLogger.directError("Failed to fetch latest release", it)
}.getOrNull()
val latestRelease by lazy {
fetchLatestRelease()
} }
} }

View File

@ -201,7 +201,7 @@ class HomeRootSection : Routes.Route() {
} }
val latestUpdate by rememberAsyncMutableState(defaultValue = null) { val latestUpdate by rememberAsyncMutableState(defaultValue = null) {
if (!BuildConfig.DEBUG) Updater.checkForLatestRelease() else null if (!BuildConfig.DEBUG) Updater.latestRelease else null
} }
if (latestUpdate != null) { if (latestUpdate != null) {