|
| 1 | +@file:JvmName("android") |
| 2 | +package dev.gitlive.firebase.remoteconfig |
| 3 | + |
| 4 | +import com.google.firebase.remoteconfig.FirebaseRemoteConfigClientException |
| 5 | +import com.google.firebase.remoteconfig.FirebaseRemoteConfigFetchThrottledException |
| 6 | +import com.google.firebase.remoteconfig.FirebaseRemoteConfigServerException |
| 7 | +import com.google.firebase.remoteconfig.ktx.remoteConfig |
| 8 | +import com.google.firebase.remoteconfig.ktx.remoteConfigSettings |
| 9 | +import dev.gitlive.firebase.Firebase |
| 10 | +import dev.gitlive.firebase.FirebaseApp |
| 11 | +import kotlinx.coroutines.tasks.await |
| 12 | +import com.google.firebase.ktx.Firebase as AndroidFirebase |
| 13 | +import com.google.firebase.remoteconfig.FirebaseRemoteConfig as AndroidFirebaseRemoteConfig |
| 14 | +import com.google.firebase.remoteconfig.FirebaseRemoteConfigInfo as AndroidFirebaseRemoteConfigInfo |
| 15 | +import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings as AndroidFirebaseRemoteConfigSettings |
| 16 | + |
| 17 | +actual val Firebase.remoteConfig: FirebaseRemoteConfig |
| 18 | + get() = FirebaseRemoteConfig(AndroidFirebase.remoteConfig) |
| 19 | + |
| 20 | +actual fun Firebase.remoteConfig(app: FirebaseApp): FirebaseRemoteConfig = |
| 21 | + FirebaseRemoteConfig(AndroidFirebase.remoteConfig) |
| 22 | + |
| 23 | +actual class FirebaseRemoteConfig internal constructor(val android: AndroidFirebaseRemoteConfig) { |
| 24 | + actual val all: Map<String, FirebaseRemoteConfigValue> |
| 25 | + get() = android.all.mapValues { FirebaseRemoteConfigValue(it.value) } |
| 26 | + |
| 27 | + actual val info: FirebaseRemoteConfigInfo |
| 28 | + get() = android.info.asCommon() |
| 29 | + |
| 30 | + actual suspend fun settings(init: FirebaseRemoteConfigSettings.() -> Unit) { |
| 31 | + val settings = FirebaseRemoteConfigSettings().apply(init) |
| 32 | + val androidSettings = remoteConfigSettings { |
| 33 | + minimumFetchIntervalInSeconds = settings.minimumFetchIntervalInSeconds |
| 34 | + fetchTimeoutInSeconds = settings.fetchTimeoutInSeconds |
| 35 | + } |
| 36 | + android.setConfigSettingsAsync(androidSettings).await() |
| 37 | + } |
| 38 | + |
| 39 | + actual suspend fun setDefaults(vararg defaults: Pair<String, Any?>) { |
| 40 | + android.setDefaultsAsync(defaults.toMap()).await() |
| 41 | + } |
| 42 | + |
| 43 | + actual suspend fun fetch(minimumFetchIntervalInSeconds: Long?) { |
| 44 | + minimumFetchIntervalInSeconds |
| 45 | + ?.also { android.fetch(it).await() } |
| 46 | + ?: run { android.fetch().await() } |
| 47 | + } |
| 48 | + |
| 49 | + actual suspend fun activate(): Boolean = android.activate().await() |
| 50 | + actual suspend fun ensureInitialized() = android.ensureInitialized().await().let { } |
| 51 | + actual suspend fun fetchAndActivate(): Boolean = android.fetchAndActivate().await() |
| 52 | + actual fun getKeysByPrefix(prefix: String): Set<String> = android.getKeysByPrefix(prefix) |
| 53 | + actual fun getValue(key: String) = FirebaseRemoteConfigValue(android.getValue(key)) |
| 54 | + actual suspend fun reset() = android.reset().await().let { } |
| 55 | + |
| 56 | + private fun AndroidFirebaseRemoteConfigSettings.asCommon(): FirebaseRemoteConfigSettings { |
| 57 | + return FirebaseRemoteConfigSettings( |
| 58 | + fetchTimeoutInSeconds = fetchTimeoutInSeconds, |
| 59 | + minimumFetchIntervalInSeconds = minimumFetchIntervalInSeconds, |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + private fun AndroidFirebaseRemoteConfigInfo.asCommon(): FirebaseRemoteConfigInfo { |
| 64 | + val lastFetchStatus = when (lastFetchStatus) { |
| 65 | + AndroidFirebaseRemoteConfig.LAST_FETCH_STATUS_SUCCESS -> FetchStatus.Success |
| 66 | + AndroidFirebaseRemoteConfig.LAST_FETCH_STATUS_NO_FETCH_YET -> FetchStatus.NoFetchYet |
| 67 | + AndroidFirebaseRemoteConfig.LAST_FETCH_STATUS_FAILURE -> FetchStatus.Failure |
| 68 | + AndroidFirebaseRemoteConfig.LAST_FETCH_STATUS_THROTTLED -> FetchStatus.Throttled |
| 69 | + else -> error("Unknown last fetch status value: $lastFetchStatus") |
| 70 | + } |
| 71 | + |
| 72 | + return FirebaseRemoteConfigInfo( |
| 73 | + configSettings = configSettings.asCommon(), |
| 74 | + fetchTimeMillis = fetchTimeMillis, |
| 75 | + lastFetchStatus = lastFetchStatus |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +actual typealias FirebaseRemoteConfigException = com.google.firebase.remoteconfig.FirebaseRemoteConfigException |
| 81 | +actual typealias FirebaseRemoteConfigClientException = FirebaseRemoteConfigClientException |
| 82 | +actual typealias FirebaseRemoteConfigFetchThrottledException = FirebaseRemoteConfigFetchThrottledException |
| 83 | +actual typealias FirebaseRemoteConfigServerException = FirebaseRemoteConfigServerException |
0 commit comments