fix(ui,update): remove refresh button, rely on pull-to-refresh, sync update check in background worker (v1.1.9)

This commit is contained in:
Vaishno Singh
2026-11-19 10:50:47 +05:30
parent d8eaceb314
commit 6e6eb837ba
4 changed files with 166 additions and 184 deletions
+2 -2
View File
@@ -11,8 +11,8 @@ android {
applicationId = "com.expapp.trigger" applicationId = "com.expapp.trigger"
minSdk = 24 minSdk = 24
targetSdk = 34 targetSdk = 34
versionCode = 12 versionCode = 13
versionName = "1.1.8" versionName = "1.1.9"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables { vectorDrawables {
@@ -38,7 +38,6 @@ import androidx.compose.material.icons.filled.DeleteOutline
import androidx.compose.material.icons.filled.Edit import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.PowerOff import androidx.compose.material.icons.filled.PowerOff
import androidx.compose.material.icons.filled.PowerSettingsNew import androidx.compose.material.icons.filled.PowerSettingsNew
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.RestartAlt import androidx.compose.material.icons.filled.RestartAlt
import androidx.compose.material.icons.filled.Schedule import androidx.compose.material.icons.filled.Schedule
import androidx.compose.material.icons.filled.Terminal import androidx.compose.material.icons.filled.Terminal
@@ -262,15 +261,8 @@ fun DevicePage(
) )
} }
// Spin animation for refresh icon
val infiniteTransition = rememberInfiniteTransition(label = "refresh_spin")
val spinAngle by infiniteTransition.animateFloat(
initialValue = 0f, targetValue = 360f,
animationSpec = infiniteRepeatable(tween(900, easing = LinearEasing)),
label = "spin"
)
// Pulse for online dot // Pulse for online dot
val infiniteTransition = rememberInfiniteTransition(label = "pulse_transition")
val pulseScale by infiniteTransition.animateFloat( val pulseScale by infiniteTransition.animateFloat(
initialValue = 0.8f, targetValue = 1.5f, initialValue = 0.8f, targetValue = 1.5f,
animationSpec = infiniteRepeatable( animationSpec = infiniteRepeatable(
@@ -394,44 +386,7 @@ fun DevicePage(
) )
} }
// Compact, non-overlapping actions: Single Refresh + Overflow Menu // Device options menu (Edit / Delete)
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(6.dp)
) {
// Refresh button (syncs status & R2 endpoints)
IconButton(
onClick = {
if (!isCheckingStatus) {
onRefreshStatus()
onSyncConfig()
}
},
modifier = Modifier
.size(34.dp)
.clip(RoundedCornerShape(9.dp))
.background(
if (isCheckingStatus) GreenVibrant.copy(alpha = 0.15f)
else BorderSubtle.copy(alpha = 0.25f)
)
.border(
1.dp,
if (isCheckingStatus) GreenVibrant.copy(alpha = 0.4f)
else BorderSubtle,
RoundedCornerShape(9.dp)
)
) {
Icon(
imageVector = Icons.Default.Refresh,
contentDescription = "Refresh server",
tint = if (isCheckingStatus) GreenVibrant else TextSecondary,
modifier = Modifier
.size(17.dp)
.rotate(if (isCheckingStatus) spinAngle else 0f)
)
}
// More Options Overflow (Edit / Delete)
Box { Box {
IconButton( IconButton(
onClick = { showMenu = true }, onClick = { showMenu = true },
@@ -489,7 +444,6 @@ fun DevicePage(
} }
} }
} }
}
Spacer(modifier = Modifier.height(14.dp)) Spacer(modifier = Modifier.height(14.dp))
@@ -44,15 +44,11 @@ class UpdateManager(
} }
/** /**
* Checks GitHub Releases API for the latest published release. * Synchronously checks GitHub Releases API for the latest published release.
* Suitable for use inside background workers (e.g. WorkManager CoroutineWorker).
*/ */
fun checkForUpdate( fun checkForUpdateSync(currentVersionName: String): UpdateCheckResult {
currentVersionName: String,
callback: (UpdateCheckResult) -> Unit
) {
// Query releases list so we always inspect the highest semantic version
val url = "https://api.github.com/repos/$githubRepo/releases" val url = "https://api.github.com/repos/$githubRepo/releases"
val request = Request.Builder() val request = Request.Builder()
.url(url) .url(url)
.addHeader("Accept", "application/vnd.github.v3+json") .addHeader("Accept", "application/vnd.github.v3+json")
@@ -60,33 +56,29 @@ class UpdateManager(
.get() .get()
.build() .build()
okHttpClient.newCall(request).enqueue(object : Callback { return try {
override fun onFailure(call: Call, e: IOException) { val response = okHttpClient.newCall(request).execute()
runOnMain {
callback(UpdateCheckResult.Error("Network error: ${e.localizedMessage}"))
}
}
override fun onResponse(call: Call, response: Response) {
response.use { res -> response.use { res ->
if (res.code == 404) { if (res.code == 404) {
runOnMain { callback(UpdateCheckResult.UpToDate) } return UpdateCheckResult.UpToDate
return
} }
if (!res.isSuccessful) { if (!res.isSuccessful) {
runOnMain { return UpdateCheckResult.Error("HTTP ${res.code}: Unable to check updates")
callback(UpdateCheckResult.Error("HTTP ${res.code}: Unable to check updates")) }
val body = res.body?.string().orEmpty()
parseReleases(body, currentVersionName)
}
} catch (e: Exception) {
UpdateCheckResult.Error("Network error: ${e.localizedMessage}")
} }
return
} }
val body = res.body?.string().orEmpty() private fun parseReleases(body: String, currentVersionName: String): UpdateCheckResult {
try { return try {
val jsonElement = JsonParser.parseString(body) val jsonElement = JsonParser.parseString(body)
val releasesArray = if (jsonElement.isJsonArray) { val releasesArray = if (jsonElement.isJsonArray) {
jsonElement.asJsonArray jsonElement.asJsonArray
} else { } else {
// In case a single release object was returned
val arr = com.google.gson.JsonArray() val arr = com.google.gson.JsonArray()
if (jsonElement.isJsonObject) arr.add(jsonElement.asJsonObject) if (jsonElement.isJsonObject) arr.add(jsonElement.asJsonObject)
arr arr
@@ -143,24 +135,61 @@ class UpdateManager(
} }
if (highestRelease == null) { if (highestRelease == null) {
runOnMain { callback(UpdateCheckResult.UpToDate) } return UpdateCheckResult.UpToDate
return
} }
val cleanLocalVersion = currentVersionName.removePrefix("v").removePrefix("V").trim() val cleanLocalVersion = currentVersionName.removePrefix("v").removePrefix("V").trim()
if (isNewerVersion(highestRelease.versionName, cleanLocalVersion)) { if (isNewerVersion(highestRelease.versionName, cleanLocalVersion)) {
runOnMain { UpdateCheckResult.UpdateAvailable(highestRelease)
callback(UpdateCheckResult.UpdateAvailable(highestRelease))
}
} else { } else {
runOnMain { callback(UpdateCheckResult.UpToDate) } UpdateCheckResult.UpToDate
} }
} catch (e: Exception) { } catch (e: Exception) {
UpdateCheckResult.Error("Failed to parse update info: ${e.message}")
}
}
/**
* Checks GitHub Releases API for the latest published release.
*/
fun checkForUpdate(
currentVersionName: String,
callback: (UpdateCheckResult) -> Unit
) {
// Query releases list so we always inspect the highest semantic version
val url = "https://api.github.com/repos/$githubRepo/releases"
val request = Request.Builder()
.url(url)
.addHeader("Accept", "application/vnd.github.v3+json")
.addHeader("User-Agent", "ServerControlApp")
.get()
.build()
okHttpClient.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
runOnMain { runOnMain {
callback(UpdateCheckResult.Error("Failed to parse update info: ${e.message}")) callback(UpdateCheckResult.Error("Network error: ${e.localizedMessage}"))
} }
} }
override fun onResponse(call: Call, response: Response) {
response.use { res ->
if (res.code == 404) {
runOnMain { callback(UpdateCheckResult.UpToDate) }
return
}
if (!res.isSuccessful) {
runOnMain {
callback(UpdateCheckResult.Error("HTTP ${res.code}: Unable to check updates"))
}
return
}
val body = res.body?.string().orEmpty()
val result = parseReleases(body, currentVersionName)
runOnMain { callback(result) }
} }
} }
}) })
@@ -120,11 +120,11 @@ class ServerMonitorWorker(
} }
} }
// Check for app updates in the background and notify if a new update is found // Check for app updates in the background synchronously and notify if a new update is found
try { try {
val updateManager = com.expapp.trigger.update.UpdateManager(appContext) val updateManager = com.expapp.trigger.update.UpdateManager(appContext)
val currentVersion = com.expapp.trigger.BuildConfig.VERSION_NAME val currentVersion = com.expapp.trigger.BuildConfig.VERSION_NAME
updateManager.checkForUpdate(currentVersion) { result -> val result = updateManager.checkForUpdateSync(currentVersion)
if (result is com.expapp.trigger.update.UpdateCheckResult.UpdateAvailable) { if (result is com.expapp.trigger.update.UpdateCheckResult.UpdateAvailable) {
val newVer = result.info.versionName val newVer = result.info.versionName
val lastNotified = appSettings.lastNotifiedUpdateVersion val lastNotified = appSettings.lastNotifiedUpdateVersion
@@ -133,7 +133,6 @@ class ServerMonitorWorker(
NotificationHelper.sendUpdateAvailableNotification(appContext, newVer) NotificationHelper.sendUpdateAvailableNotification(appContext, newVer)
} }
} }
}
} catch (_: Exception) { } catch (_: Exception) {
// Ignore background check failure // Ignore background check failure
} }