Auto connect ble, update realtime setting
This commit is contained in:
+22
@@ -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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user