Skip to content

Commit b834be2

Browse files
committed
Implement iOS and JS targets
1 parent dffcd3a commit b834be2

File tree

26 files changed

+742
-222
lines changed

26 files changed

+742
-222
lines changed

firebase-app/src/jsMain/kotlin/dev/gitlive/firebase/firebase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ actual fun Firebase.apps(context: Any?) = firebase.apps.map { FirebaseApp(it) }
3434

3535
private fun FirebaseOptions.toJson() = json(
3636
"apiKey" to apiKey,
37-
"applicationId" to applicationId,
37+
"appId" to applicationId,
3838
"databaseURL" to (databaseUrl ?: undefined),
3939
"storageBucket" to (storageBucket ?: undefined),
4040
"projectId" to (projectId ?: undefined),

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/externals.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,36 @@ external object firebase {
461461
}
462462
}
463463
}
464+
465+
fun remoteConfig(app: App? = definedExternally): remoteConfig.RemoteConfig
466+
467+
object remoteConfig {
468+
interface RemoteConfig {
469+
var defaultConfig: Any
470+
var fetchTimeMillis: Long
471+
var lastFetchStatus: String
472+
val settings: Settings
473+
fun activate(): Promise<Boolean>
474+
fun ensureInitialized(): Promise<Unit>
475+
fun fetch(): Promise<Unit>
476+
fun fetchAndActivate(): Promise<Boolean>
477+
fun getAll(): Json
478+
fun getBoolean(key: String): Boolean
479+
fun getNumber(key: String): Number
480+
fun getString(key: String): String?
481+
fun getValue(key: String): Value
482+
}
483+
484+
interface Settings {
485+
var fetchTimeoutMillis: Number
486+
var minimumFetchIntervalMillis: Number
487+
}
488+
489+
interface Value {
490+
fun asBoolean(): Boolean
491+
fun asNumber(): Number
492+
fun asString(): String?
493+
fun getSource(): String
494+
}
495+
}
464496
}

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/externals2.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ external object database
1818
@JsModule("firebase/firestore")
1919
external object firestore
2020

21+
@JsModule("firebase/remote-config")
22+
external object remoteConfig
23+
2124
typealias SnapshotCallback = (data: firebase.database.DataSnapshot, b: String?) -> Unit
2225

2326
operator fun firebase.functions.HttpsCallable.invoke() = asDynamic()() as Promise<firebase.functions.HttpsCallableResult>

firebase-remoteconfig/build.gradle.kts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,10 @@ kotlin {
9191

9292
js {
9393
useCommonJs()
94-
nodejs {
95-
testTask {
96-
useMocha {
97-
timeout = "5s"
98-
}
99-
}
100-
}
10194
browser {
10295
testTask {
103-
useMocha {
104-
timeout = "5s"
96+
useKarma {
97+
useChromeHeadless()
10598
}
10699
}
107100
}

firebase-remoteconfig/src/androidAndroidTest/kotlin/dev/gitlive/firebase/remoteconfig/RemoteConfig.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package dev.gitlive.firebase.remoteconfig
88
import androidx.test.platform.app.InstrumentationRegistry
99
import kotlinx.coroutines.runBlocking
1010

11-
actual val emulatorHost: String = "10.0.2.2"
12-
1311
actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext
1412

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

firebase-remoteconfig/src/androidMain/kotlin/dev/gitlive/firebase/remoteconfig/RemoteConfigValue.kt renamed to firebase-remoteconfig/src/androidMain/kotlin/dev/gitlive/firebase/remoteconfig/FirebaseRemoteConfigValue.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package dev.gitlive.firebase.remoteconfig
22

33
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
4-
import com.google.firebase.remoteconfig.FirebaseRemoteConfigValue
4+
import com.google.firebase.remoteconfig.FirebaseRemoteConfigValue as AndroidFirebaseRemoteConfigValue
55

6-
actual class RemoteConfigValue internal constructor(private val android: FirebaseRemoteConfigValue) {
6+
actual class FirebaseRemoteConfigValue internal constructor(
7+
private val android: AndroidFirebaseRemoteConfigValue
8+
) {
79
actual fun asBoolean(): Boolean = android.asBoolean()
810
actual fun asByteArray(): ByteArray = android.asByteArray()
911
actual fun asDouble(): Double = android.asDouble()

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

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dev.gitlive.firebase.remoteconfig
2+
3+
import dev.gitlive.firebase.Firebase
4+
import dev.gitlive.firebase.FirebaseApp
5+
import dev.gitlive.firebase.FirebaseException
6+
7+
expect val Firebase.remoteConfig: FirebaseRemoteConfig
8+
9+
expect fun Firebase.remoteConfig(app: FirebaseApp): FirebaseRemoteConfig
10+
11+
expect class FirebaseRemoteConfig {
12+
suspend fun activate(): Boolean
13+
suspend fun ensureInitialized()
14+
suspend fun fetch(minimumFetchIntervalInSeconds: Long? = null)
15+
suspend fun fetchAndActivate(): Boolean
16+
fun getAll(): Map<String, FirebaseRemoteConfigValue>
17+
fun getBoolean(key: String): Boolean
18+
fun getDouble(key: String): Double
19+
fun getInfo(): FirebaseRemoteConfigInfo
20+
fun getKeysByPrefix(prefix: String): Set<String>
21+
fun getLong(key: String): Long
22+
fun getString(key: String): String
23+
fun getValue(key: String): FirebaseRemoteConfigValue
24+
suspend fun reset()
25+
suspend fun setConfigSettings(settings: FirebaseRemoteConfigSettings)
26+
suspend fun setDefaults(defaults: Map<String, Any?>)
27+
}
28+
29+
expect open class FirebaseRemoteConfigException : FirebaseException
30+
expect class FirebaseRemoteConfigClientException : FirebaseRemoteConfigException
31+
expect class FirebaseRemoteConfigFetchThrottledException : FirebaseRemoteConfigException
32+
expect class FirebaseRemoteConfigServerException : FirebaseRemoteConfigException
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.gitlive.firebase.remoteconfig
2+
3+
data class FirebaseRemoteConfigInfo(
4+
val configSettings: FirebaseRemoteConfigSettings,
5+
val fetchTimeMillis: Long,
6+
val lastFetchStatus: FetchStatus,
7+
)
8+
9+
enum class FetchStatus { Success, Failure, Throttled, NoFetchYet }

0 commit comments

Comments
 (0)