From 82d1f5cd7f8adf5c4828059bfd77a230baf36e44 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Mon, 12 Jan 2026 09:02:47 -0300 Subject: [PATCH] Refactor relay status notification updates - Use `applicationIOScope` to collect relay flows with a 300ms debounce. - Update notifications using current flow values for available and connected relays. - Add `@OptIn(FlowPreview::class)` to `AmberRelayStats`. - Minor code formatting and import cleanup. --- .../nostrsigner/relays/AmberRelayStat.kt | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/relays/AmberRelayStat.kt b/app/src/main/java/com/greenart7c3/nostrsigner/relays/AmberRelayStat.kt index 0f9cab5e..1098bee4 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/relays/AmberRelayStat.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/relays/AmberRelayStat.kt @@ -25,7 +25,9 @@ import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch +@OptIn(FlowPreview::class) class AmberRelayStats( client: NostrClient, val appContext: Context, @@ -44,8 +46,8 @@ class AmberRelayStats( val notificationManager = NotificationManagerCompat.from(appContext) if (ActivityCompat.checkSelfPermission(appContext, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) { createNotification( - it.second, - it.first, + client.connectedRelaysFlow().value, + client.availableRelaysFlow().value, false, )?.let { notification -> Log.d(Amber.TAG, "updateNotification") @@ -89,6 +91,19 @@ class AmberRelayStats( notificationManager.createNotificationChannel(channel) notificationManager.createNotificationChannelGroup(statusGroup) notificationManager.createNotificationChannel(statusChannel) + + Amber.instance.applicationIOScope.launch { + Amber.instance.client.availableRelaysFlow().debounce(300).collect { + available = it + updateNotification() + } + } + Amber.instance.applicationIOScope.launch { + Amber.instance.client.connectedRelaysFlow().debounce(300).collect { + connected = it + updateNotification() + } + } } private val innerCache = mutableMapOf() @@ -143,7 +158,9 @@ class AmberRelayStats( connected + ping + sent + received + failed + error } - if (message == oldMessage && oldMessage.isNotBlank() && !forceCreate) return null + if (message == oldMessage && oldMessage.isNotBlank() && !forceCreate) { + return null + } this.oldMessage = message val contentIntent = Intent(appContext, MainActivity::class.java)