diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 5942c8c..44acfef 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -11,8 +11,8 @@ android { applicationId = "com.expapp.trigger" minSdk = 24 targetSdk = 34 - versionCode = 12 - versionName = "1.1.8" + versionCode = 13 + versionName = "1.1.9" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { diff --git a/app/src/main/java/com/expapp/trigger/ui/screens/DevicePage.kt b/app/src/main/java/com/expapp/trigger/ui/screens/DevicePage.kt index 35bf901..8421c92 100644 --- a/app/src/main/java/com/expapp/trigger/ui/screens/DevicePage.kt +++ b/app/src/main/java/com/expapp/trigger/ui/screens/DevicePage.kt @@ -38,7 +38,6 @@ import androidx.compose.material.icons.filled.DeleteOutline import androidx.compose.material.icons.filled.Edit import androidx.compose.material.icons.filled.PowerOff 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.Schedule 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 + val infiniteTransition = rememberInfiniteTransition(label = "pulse_transition") val pulseScale by infiniteTransition.animateFloat( initialValue = 0.8f, targetValue = 1.5f, animationSpec = infiniteRepeatable( @@ -394,99 +386,61 @@ fun DevicePage( ) } - // Compact, non-overlapping actions: Single Refresh + Overflow Menu - Row( - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(6.dp) - ) { - // Refresh button (syncs status & R2 endpoints) + // Device options menu (Edit / Delete) + Box { IconButton( - onClick = { - if (!isCheckingStatus) { - onRefreshStatus() - onSyncConfig() - } - }, + onClick = { showMenu = true }, 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) - ) + .background(BorderSubtle.copy(alpha = 0.25f)) + .border(1.dp, 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) + imageVector = Icons.Default.MoreVert, + contentDescription = "Device options", + tint = TextSecondary, + modifier = Modifier.size(18.dp) ) } - // More Options Overflow (Edit / Delete) - Box { - IconButton( - onClick = { showMenu = true }, - modifier = Modifier - .size(34.dp) - .clip(RoundedCornerShape(9.dp)) - .background(BorderSubtle.copy(alpha = 0.25f)) - .border(1.dp, BorderSubtle, RoundedCornerShape(9.dp)) - ) { - Icon( - imageVector = Icons.Default.MoreVert, - contentDescription = "Device options", - tint = TextSecondary, - modifier = Modifier.size(18.dp) - ) - } - - DropdownMenu( - expanded = showMenu, - onDismissRequest = { showMenu = false }, - modifier = Modifier - .background(BgElevated) - .border(1.dp, BorderSubtle, RoundedCornerShape(12.dp)) - ) { - DropdownMenuItem( - text = { Text("Edit Device", color = TextPrimary) }, - leadingIcon = { - Icon( - Icons.Default.Edit, - contentDescription = null, - tint = TextSecondary, - modifier = Modifier.size(18.dp) - ) - }, - onClick = { - showMenu = false - showEditDialog = true - } - ) - DropdownMenuItem( - text = { Text("Remove Device", color = RedVibrant) }, - leadingIcon = { - Icon( - Icons.Default.DeleteOutline, - contentDescription = null, - tint = RedVibrant, - modifier = Modifier.size(18.dp) - ) - }, - onClick = { - showMenu = false - showDeleteConfirm = true - } - ) - } + DropdownMenu( + expanded = showMenu, + onDismissRequest = { showMenu = false }, + modifier = Modifier + .background(BgElevated) + .border(1.dp, BorderSubtle, RoundedCornerShape(12.dp)) + ) { + DropdownMenuItem( + text = { Text("Edit Device", color = TextPrimary) }, + leadingIcon = { + Icon( + Icons.Default.Edit, + contentDescription = null, + tint = TextSecondary, + modifier = Modifier.size(18.dp) + ) + }, + onClick = { + showMenu = false + showEditDialog = true + } + ) + DropdownMenuItem( + text = { Text("Remove Device", color = RedVibrant) }, + leadingIcon = { + Icon( + Icons.Default.DeleteOutline, + contentDescription = null, + tint = RedVibrant, + modifier = Modifier.size(18.dp) + ) + }, + onClick = { + showMenu = false + showDeleteConfirm = true + } + ) } } } diff --git a/app/src/main/java/com/expapp/trigger/update/UpdateManager.kt b/app/src/main/java/com/expapp/trigger/update/UpdateManager.kt index 8b23d1d..1abf62f 100644 --- a/app/src/main/java/com/expapp/trigger/update/UpdateManager.kt +++ b/app/src/main/java/com/expapp/trigger/update/UpdateManager.kt @@ -43,6 +43,113 @@ class UpdateManager( mainHandler?.post(block) ?: block() } + /** + * Synchronously checks GitHub Releases API for the latest published release. + * Suitable for use inside background workers (e.g. WorkManager CoroutineWorker). + */ + fun checkForUpdateSync(currentVersionName: String): UpdateCheckResult { + 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() + + return try { + val response = okHttpClient.newCall(request).execute() + response.use { res -> + if (res.code == 404) { + return UpdateCheckResult.UpToDate + } + if (!res.isSuccessful) { + return 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}") + } + } + + private fun parseReleases(body: String, currentVersionName: String): UpdateCheckResult { + return try { + val jsonElement = JsonParser.parseString(body) + val releasesArray = if (jsonElement.isJsonArray) { + jsonElement.asJsonArray + } else { + val arr = com.google.gson.JsonArray() + if (jsonElement.isJsonObject) arr.add(jsonElement.asJsonObject) + arr + } + + var highestRelease: ReleaseInfo? = null + + for (item in releasesArray) { + if (!item.isJsonObject) continue + val json = item.asJsonObject + if (json.has("draft") && json.get("draft").asBoolean) continue + + val tagName = json.get("tag_name")?.asString.orEmpty() + val cleanVer = tagName.removePrefix("v").removePrefix("V").trim() + val title = json.get("name")?.asString?.ifBlank { tagName } ?: tagName + val changelog = json.get("body")?.asString.orEmpty() + + val assets = json.getAsJsonArray("assets") + var apkUrl: String? = null + var apkApiUrl: String? = null + var apkName = "app-release.apk" + var apkSize = 0L + + if (assets != null) { + for (element in assets) { + val assetObj = element.asJsonObject + val name = assetObj.get("name")?.asString.orEmpty() + if (name.endsWith(".apk", ignoreCase = true)) { + apkUrl = assetObj.get("browser_download_url")?.asString + apkApiUrl = assetObj.get("url")?.asString + apkName = name + apkSize = assetObj.get("size")?.asLong ?: 0L + break + } + } + } + + if (!apkUrl.isNullOrBlank()) { + val releaseInfo = ReleaseInfo( + tagName = tagName, + versionName = cleanVer, + title = title, + changelog = changelog, + downloadUrl = apkUrl, + assetApiUrl = apkApiUrl, + apkFileName = apkName, + sizeBytes = apkSize + ) + + if (highestRelease == null || isNewerVersion(cleanVer, highestRelease.versionName)) { + highestRelease = releaseInfo + } + } + } + + if (highestRelease == null) { + return UpdateCheckResult.UpToDate + } + + val cleanLocalVersion = currentVersionName.removePrefix("v").removePrefix("V").trim() + + if (isNewerVersion(highestRelease.versionName, cleanLocalVersion)) { + UpdateCheckResult.UpdateAvailable(highestRelease) + } else { + UpdateCheckResult.UpToDate + } + } catch (e: Exception) { + UpdateCheckResult.Error("Failed to parse update info: ${e.message}") + } + } + /** * Checks GitHub Releases API for the latest published release. */ @@ -81,86 +188,8 @@ class UpdateManager( } val body = res.body?.string().orEmpty() - try { - val jsonElement = JsonParser.parseString(body) - val releasesArray = if (jsonElement.isJsonArray) { - jsonElement.asJsonArray - } else { - // In case a single release object was returned - val arr = com.google.gson.JsonArray() - if (jsonElement.isJsonObject) arr.add(jsonElement.asJsonObject) - arr - } - - var highestRelease: ReleaseInfo? = null - - for (item in releasesArray) { - if (!item.isJsonObject) continue - val json = item.asJsonObject - if (json.has("draft") && json.get("draft").asBoolean) continue - - val tagName = json.get("tag_name")?.asString.orEmpty() - val cleanVer = tagName.removePrefix("v").removePrefix("V").trim() - val title = json.get("name")?.asString?.ifBlank { tagName } ?: tagName - val changelog = json.get("body")?.asString.orEmpty() - - val assets = json.getAsJsonArray("assets") - var apkUrl: String? = null - var apkApiUrl: String? = null - var apkName = "app-release.apk" - var apkSize = 0L - - if (assets != null) { - for (element in assets) { - val assetObj = element.asJsonObject - val name = assetObj.get("name")?.asString.orEmpty() - if (name.endsWith(".apk", ignoreCase = true)) { - apkUrl = assetObj.get("browser_download_url")?.asString - apkApiUrl = assetObj.get("url")?.asString - apkName = name - apkSize = assetObj.get("size")?.asLong ?: 0L - break - } - } - } - - if (!apkUrl.isNullOrBlank()) { - val releaseInfo = ReleaseInfo( - tagName = tagName, - versionName = cleanVer, - title = title, - changelog = changelog, - downloadUrl = apkUrl, - assetApiUrl = apkApiUrl, - apkFileName = apkName, - sizeBytes = apkSize - ) - - if (highestRelease == null || isNewerVersion(cleanVer, highestRelease.versionName)) { - highestRelease = releaseInfo - } - } - } - - if (highestRelease == null) { - runOnMain { callback(UpdateCheckResult.UpToDate) } - return - } - - val cleanLocalVersion = currentVersionName.removePrefix("v").removePrefix("V").trim() - - if (isNewerVersion(highestRelease.versionName, cleanLocalVersion)) { - runOnMain { - callback(UpdateCheckResult.UpdateAvailable(highestRelease)) - } - } else { - runOnMain { callback(UpdateCheckResult.UpToDate) } - } - } catch (e: Exception) { - runOnMain { - callback(UpdateCheckResult.Error("Failed to parse update info: ${e.message}")) - } - } + val result = parseReleases(body, currentVersionName) + runOnMain { callback(result) } } } }) diff --git a/app/src/main/java/com/expapp/trigger/worker/ServerMonitorWorker.kt b/app/src/main/java/com/expapp/trigger/worker/ServerMonitorWorker.kt index e727e04..6cde304 100644 --- a/app/src/main/java/com/expapp/trigger/worker/ServerMonitorWorker.kt +++ b/app/src/main/java/com/expapp/trigger/worker/ServerMonitorWorker.kt @@ -120,18 +120,17 @@ 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 { val updateManager = com.expapp.trigger.update.UpdateManager(appContext) val currentVersion = com.expapp.trigger.BuildConfig.VERSION_NAME - updateManager.checkForUpdate(currentVersion) { result -> - if (result is com.expapp.trigger.update.UpdateCheckResult.UpdateAvailable) { - val newVer = result.info.versionName - val lastNotified = appSettings.lastNotifiedUpdateVersion - if (lastNotified != newVer) { - appSettings.lastNotifiedUpdateVersion = newVer - NotificationHelper.sendUpdateAvailableNotification(appContext, newVer) - } + val result = updateManager.checkForUpdateSync(currentVersion) + if (result is com.expapp.trigger.update.UpdateCheckResult.UpdateAvailable) { + val newVer = result.info.versionName + val lastNotified = appSettings.lastNotifiedUpdateVersion + if (lastNotified != newVer) { + appSettings.lastNotifiedUpdateVersion = newVer + NotificationHelper.sendUpdateAvailableNotification(appContext, newVer) } } } catch (_: Exception) {