Auto connect ble, update realtime setting

This commit is contained in:
2026-05-30 14:52:37 +07:00
parent e55fcab158
commit 07611f3ace
9 changed files with 118 additions and 38 deletions
@@ -1,7 +1,14 @@
package com.digitoolsolutions.app.torquevaultkmp
import com.digitoolsolutions.app.torquevaultkmp.data.storage.AppStorage
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map
import platform.Foundation.NSNotificationCenter
import platform.Foundation.NSUserDefaults
import platform.Foundation.NSUserDefaultsDidChangeNotification
import kotlin.reflect.KClass
class AppStoragePlatform : AppStorage {
@@ -28,4 +35,19 @@ class AppStoragePlatform : AppStorage {
override fun remove(key: String) {
defaults.removeObjectForKey(key)
}
override fun <T : Any> observe(key: String, type: KClass<T>): Flow<T> = callbackFlow {
val observer = NSNotificationCenter.defaultCenter.addObserverForName(
name = NSUserDefaultsDidChangeNotification,
`object` = null,
queue = null
) { _ ->
load(key, type)?.let { trySend(it) }
}
// emit initial value
load(key, type)?.let { trySend(it) }
awaitClose {
NSNotificationCenter.defaultCenter.removeObserver(observer)
}
}.filterNotNull().map { it }
}