Skip to content

Commit 0e9f790

Browse files
committed
Cleanup API
1 parent b834be2 commit 0e9f790

File tree

6 files changed

+79
-76
lines changed

6 files changed

+79
-76
lines changed

firebase-remoteconfig/src/androidMain/kotlin/dev/gitlive/firebase/remoteconfig/FirebaseRemoteConfig.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ actual fun Firebase.remoteConfig(app: FirebaseApp): FirebaseRemoteConfig =
2020
FirebaseRemoteConfig(AndroidFirebase.remoteConfig)
2121

2222
actual class FirebaseRemoteConfig internal constructor(val android: AndroidFirebaseRemoteConfig) {
23-
actual suspend fun setConfigSettings(settings: FirebaseRemoteConfigSettings) {
23+
actual val all: Map<String, FirebaseRemoteConfigValue>
24+
get() = android.all.mapValues { FirebaseRemoteConfigValue(it.value) }
25+
26+
actual val info: FirebaseRemoteConfigInfo
27+
get() = android.info.asCommon()
28+
29+
actual suspend fun settings(build: FirebaseRemoteConfigSettings.() -> Unit) {
30+
val settings = FirebaseRemoteConfigSettings().apply(build)
2431
val androidSettings = remoteConfigSettings {
2532
minimumFetchIntervalInSeconds = settings.minimumFetchIntervalInSeconds
2633
fetchTimeoutInSeconds = settings.fetchTimeoutInSeconds
2734
}
2835
android.setConfigSettingsAsync(androidSettings).await()
2936
}
3037

31-
actual suspend fun setDefaults(defaults: Map<String, Any?>) {
32-
android.setDefaultsAsync(defaults).await()
33-
}
34-
35-
actual fun getAll(): Map<String, FirebaseRemoteConfigValue> {
36-
return android.all.mapValues { FirebaseRemoteConfigValue(it.value) }
38+
actual suspend fun setDefaults(vararg defaults: Pair<String, Any?>) {
39+
android.setDefaultsAsync(defaults.toMap()).await()
3740
}
3841

3942
actual suspend fun fetch(minimumFetchIntervalInSeconds: Long?) {
@@ -47,7 +50,6 @@ actual class FirebaseRemoteConfig internal constructor(val android: AndroidFireb
4750
actual suspend fun fetchAndActivate(): Boolean = android.fetchAndActivate().await()
4851
actual fun getBoolean(key: String) = android.getBoolean(key)
4952
actual fun getDouble(key: String) = android.getDouble(key)
50-
actual fun getInfo() = android.info.asCommon()
5153
actual fun getKeysByPrefix(prefix: String): Set<String> = android.getKeysByPrefix(prefix)
5254
actual fun getLong(key: String) = android.getLong(key)
5355
actual fun getString(key: String) = android.getString(key)

firebase-remoteconfig/src/commonMain/kotlin/dev/gitlive/firebase/remoteconfig/FirebaseRemoteConfig.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ expect val Firebase.remoteConfig: FirebaseRemoteConfig
99
expect fun Firebase.remoteConfig(app: FirebaseApp): FirebaseRemoteConfig
1010

1111
expect class FirebaseRemoteConfig {
12+
val all: Map<String, FirebaseRemoteConfigValue>
13+
val info: FirebaseRemoteConfigInfo
14+
1215
suspend fun activate(): Boolean
1316
suspend fun ensureInitialized()
1417
suspend fun fetch(minimumFetchIntervalInSeconds: Long? = null)
1518
suspend fun fetchAndActivate(): Boolean
16-
fun getAll(): Map<String, FirebaseRemoteConfigValue>
1719
fun getBoolean(key: String): Boolean
1820
fun getDouble(key: String): Double
19-
fun getInfo(): FirebaseRemoteConfigInfo
2021
fun getKeysByPrefix(prefix: String): Set<String>
2122
fun getLong(key: String): Long
2223
fun getString(key: String): String
2324
fun getValue(key: String): FirebaseRemoteConfigValue
2425
suspend fun reset()
25-
suspend fun setConfigSettings(settings: FirebaseRemoteConfigSettings)
26-
suspend fun setDefaults(defaults: Map<String, Any?>)
26+
suspend fun settings(build: FirebaseRemoteConfigSettings.() -> Unit)
27+
suspend fun setDefaults(vararg defaults: Pair<String, Any?>)
2728
}
2829

2930
expect open class FirebaseRemoteConfigException : FirebaseException

firebase-remoteconfig/src/commonMain/kotlin/dev/gitlive/firebase/remoteconfig/FirebaseRemoteConfigSettings.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ private const val CONNECTION_TIMEOUT_IN_SECONDS = 60L
66
private const val DEFAULT_FETCH_INTERVAL_IN_SECONDS = 12 * 3600L
77

88
data class FirebaseRemoteConfigSettings(
9-
val fetchTimeoutInSeconds: Long = CONNECTION_TIMEOUT_IN_SECONDS,
10-
val minimumFetchIntervalInSeconds: Long = DEFAULT_FETCH_INTERVAL_IN_SECONDS,
9+
var fetchTimeoutInSeconds: Long = CONNECTION_TIMEOUT_IN_SECONDS,
10+
var minimumFetchIntervalInSeconds: Long = DEFAULT_FETCH_INTERVAL_IN_SECONDS,
1111
)

firebase-remoteconfig/src/commonTest/kotlin/dev/gitlive/firebase/remoteconfig/FirebaseRemoteConfig.kt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ expect val context: Any
1818
expect fun runTest(test: suspend () -> Unit)
1919

2020
class FirebaseRemoteConfigTest {
21-
private val defaults = mapOf(
21+
private val defaults = arrayOf(
2222
"test_default_boolean" to true,
2323
"test_default_double" to 42.0,
2424
"test_default_long" to 42L,
@@ -52,7 +52,7 @@ class FirebaseRemoteConfigTest {
5252
@Test
5353
fun testGettingValues() = runTest {
5454
val remoteConfig = Firebase.remoteConfig
55-
remoteConfig.setDefaults(defaults)
55+
remoteConfig.setDefaults(*defaults)
5656

5757
assertEquals(true, remoteConfig.getBoolean("test_default_boolean"))
5858
assertEquals(42.0, remoteConfig.getDouble("test_default_double"))
@@ -68,8 +68,8 @@ class FirebaseRemoteConfigTest {
6868

6969
@Test
7070
fun testGetAll() = runTest {
71-
Firebase.remoteConfig.setDefaults(defaults)
72-
val all = Firebase.remoteConfig.getAll()
71+
Firebase.remoteConfig.setDefaults(*defaults)
72+
val all = Firebase.remoteConfig.all
7373
assertEquals(true, all["test_default_boolean"]?.asBoolean())
7474
assertEquals(42.0, all["test_default_double"]?.asDouble())
7575
assertEquals(42L, all["test_default_long"]?.asLong())
@@ -79,7 +79,7 @@ class FirebaseRemoteConfigTest {
7979

8080
@Test
8181
fun testGetKeysByPrefix() = runTest {
82-
Firebase.remoteConfig.setDefaults(defaults)
82+
Firebase.remoteConfig.setDefaults(*defaults)
8383
val keys = Firebase.remoteConfig.getKeysByPrefix("test_default")
8484
assertEquals(
8585
setOf(
@@ -94,26 +94,23 @@ class FirebaseRemoteConfigTest {
9494

9595
@Test
9696
fun testGetInfo() = runTest {
97-
val info = Firebase.remoteConfig.getInfo()
9897
assertEquals(
9998
FirebaseRemoteConfigInfo(
10099
configSettings = FirebaseRemoteConfigSettings(),
101100
fetchTimeMillis = -1,
102101
lastFetchStatus = FetchStatus.NoFetchYet
103102
).toString(),
104-
info.toString()
103+
Firebase.remoteConfig.info.toString()
105104
)
106105
}
107106

108107
@Test
109108
fun testSetConfigSettings() = runTest {
110-
Firebase.remoteConfig.setConfigSettings(
111-
FirebaseRemoteConfigSettings(
112-
fetchTimeoutInSeconds = 42,
113-
minimumFetchIntervalInSeconds = 42
114-
)
115-
)
116-
val info = Firebase.remoteConfig.getInfo()
109+
Firebase.remoteConfig.settings {
110+
fetchTimeoutInSeconds = 42
111+
minimumFetchIntervalInSeconds = 42
112+
}
113+
val info = Firebase.remoteConfig.info
117114
assertEquals(42, info.configSettings.fetchTimeoutInSeconds)
118115
assertEquals(42, info.configSettings.minimumFetchIntervalInSeconds)
119116
}
@@ -125,8 +122,9 @@ class FirebaseRemoteConfigTest {
125122
@Ignore
126123
fun testFetch() = runTest {
127124
val remoteConfig = Firebase.remoteConfig
128-
val settings = FirebaseRemoteConfigSettings(minimumFetchIntervalInSeconds = 60)
129-
remoteConfig.setConfigSettings(settings)
125+
remoteConfig.settings {
126+
minimumFetchIntervalInSeconds = 60
127+
}
130128

131129
remoteConfig.fetch()
132130
remoteConfig.activate()
@@ -140,8 +138,9 @@ class FirebaseRemoteConfigTest {
140138
@Ignore
141139
fun testFetchAndActivate() = runTest {
142140
val remoteConfig = Firebase.remoteConfig
143-
val settings = FirebaseRemoteConfigSettings(minimumFetchIntervalInSeconds = 60)
144-
remoteConfig.setConfigSettings(settings)
141+
remoteConfig.settings {
142+
minimumFetchIntervalInSeconds = 60
143+
}
145144

146145
remoteConfig.fetchAndActivate()
147146

firebase-remoteconfig/src/iosMain/kotlin/dev/gitlive/firebase/remoteconfig/FirebaseRemoteConfig.kt

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ actual fun Firebase.remoteConfig(app: FirebaseApp): FirebaseRemoteConfig =
2323
FirebaseRemoteConfig(FIRRemoteConfig.remoteConfigWithApp(Firebase.app.ios))
2424

2525
actual class FirebaseRemoteConfig internal constructor(val ios: FIRRemoteConfig) {
26+
actual val all: Map<String, FirebaseRemoteConfigValue>
27+
get() {
28+
return listOf(
29+
FIRRemoteConfigSource.FIRRemoteConfigSourceStatic,
30+
FIRRemoteConfigSource.FIRRemoteConfigSourceRemote,
31+
FIRRemoteConfigSource.FIRRemoteConfigSourceDefault,
32+
).map { source ->
33+
val keys = ios.allKeysFromSource(source) as List<String>
34+
keys.map { it to FirebaseRemoteConfigValue(ios.configValueForKey(it, source)) }
35+
}.flatten().toMap()
36+
}
37+
38+
actual val info: FirebaseRemoteConfigInfo
39+
get() {
40+
return FirebaseRemoteConfigInfo(
41+
configSettings = ios.configSettings.asCommon(),
42+
fetchTimeMillis = ios.lastFetchTime
43+
?.timeIntervalSince1970
44+
?.let { it.toLong() * 1000 }
45+
?.takeIf { it > 0 }
46+
?: -1L,
47+
lastFetchStatus = ios.lastFetchStatus.asCommon()
48+
)
49+
}
50+
2651
actual suspend fun activate(): Boolean = ios.awaitResult { activateWithCompletion(it) }
2752

2853
actual suspend fun ensureInitialized() =
@@ -45,36 +70,13 @@ actual class FirebaseRemoteConfig internal constructor(val ios: FIRRemoteConfig)
4570
return status == FIRRemoteConfigFetchAndActivateStatus.FIRRemoteConfigFetchAndActivateStatusSuccessFetchedFromRemote
4671
}
4772

48-
actual fun getAll(): Map<String, FirebaseRemoteConfigValue> {
49-
return listOf(
50-
FIRRemoteConfigSource.FIRRemoteConfigSourceStatic,
51-
FIRRemoteConfigSource.FIRRemoteConfigSourceRemote,
52-
FIRRemoteConfigSource.FIRRemoteConfigSourceDefault,
53-
).map { source ->
54-
val keys = ios.allKeysFromSource(source) as List<String>
55-
keys.map { it to FirebaseRemoteConfigValue(ios.configValueForKey(it, source)) }
56-
}.flatten().toMap()
57-
}
58-
5973
actual fun getBoolean(key: String): Boolean = ios.configValueForKey(key).boolValue
6074
actual fun getDouble(key: String): Double = ios.configValueForKey(key).numberValue.doubleValue
6175
actual fun getLong(key: String): Long = ios.configValueForKey(key).numberValue.longValue
6276
actual fun getString(key: String): String = ios.configValueForKey(key).stringValue ?: ""
6377

64-
actual fun getInfo(): FirebaseRemoteConfigInfo {
65-
return FirebaseRemoteConfigInfo(
66-
configSettings = ios.configSettings.asCommon(),
67-
fetchTimeMillis = ios.lastFetchTime
68-
?.timeIntervalSince1970
69-
?.let { it.toLong() * 1000 }
70-
?.takeIf { it > 0 }
71-
?: -1L,
72-
lastFetchStatus = ios.lastFetchStatus.asCommon()
73-
)
74-
}
75-
7678
actual fun getKeysByPrefix(prefix: String): Set<String> =
77-
getAll().keys.filter { it.startsWith(prefix) }.toSet()
79+
all.keys.filter { it.startsWith(prefix) }.toSet()
7880

7981
actual fun getValue(key: String): FirebaseRemoteConfigValue =
8082
FirebaseRemoteConfigValue(ios.configValueForKey(key))
@@ -83,16 +85,17 @@ actual class FirebaseRemoteConfig internal constructor(val ios: FIRRemoteConfig)
8385
// not implemented for iOS target
8486
}
8587

86-
actual suspend fun setConfigSettings(settings: FirebaseRemoteConfigSettings) {
88+
actual suspend fun settings(build: FirebaseRemoteConfigSettings.() -> Unit) {
89+
val settings = FirebaseRemoteConfigSettings().apply(build)
8790
val iosSettings = FIRRemoteConfigSettings().apply {
8891
minimumFetchInterval = settings.minimumFetchIntervalInSeconds.toDouble()
8992
fetchTimeout = settings.fetchTimeoutInSeconds.toDouble()
9093
}
9194
ios.setConfigSettings(iosSettings)
9295
}
9396

94-
actual suspend fun setDefaults(defaults: Map<String, Any?>) {
95-
ios.setDefaults(defaults as Map<Any?, Any?>)
97+
actual suspend fun setDefaults(vararg defaults: Pair<String, Any?>) {
98+
ios.setDefaults(defaults.toMap())
9699
}
97100

98101
private fun FIRRemoteConfigSettings.asCommon(): FirebaseRemoteConfigSettings {

firebase-remoteconfig/src/jsMain/kotlin/dev/gitlive/firebase/remoteconfig/FirebaseRemoteConfig.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ actual fun Firebase.remoteConfig(app: FirebaseApp): FirebaseRemoteConfig = rethr
1919
}
2020

2121
actual class FirebaseRemoteConfig internal constructor(val js: firebase.remoteConfig.RemoteConfig) {
22+
actual val all: Map<String, FirebaseRemoteConfigValue>
23+
get() = rethrow { getAllKeys().map { it to getValue(it) }.toMap() }
24+
25+
actual val info: FirebaseRemoteConfigInfo
26+
get() = rethrow {
27+
FirebaseRemoteConfigInfo(
28+
configSettings = js.settings.toSettings(),
29+
fetchTimeMillis = js.fetchTimeMillis,
30+
lastFetchStatus = js.lastFetchStatus.toFetchStatus()
31+
)
32+
}
2233

2334
actual suspend fun activate(): Boolean = rethrow { js.activate().await() }
2435
actual suspend fun ensureInitialized(): Unit = rethrow { js.activate().await() }
@@ -27,11 +38,6 @@ actual class FirebaseRemoteConfig internal constructor(val js: firebase.remoteCo
2738
rethrow { js.fetch().await() }
2839

2940
actual suspend fun fetchAndActivate(): Boolean = rethrow { js.fetchAndActivate().await() }
30-
31-
actual fun getAll(): Map<String, FirebaseRemoteConfigValue> = rethrow {
32-
getAllKeys().map { it to getValue(it) }.toMap()
33-
}
34-
3541
actual fun getBoolean(key: String): Boolean = rethrow { js.getBoolean(key) }
3642
actual fun getDouble(key: String): Double = rethrow { js.getNumber(key).toDouble() }
3743
actual fun getLong(key: String): Long = rethrow { js.getNumber(key).toLong() }
@@ -41,14 +47,6 @@ actual class FirebaseRemoteConfig internal constructor(val js: firebase.remoteCo
4147
FirebaseRemoteConfigValue(js.getValue(key))
4248
}
4349

44-
actual fun getInfo(): FirebaseRemoteConfigInfo = rethrow {
45-
FirebaseRemoteConfigInfo(
46-
configSettings = js.settings.toSettings(),
47-
fetchTimeMillis = js.fetchTimeMillis,
48-
lastFetchStatus = js.lastFetchStatus.toFetchStatus()
49-
)
50-
}
51-
5250
actual fun getKeysByPrefix(prefix: String): Set<String> {
5351
return getAllKeys().filter { it.startsWith(prefix) }.toSet()
5452
}
@@ -62,16 +60,16 @@ actual class FirebaseRemoteConfig internal constructor(val js: firebase.remoteCo
6260
// not implemented for JS target
6361
}
6462

65-
actual suspend fun setConfigSettings(settings: FirebaseRemoteConfigSettings) {
63+
actual suspend fun settings(build: FirebaseRemoteConfigSettings.() -> Unit) {
64+
val settings = FirebaseRemoteConfigSettings().apply(build)
6665
js.settings.apply {
6766
fetchTimeoutMillis = settings.fetchTimeoutInSeconds * 1000
6867
minimumFetchIntervalMillis = settings.minimumFetchIntervalInSeconds * 1000
6968
}
7069
}
7170

72-
actual suspend fun setDefaults(defaults: Map<String, Any?>) = rethrow {
73-
val pairs = defaults.map { it.key to it.value }.toTypedArray()
74-
js.defaultConfig = json(*pairs)
71+
actual suspend fun setDefaults(vararg defaults: Pair<String, Any?>) = rethrow {
72+
js.defaultConfig = json(*defaults)
7573
}
7674

7775
private fun firebase.remoteConfig.Settings.toSettings(): FirebaseRemoteConfigSettings {

0 commit comments

Comments
 (0)