Half fix StringSet listener

This commit is contained in:
Syer10
2021-06-16 20:22:20 -04:00
parent e499a04fe7
commit aa75678f71
2 changed files with 18 additions and 1 deletions

View File

@@ -30,6 +30,8 @@ internal class JvmPreference<T>(
fun set(key: String, value: T, editor: ObservableSettings)
fun isSet(keys: Set<String>, key: String): Boolean = key in keys
fun keyListener(key: String) = key
}
/**
@@ -83,7 +85,7 @@ internal class JvmPreference<T>(
*/
override fun changes(): Flow<T> {
return callbackFlow {
val listener = preferences.addListener(key) {
val listener = preferences.addListener(adapter.keyListener(key)) {
trySend(get())
}
awaitClose { listener.deactivate() }

View File

@@ -73,6 +73,21 @@ internal object StringSetAdapter : JvmPreference.Adapter<Set<String>> {
override fun set(key: String, value: Set<String>, editor: ObservableSettings) {
editor.encodeValue(SetSerializer(String.serializer()), key, value)
}
/**
* Encoding a string set makes a list of keys and a size key, such as key.size and key.0-size
*/
override fun isSet(keys: Set<String>, key: String): Boolean {
return keys.contains("$key.size")
}
/**
* Watching the regular key doesnt produce updates for a string set for some reason
* TODO make better, doesnt produce updates when you add something and remove something
*/
override fun keyListener(key: String): String {
return "$key.size"
}
}
internal class ObjectAdapter<T>(