Skip to content

Commit e902480

Browse files
vanniktechnbransby
andauthored
FireStore: clearPersistence(). (#32)
* FireStore: clearPersistence(). * add tests for clearPersistence * add gmcSenderId * fix gmcSenderId usage in ios * fix gmcSenderId usage in js Co-authored-by: Nicholas Bransby-Williams <[email protected]>
1 parent e8ce97d commit e902480

File tree

13 files changed

+96
-4
lines changed
  • firebase-app/src
  • firebase-common/src/jsMain/kotlin/dev/gitlive/firebase
  • firebase-firestore/src

13 files changed

+96
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ private fun FirebaseOptions.toAndroid() = com.google.firebase.FirebaseOptions.Bu
4646
.setGaTrackingId(gaTrackingId)
4747
.setStorageBucket(storageBucket)
4848
.setProjectId(projectId)
49+
.setGcmSenderId(gcmSenderId)
4950
.build()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ data class FirebaseOptions(
4949
val databaseUrl: String? = null,
5050
val gaTrackingId: String? = null,
5151
val storageBucket: String? = null,
52-
val projectId: String? = null
52+
val projectId: String? = null,
53+
val gcmSenderId: String? = null
5354
)
5455

5556
expect open class FirebaseException : Exception

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ actual fun Firebase.apps(context: Any?) = FIRApp.allApps()
3838
.values
3939
.map { FirebaseApp(it as FIRApp) }
4040

41-
private fun FirebaseOptions.toIos() = FIROptions(this@toIos.applicationId, "846484016111").apply {
41+
private fun FirebaseOptions.toIos() = FIROptions(this@toIos.applicationId, this@toIos.gcmSenderId ?: "").apply {
4242
APIKey = this@toIos.apiKey
4343
databaseURL = this@toIos.databaseUrl
4444
trackingID = this@toIos.gaTrackingId

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ private fun FirebaseOptions.toJson() = json(
3838
"databaseURL" to (databaseUrl ?: undefined),
3939
"storageBucket" to (storageBucket ?: undefined),
4040
"projectId" to (projectId ?: undefined),
41-
"gaTrackingId" to (gaTrackingId ?: undefined)
41+
"gaTrackingId" to (gaTrackingId ?: undefined),
42+
"messagingSenderId" to (gcmSenderId ?: undefined)
4243
)
4344

4445
actual open class FirebaseException(code: String?, cause: Throwable) : Exception("$code: ${cause.message}", cause)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ external object firebase {
169169
fun doc(documentPath: String): DocumentReference
170170
fun settings(settings: Json)
171171
fun enablePersistence(): Promise<Unit>
172+
fun clearPersistence(): Promise<Unit>
172173
}
173174

174175
open class FieldPath constructor(fieldNames: Array<out String>)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:JvmName("tests")
6+
package dev.gitlive.firebase.firestore
7+
8+
import androidx.test.platform.app.InstrumentationRegistry
9+
import kotlinx.coroutines.runBlocking
10+
11+
actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext
12+
13+
actual fun runTest(test: suspend () -> Unit) = runBlocking { test() }

firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ actual class FirebaseFirestore(val android: com.google.firebase.firestore.Fireba
4242

4343
actual suspend fun <T> runTransaction(func: suspend Transaction.() -> T) =
4444
android.runTransaction { runBlocking { Transaction(it).func() } }.await()
45+
46+
actual suspend fun clearPersistence() =
47+
android.clearPersistence().await()
48+
.run { Unit }
4549
}
4650

4751
actual class WriteBatch(val android: com.google.firebase.firestore.WriteBatch) {

firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ expect class FirebaseFirestore {
2424
fun document(documentPath: String): DocumentReference
2525
fun batch(): WriteBatch
2626
fun setLoggingEnabled(loggingEnabled: Boolean)
27+
suspend fun clearPersistence()
2728
suspend fun <T> runTransaction(func: suspend Transaction.() -> T): T
2829
}
2930

firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,33 @@
33
*/
44

55
package dev.gitlive.firebase.firestore
6+
7+
import dev.gitlive.firebase.*
8+
import kotlin.test.*
9+
10+
expect val context: Any
11+
expect fun runTest(test: suspend () -> Unit)
12+
13+
class FirebaseFirestoreTest {
14+
15+
@BeforeTest
16+
fun initializeFirebase() {
17+
Firebase
18+
.takeIf { Firebase.apps(context).isEmpty() }
19+
?.initialize(
20+
context,
21+
FirebaseOptions(
22+
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
23+
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
24+
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
25+
storageBucket = "fir-kotlin-sdk.appspot.com",
26+
projectId = "fir-kotlin-sdk"
27+
)
28+
)
29+
}
30+
31+
@Test
32+
fun testClearPersistence() = runTest {
33+
Firebase.firestore.clearPersistence()
34+
}
35+
}

firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ actual class FirebaseFirestore(val ios: FIRFirestore) {
3838

3939
actual suspend fun <T> runTransaction(func: suspend Transaction.() -> T) =
4040
awaitResult<Any?> { ios.runTransactionWithBlock({ transaction, error -> runBlocking { Transaction(transaction!!).func() } }, it) } as T
41+
42+
actual suspend fun clearPersistence() =
43+
await { ios.clearPersistenceWithCompletion(it) }
4144
}
4245

4346
actual class WriteBatch(val ios: FIRWriteBatch) {

0 commit comments

Comments
 (0)