-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2287 from CruGlobal/firebaseConfig
GT-2494 Fix FirebaseRemoteConfig.get*Flow() extension methods
- Loading branch information
Showing
1 changed file
with
23 additions
and
6 deletions.
There are no files selected for viewing
29 changes: 23 additions & 6 deletions
29
...ain/kotlin/org/ccci/gto/android/common/firebase/remoteconfig/FirebaseRemoteConfig+Flow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
package org.ccci.gto.android.common.firebase.remoteconfig | ||
|
||
import com.google.firebase.remoteconfig.ConfigUpdate | ||
import com.google.firebase.remoteconfig.ConfigUpdateListener | ||
import com.google.firebase.remoteconfig.FirebaseRemoteConfig | ||
import com.google.firebase.remoteconfig.configUpdates | ||
import kotlinx.coroutines.flow.map | ||
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.channels.trySendBlocking | ||
import kotlinx.coroutines.flow.callbackFlow | ||
|
||
fun FirebaseRemoteConfig.getBooleanFlow(key: String) = configUpdates.map { getBoolean(key) } | ||
fun FirebaseRemoteConfig.getDoubleFlow(key: String) = configUpdates.map { getDouble(key) } | ||
fun FirebaseRemoteConfig.getLongFlow(key: String) = configUpdates.map { getLong(key) } | ||
fun FirebaseRemoteConfig.getStringFlow(key: String) = configUpdates.map { getString(key) } | ||
private fun <T> FirebaseRemoteConfig.configFlow(block: () -> T) = callbackFlow { | ||
val registration = addOnConfigUpdateListener( | ||
object : ConfigUpdateListener { | ||
override fun onUpdate(configUpdate: ConfigUpdate) = schedule { trySendBlocking(block()) } | ||
override fun onError(error: FirebaseRemoteConfigException) = Unit | ||
} | ||
) | ||
|
||
trySend(block()) | ||
|
||
awaitClose { registration.remove() } | ||
} | ||
|
||
fun FirebaseRemoteConfig.getBooleanFlow(key: String) = configFlow { getBoolean(key) } | ||
fun FirebaseRemoteConfig.getDoubleFlow(key: String) = configFlow { getDouble(key) } | ||
fun FirebaseRemoteConfig.getLongFlow(key: String) = configFlow { getLong(key) } | ||
fun FirebaseRemoteConfig.getStringFlow(key: String) = configFlow { getString(key) } |