Skip to content

Commit

Permalink
Merge pull request #2287 from CruGlobal/firebaseConfig
Browse files Browse the repository at this point in the history
GT-2494 Fix FirebaseRemoteConfig.get*Flow() extension methods
  • Loading branch information
frett authored Jan 10, 2025
2 parents 23eb284 + 12662f6 commit 0e49777
Showing 1 changed file with 23 additions and 6 deletions.
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) }

0 comments on commit 0e49777

Please sign in to comment.