From 2350152ec26600efff78d716eb0db66e95172ec3 Mon Sep 17 00:00:00 2001 From: Bas Buijsen Date: Thu, 6 Jun 2024 21:25:25 +0200 Subject: [PATCH 1/6] add analytics to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 44af9afdc..ee138e76d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ The following libraries are available for the various Firebase products. | Service or Product | Gradle Dependency | API Coverage | |---------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| [Analytics](https://firebase.google.com/docs/analytics) | [`dev.gitlive:firebase-analytics:1.12.0`](https://search.maven.org/artifact/dev.gitlive/firebase-analytics/1.12.0/pom) | [![80%](https://img.shields.io/badge/-80%25-green?style=flat-square)](/firebase-auth/src/commonMain/kotlin/dev/gitlive/firebase/auth/auth.kt) | | [Authentication](https://firebase.google.com/docs/auth) | [`dev.gitlive:firebase-auth:1.12.0`](https://search.maven.org/artifact/dev.gitlive/firebase-auth/1.12.0/pom) | [![80%](https://img.shields.io/badge/-80%25-green?style=flat-square)](/firebase-auth/src/commonMain/kotlin/dev/gitlive/firebase/auth/auth.kt) | | [Realtime Database](https://firebase.google.com/docs/database) | [`dev.gitlive:firebase-database:1.12.0`](https://search.maven.org/artifact/dev.gitlive/firebase-database/1.12.0/pom) | [![70%](https://img.shields.io/badge/-70%25-orange?style=flat-square)](/firebase-database/src/commonMain/kotlin/dev/gitlive/firebase/database/database.kt) | | [Cloud Firestore](https://firebase.google.com/docs/firestore) | [`dev.gitlive:firebase-firestore:1.12.0`](https://search.maven.org/artifact/dev.gitlive/firebase-firestore/1.12.0/pom) | [![60%](https://img.shields.io/badge/-60%25-orange?style=flat-square)](/firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt) | From 95e11ec9a2e628006c2a0186b00f6560d10383fb Mon Sep 17 00:00:00 2001 From: Bas Buijsen Date: Sun, 16 Jun 2024 13:28:53 +0200 Subject: [PATCH 2/6] add messaging functionality --- .../gitlive/firebase/messaging/messaging.kt | 9 +++++ .../gitlive/firebase/messaging/messaging.kt | 20 ++++++++++- .../gitlive/firebase/messaging/messaging.kt | 34 +++++++++++++++++++ .../firebase/messaging/externals/messaging.kt | 8 ++++- .../gitlive/firebase/messaging/messaging.kt | 15 ++++++++ .../gitlive/firebase/messaging/messaging.kt | 14 +++++++- 6 files changed, 97 insertions(+), 3 deletions(-) diff --git a/firebase-messaging/src/androidMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt b/firebase-messaging/src/androidMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt index 17619585f..5fba05385 100644 --- a/firebase-messaging/src/androidMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt +++ b/firebase-messaging/src/androidMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt @@ -2,10 +2,19 @@ package dev.gitlive.firebase.messaging import dev.gitlive.firebase.Firebase +import kotlinx.coroutines.tasks.await actual val Firebase.messaging: FirebaseMessaging get() = FirebaseMessaging(com.google.firebase.messaging.FirebaseMessaging.getInstance()) actual class FirebaseMessaging(val android: com.google.firebase.messaging.FirebaseMessaging) { + actual fun subscribeToTopic(topic: String) { + android.subscribeToTopic(topic) + } + actual fun unsubscribeFromTopic(topic: String) { + android.unsubscribeFromTopic(topic) + } + + actual suspend fun getToken(): String = android.token.await() } \ No newline at end of file diff --git a/firebase-messaging/src/commonMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt b/firebase-messaging/src/commonMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt index ea8fa6e3d..3312bb795 100644 --- a/firebase-messaging/src/commonMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt +++ b/firebase-messaging/src/commonMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt @@ -6,4 +6,22 @@ import dev.gitlive.firebase.FirebaseApp /** Returns the [FirebaseMessaging] instance of the default [FirebaseApp]. */ expect val Firebase.messaging: FirebaseMessaging -expect class FirebaseMessaging +expect class FirebaseMessaging { + /** + * Subscribe to a topic. + * @param topic The topic to subscribe to. + */ + fun subscribeToTopic(topic: String) + + /** + * Unsubscribe from a topic. + * @param topic The topic to unsubscribe from. + */ + fun unsubscribeFromTopic(topic: String) + + /** + * Get FCM token for client + * @return [String] FCM token + */ + suspend fun getToken(): String +} \ No newline at end of file diff --git a/firebase-messaging/src/iosMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt b/firebase-messaging/src/iosMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt index 2f1897c52..b2511d86a 100644 --- a/firebase-messaging/src/iosMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt +++ b/firebase-messaging/src/iosMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt @@ -2,10 +2,44 @@ package dev.gitlive.firebase.messaging import cocoapods.FirebaseMessaging.FIRMessaging import dev.gitlive.firebase.Firebase +import kotlinx.coroutines.CompletableDeferred +import platform.Foundation.NSError actual val Firebase.messaging: FirebaseMessaging get() = FirebaseMessaging(FIRMessaging.messaging()) actual class FirebaseMessaging(val ios: FIRMessaging) { + actual fun subscribeToTopic(topic: String) { + ios.subscribeToTopic(topic) + } + actual fun unsubscribeFromTopic(topic: String) { + ios.unsubscribeFromTopic(topic) + } + + actual suspend fun getToken(): String = awaitResult { ios.tokenWithCompletion(it) } +} + +suspend inline fun T.await(function: T.(callback: (NSError?) -> Unit) -> Unit) { + val job = CompletableDeferred() + function { error -> + if(error == null) { + job.complete(Unit) + } else { + job.completeExceptionally(Exception(error.toString())) + } + } + job.await() +} + +suspend inline fun T.awaitResult(function: T.(callback: (R?, NSError?) -> Unit) -> Unit): R { + val job = CompletableDeferred() + function { result, error -> + if(error == null) { + job.complete(result) + } else { + job.completeExceptionally(Exception(error.toString())) + } + } + return job.await() as R } \ No newline at end of file diff --git a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/externals/messaging.kt b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/externals/messaging.kt index 506c8ef8f..94f6ff8ca 100644 --- a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/externals/messaging.kt +++ b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/externals/messaging.kt @@ -1,9 +1,15 @@ package dev.gitlive.firebase.messaging.externals import dev.gitlive.firebase.externals.FirebaseApp +import kotlin.js.Promise external fun getMessaging( app: FirebaseApp? = definedExternally, ): Messaging -external interface Messaging \ No newline at end of file +external fun getToken(messaging: Messaging = definedExternally, options: dynamic = definedExternally): Promise + +external interface Messaging { + fun subscribeToTopic(tokens: Array, topic: String): Promise + fun unsubscribeFromTopic(tokens: Array, topic: String): Promise +} \ No newline at end of file diff --git a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt index dc809b68f..4ec260ebb 100644 --- a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt +++ b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt @@ -3,10 +3,25 @@ package dev.gitlive.firebase.messaging import dev.gitlive.firebase.Firebase import dev.gitlive.firebase.messaging.externals.Messaging import dev.gitlive.firebase.messaging.externals.getMessaging +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.await +import kotlinx.coroutines.launch actual val Firebase.messaging: FirebaseMessaging get() = FirebaseMessaging(getMessaging()) actual class FirebaseMessaging(val js: Messaging) { + actual fun subscribeToTopic(topic: String) { + GlobalScope.launch { + js.subscribeToTopic(arrayOf(getToken()), topic) + } + } + actual fun unsubscribeFromTopic(topic: String) { + GlobalScope.launch { + js.unsubscribeFromTopic(arrayOf(getToken()), topic) + } + } + + actual suspend fun getToken(): String = dev.gitlive.firebase.messaging.externals.getToken(js).await() } \ No newline at end of file diff --git a/firebase-messaging/src/jvmMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt b/firebase-messaging/src/jvmMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt index 6e13a52b2..6b8f3b82a 100644 --- a/firebase-messaging/src/jvmMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt +++ b/firebase-messaging/src/jvmMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt @@ -6,4 +6,16 @@ import dev.gitlive.firebase.Firebase actual val Firebase.messaging: FirebaseMessaging get() = TODO("Not yet implemented") -actual class FirebaseMessaging \ No newline at end of file +actual class FirebaseMessaging { + actual fun subscribeToTopic(topic: String) { + TODO("Not yet implemented") + } + + actual fun unsubscribeFromTopic(topic: String) { + TODO("Not yet implemented") + } + + actual suspend fun getToken(): String { + TODO("Not yet implemented") + } +} \ No newline at end of file From ddb283fec1557c1760d82d22ba013288d4459ad2 Mon Sep 17 00:00:00 2001 From: Bas Buijsen Date: Sun, 16 Jun 2024 17:20:24 +0200 Subject: [PATCH 3/6] fix js implementation --- .../dev/gitlive/firebase/messaging/messaging.kt | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt index 4ec260ebb..fda5570da 100644 --- a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt +++ b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt @@ -3,24 +3,21 @@ package dev.gitlive.firebase.messaging import dev.gitlive.firebase.Firebase import dev.gitlive.firebase.messaging.externals.Messaging import dev.gitlive.firebase.messaging.externals.getMessaging -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.await -import kotlinx.coroutines.launch + actual val Firebase.messaging: FirebaseMessaging get() = FirebaseMessaging(getMessaging()) actual class FirebaseMessaging(val js: Messaging) { actual fun subscribeToTopic(topic: String) { - GlobalScope.launch { - js.subscribeToTopic(arrayOf(getToken()), topic) - } + // This is not supported in the JS SDK + // https://firebase.google.com/docs/reference/js/messaging_.md#@firebase/messaging } actual fun unsubscribeFromTopic(topic: String) { - GlobalScope.launch { - js.unsubscribeFromTopic(arrayOf(getToken()), topic) - } + // This is not supported in the JS SDK + // https://firebase.google.com/docs/reference/js/messaging_.md#@firebase/messaging } actual suspend fun getToken(): String = dev.gitlive.firebase.messaging.externals.getToken(js).await() From 24268d510e25be50d7c4c9d5423d58993fba9a98 Mon Sep 17 00:00:00 2001 From: Bas Buijsen Date: Sun, 16 Jun 2024 17:21:42 +0200 Subject: [PATCH 4/6] small testing improvement --- .../kotlin/dev/gitlive/firebase/storage/storage.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/firebase-storage/src/commonTest/kotlin/dev/gitlive/firebase/storage/storage.kt b/firebase-storage/src/commonTest/kotlin/dev/gitlive/firebase/storage/storage.kt index cf5fbc127..009c9027e 100644 --- a/firebase-storage/src/commonTest/kotlin/dev/gitlive/firebase/storage/storage.kt +++ b/firebase-storage/src/commonTest/kotlin/dev/gitlive/firebase/storage/storage.kt @@ -64,14 +64,14 @@ class FirebaseStorageTest { @Test fun testUploadShouldNotCrash() = runBlockingTest { val data = createTestData() - val ref = storage.reference("test").child("testFile.txt") + val ref = storage.reference("test").child("testUploadShouldNotCrash.txt") ref.putData(data) } @Test fun testUploadMetadata() = runBlockingTest { val data = createTestData() - val ref = storage.reference("test").child("testFile.txt") + val ref = storage.reference("test").child("testUploadMetadata.txt") val metadata = storageMetadata { contentType = "text/plain" } @@ -87,7 +87,7 @@ class FirebaseStorageTest { @Test fun testUploadCustomMetadata() = runBlockingTest { val data = createTestData() - val ref = storage.reference("test").child("testFile.txt") + val ref = storage.reference("test").child("testUploadCustomMetadata.txt") val metadata = storageMetadata { contentType = "text/plain" setCustomMetadata("key", "value") From 29000d28cb21f9eff28e32596a7e75c024173d82 Mon Sep 17 00:00:00 2001 From: Bas Buijsen Date: Tue, 18 Jun 2024 16:42:24 +0200 Subject: [PATCH 5/6] throw error for subscribing on js --- .../jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt index fda5570da..223a15338 100644 --- a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt +++ b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/messaging.kt @@ -13,11 +13,13 @@ actual class FirebaseMessaging(val js: Messaging) { actual fun subscribeToTopic(topic: String) { // This is not supported in the JS SDK // https://firebase.google.com/docs/reference/js/messaging_.md#@firebase/messaging + throw NotImplementedError("Subscribing to topics is not supported in the JS SDK") } actual fun unsubscribeFromTopic(topic: String) { // This is not supported in the JS SDK // https://firebase.google.com/docs/reference/js/messaging_.md#@firebase/messaging + throw NotImplementedError("Unsubscribing from topics is not supported in the JS SDK") } actual suspend fun getToken(): String = dev.gitlive.firebase.messaging.externals.getToken(js).await() From b6ccc3ea93faf2fe91aab83f186531bf8c9dc462 Mon Sep 17 00:00:00 2001 From: Bas Buijsen Date: Wed, 19 Jun 2024 13:14:10 +0200 Subject: [PATCH 6/6] remove unavailable external functions --- .../dev/gitlive/firebase/messaging/externals/messaging.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/externals/messaging.kt b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/externals/messaging.kt index 94f6ff8ca..5e6c48100 100644 --- a/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/externals/messaging.kt +++ b/firebase-messaging/src/jsMain/kotlin/dev/gitlive/firebase/messaging/externals/messaging.kt @@ -9,7 +9,4 @@ external fun getMessaging( external fun getToken(messaging: Messaging = definedExternally, options: dynamic = definedExternally): Promise -external interface Messaging { - fun subscribeToTopic(tokens: Array, topic: String): Promise - fun unsubscribeFromTopic(tokens: Array, topic: String): Promise -} \ No newline at end of file +external interface Messaging \ No newline at end of file