diff --git a/app/src/main/java/com/expapp/trigger/data/repository/DeviceRepository.kt b/app/src/main/java/com/expapp/trigger/data/repository/DeviceRepository.kt index d59e2f8..5b22ec9 100644 --- a/app/src/main/java/com/expapp/trigger/data/repository/DeviceRepository.kt +++ b/app/src/main/java/com/expapp/trigger/data/repository/DeviceRepository.kt @@ -54,6 +54,29 @@ class DeviceRepository(private val storage: KeyValueStorage) { addDevice(device) } + fun updateEndpoints( + deviceId: String, + powerOnUrl: String, + powerOffUrl: String, + rebootUrl: String + ): Boolean { + val current = getDevices().toMutableList() + val index = current.indexOfFirst { it.id == deviceId } + if (index != -1) { + val old = current[index] + if (old.powerOnUrl != powerOnUrl || old.powerOffUrl != powerOffUrl || old.rebootUrl != rebootUrl) { + current[index] = old.copy( + powerOnUrl = powerOnUrl, + powerOffUrl = powerOffUrl, + rebootUrl = rebootUrl + ) + saveDevices(current) + return true + } + } + return false + } + private fun saveDevices(devices: List) { val json = gson.toJson(devices) storage.putString(storageKey, json) diff --git a/app/src/main/java/com/expapp/trigger/ui/screens/MainScreen.kt b/app/src/main/java/com/expapp/trigger/ui/screens/MainScreen.kt index 7ab9033..1aa8bc3 100644 --- a/app/src/main/java/com/expapp/trigger/ui/screens/MainScreen.kt +++ b/app/src/main/java/com/expapp/trigger/ui/screens/MainScreen.kt @@ -21,6 +21,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Add import androidx.compose.material.icons.filled.Dns +import androidx.compose.material.icons.filled.Refresh import androidx.compose.material3.CenterAlignedTopAppBar import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon @@ -71,6 +72,59 @@ fun MainScreen( val loadingStates = remember { mutableStateMapOf() } // Device ID -> Pair(StatusMessage, IsSuccess) val statusStates = remember { mutableStateMapOf>() } + var isSyncing by remember { mutableStateOf(false) } + + fun syncEndpoints(showToast: Boolean = false) { + val currentDevices = repository.getDevices() + if (currentDevices.isEmpty()) return + isSyncing = true + var updatedCount = 0 + var completedCount = 0 + val totalToSync = currentDevices.count { it.r2Url.isNotBlank() } + + if (totalToSync == 0) { + isSyncing = false + return + } + + currentDevices.forEach { device -> + if (device.r2Url.isNotBlank()) { + triggerClient.fetchR2Config(device.r2Url) { result -> + completedCount++ + result.onSuccess { endpoints -> + val changed = repository.updateEndpoints( + deviceId = device.id, + powerOnUrl = endpoints.powerOn, + powerOffUrl = endpoints.powerOff, + rebootUrl = endpoints.reboot + ) + if (changed) { + updatedCount++ + devices = repository.getDevices() + } + } + + if (completedCount == totalToSync) { + isSyncing = false + if (showToast) { + if (updatedCount > 0) { + Toast.makeText(context, "Updated endpoints for $updatedCount device(s)", Toast.LENGTH_SHORT).show() + } else { + Toast.makeText(context, "All device endpoints are up to date", Toast.LENGTH_SHORT).show() + } + } else if (updatedCount > 0) { + Toast.makeText(context, "Synced: Updated endpoints from Cloudflare R2", Toast.LENGTH_SHORT).show() + } + } + } + } + } + } + + // Automatically check Cloudflare R2 on every app launch + androidx.compose.runtime.LaunchedEffect(Unit) { + syncEndpoints(showToast = false) + } fun executeAction(device: DeviceConfig, action: TriggerAction) { val actionLabel = when (action) { @@ -122,6 +176,18 @@ fun MainScreen( } }, actions = { + // Manual sync check + IconButton( + onClick = { syncEndpoints(showToast = true) }, + enabled = !isSyncing + ) { + Icon( + imageVector = Icons.Default.Refresh, + contentDescription = "Sync Endpoints", + tint = if (isSyncing) TextSecondary.copy(alpha = 0.5f) else PowerOnGreen + ) + } + // Quick jump to Add Device tab IconButton( onClick = { diff --git a/app/src/test/java/com/expapp/trigger/data/repository/DeviceRepositoryTest.kt b/app/src/test/java/com/expapp/trigger/data/repository/DeviceRepositoryTest.kt index 43dc02b..f1cf89a 100644 --- a/app/src/test/java/com/expapp/trigger/data/repository/DeviceRepositoryTest.kt +++ b/app/src/test/java/com/expapp/trigger/data/repository/DeviceRepositoryTest.kt @@ -64,4 +64,56 @@ class DeviceRepositoryTest { assertEquals(1, remaining.size) assertEquals("2", remaining[0].id) } + + @Test + fun updateEndpoints_whenEndpointsChange_updatesDeviceAndReturnsTrue() { + val device = DeviceConfig( + id = "dev-1", + name = "Debian Server", + node = "debian", + secretKey = "Carbon@12", + r2Url = "https://r2.example.com/config.json", + powerOnUrl = "https://old.url/on", + powerOffUrl = "https://old.url/off", + rebootUrl = "https://old.url/reboot" + ) + repository.addDevice(device) + + val updated = repository.updateEndpoints( + deviceId = "dev-1", + powerOnUrl = "https://new.url/on", + powerOffUrl = "https://new.url/off", + rebootUrl = "https://new.url/reboot" + ) + + assertTrue(updated) + val saved = repository.getDevices().first { it.id == "dev-1" } + assertEquals("https://new.url/on", saved.powerOnUrl) + assertEquals("https://new.url/off", saved.powerOffUrl) + assertEquals("https://new.url/reboot", saved.rebootUrl) + } + + @Test + fun updateEndpoints_whenEndpointsIdentical_returnsFalse() { + val device = DeviceConfig( + id = "dev-1", + name = "Debian Server", + node = "debian", + secretKey = "Carbon@12", + r2Url = "https://r2.example.com/config.json", + powerOnUrl = "https://same.url/on", + powerOffUrl = "https://same.url/off", + rebootUrl = "https://same.url/reboot" + ) + repository.addDevice(device) + + val updated = repository.updateEndpoints( + deviceId = "dev-1", + powerOnUrl = "https://same.url/on", + powerOffUrl = "https://same.url/off", + rebootUrl = "https://same.url/reboot" + ) + + org.junit.Assert.assertFalse(updated) + } }