diff --git a/flow-preferences/src/main/java/com/tfcporciuncula/flow/FlowSharedPreferences.kt b/flow-preferences/src/main/java/com/tfcporciuncula/flow/FlowSharedPreferences.kt index bdfbf2e..cf0f9b5 100644 --- a/flow-preferences/src/main/java/com/tfcporciuncula/flow/FlowSharedPreferences.kt +++ b/flow-preferences/src/main/java/com/tfcporciuncula/flow/FlowSharedPreferences.kt @@ -3,6 +3,7 @@ package com.tfcporciuncula.flow import android.content.SharedPreferences import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow @@ -17,11 +18,16 @@ class FlowSharedPreferences @JvmOverloads constructor( ) { internal val keyFlow: KeyFlow = callbackFlow { - val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> offer(key) } + val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> offerCatching(key) } sharedPreferences.registerOnSharedPreferenceChangeListener(listener) awaitClose { sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener) } } + // https://github.com/Kotlin/kotlinx.coroutines/issues/974 + private fun SendChannel.offerCatching(element: E): Boolean { + return runCatching { offer(element) }.getOrDefault(false) + } + @JvmOverloads fun getInt(key: String, defaultValue: Int = 0): Preference = IntPreference(key, defaultValue, keyFlow, sharedPreferences, coroutineContext)