66package dev.gitlive.firebase.firestore
77
88import com.google.android.gms.tasks.TaskExecutors
9- import com.google.firebase.firestore.FirebaseFirestoreSettings
9+ import com.google.firebase.firestore.MemoryCacheSettings
10+ import com.google.firebase.firestore.MemoryEagerGcSettings
11+ import com.google.firebase.firestore.MemoryLruGcSettings
1012import com.google.firebase.firestore.MetadataChanges
11- import com.google.firebase.firestore.firestoreSettings
12- import com.google.firebase.firestore.memoryCacheSettings
13- import com.google.firebase.firestore.memoryEagerGcSettings
14- import com.google.firebase.firestore.memoryLruGcSettings
15- import com.google.firebase.firestore.persistentCacheSettings
13+ import com.google.firebase.firestore.PersistentCacheSettings
1614import dev.gitlive.firebase.Firebase
1715import dev.gitlive.firebase.FirebaseApp
1816import kotlinx.coroutines.channels.ProducerScope
@@ -21,13 +19,16 @@ import kotlinx.coroutines.flow.Flow
2119import kotlinx.coroutines.flow.callbackFlow
2220import kotlinx.coroutines.runBlocking
2321import kotlinx.coroutines.tasks.await
24- import kotlinx.serialization.Serializable
25- import java.lang.IllegalArgumentException
2622import java.util.concurrent.ConcurrentHashMap
2723import java.util.concurrent.Executor
2824import com.google.firebase.firestore.FieldPath as AndroidFieldPath
2925import com.google.firebase.firestore.Filter as AndroidFilter
3026import com.google.firebase.firestore.Query as AndroidQuery
27+ import com.google.firebase.firestore.firestoreSettings as androidFirestoreSettings
28+ import com.google.firebase.firestore.memoryCacheSettings as androidMemoryCacheSettings
29+ import com.google.firebase.firestore.memoryEagerGcSettings as androidMemoryEagerGcSettings
30+ import com.google.firebase.firestore.memoryLruGcSettings as androidMemoryLruGcSettings
31+ import com.google.firebase.firestore.persistentCacheSettings as androidPersistentCacheSettings
3132
3233actual val Firebase .firestore get() =
3334 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore .getInstance())
@@ -36,14 +37,14 @@ actual fun Firebase.firestore(app: FirebaseApp) =
3637 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore .getInstance(app.android))
3738
3839val LocalCacheSettings .android: com.google.firebase.firestore.LocalCacheSettings get() = when (this ) {
39- is LocalCacheSettings .Persistent -> persistentCacheSettings {
40+ is LocalCacheSettings .Persistent -> androidPersistentCacheSettings {
4041 setSizeBytes(sizeBytes)
4142 }
42- is LocalCacheSettings .Memory -> memoryCacheSettings {
43+ is LocalCacheSettings .Memory -> androidMemoryCacheSettings {
4344 setGcSettings(
4445 when (garbaseCollectorSettings) {
45- is GarbageCollectorSettings .Eager -> memoryEagerGcSettings { }
46- is GarbageCollectorSettings .LRUGC -> memoryLruGcSettings {
46+ is MemoryGarbageCollectorSettings .Eager -> androidMemoryEagerGcSettings { }
47+ is MemoryGarbageCollectorSettings .LRUGC -> androidMemoryLruGcSettings {
4748 setSizeBytes(garbaseCollectorSettings.sizeBytes)
4849 }
4950 }
@@ -54,107 +55,116 @@ val LocalCacheSettings.android: com.google.firebase.firestore.LocalCacheSettings
5455// Since on iOS Callback threads are set as settings, we store the settings explicitly here as well
5556private val callbackExecutorMap = ConcurrentHashMap < com.google.firebase.firestore.FirebaseFirestore , Executor > ()
5657
57- actual class FirebaseFirestore (val android : com.google.firebase.firestore.FirebaseFirestore ) {
58+ actual typealias NativeFirebaseFirestore = com.google.firebase.firestore.FirebaseFirestore
59+ actual internal class NativeFirebaseFirestoreWrapper actual constructor(actual val native : NativeFirebaseFirestore ) {
5860
59- actual var settings: FirestoreSettings
60- get() = with (android .firestoreSettings) {
61- FirestoreSettings (
61+ actual var settings: FirebaseFirestoreSettings
62+ get() = with (native .firestoreSettings) {
63+ FirebaseFirestoreSettings (
6264 isSslEnabled,
6365 host,
6466 cacheSettings?.let { localCacheSettings ->
6567 when (localCacheSettings) {
66- is com.google.firebase.firestore. MemoryCacheSettings -> {
68+ is MemoryCacheSettings -> {
6769 val garbageCollectionSettings = when (val settings = localCacheSettings.garbageCollectorSettings) {
68- is com.google.firebase.firestore. MemoryEagerGcSettings -> GarbageCollectorSettings .Eager
69- is com.google.firebase.firestore. MemoryLruGcSettings -> GarbageCollectorSettings .LRUGC (settings.sizeBytes)
70+ is MemoryEagerGcSettings -> MemoryGarbageCollectorSettings .Eager
71+ is MemoryLruGcSettings -> MemoryGarbageCollectorSettings .LRUGC (settings.sizeBytes)
7072 else -> throw IllegalArgumentException (" Existing settings does not have valid GarbageCollectionSettings" )
7173 }
7274 LocalCacheSettings .Memory (garbageCollectionSettings)
7375 }
74- is com.google.firebase.firestore.PersistentCacheSettings -> LocalCacheSettings .Persistent (localCacheSettings.sizeBytes)
76+
77+ is PersistentCacheSettings -> LocalCacheSettings .Persistent (localCacheSettings.sizeBytes)
7578 else -> throw IllegalArgumentException (" Existing settings is not of a valid type" )
7679 }
7780 } ? : kotlin.run {
7881 @Suppress(" DEPRECATION" )
7982 when {
8083 isPersistenceEnabled -> LocalCacheSettings .Persistent (cacheSizeBytes)
81- cacheSizeBytes == FirestoreSettings .CACHE_SIZE_UNLIMITED -> LocalCacheSettings .Memory (GarbageCollectorSettings .Eager )
82- else -> LocalCacheSettings .Memory (GarbageCollectorSettings .LRUGC (cacheSizeBytes))
84+ cacheSizeBytes == FirebaseFirestoreSettings .CACHE_SIZE_UNLIMITED -> LocalCacheSettings .Memory (MemoryGarbageCollectorSettings .Eager )
85+ else -> LocalCacheSettings .Memory (MemoryGarbageCollectorSettings .LRUGC (cacheSizeBytes))
8386 }
8487 },
85- callbackExecutorMap[android ] ? : TaskExecutors .MAIN_THREAD
88+ callbackExecutorMap[native ] ? : TaskExecutors .MAIN_THREAD
8689 )
8790 }
8891 set(value) {
89- android .firestoreSettings = firestoreSettings {
92+ native .firestoreSettings = androidFirestoreSettings {
9093 isSslEnabled = value.sslEnabled
9194 host = value.host
9295 setLocalCacheSettings(value.cacheSettings.android)
9396 }
94- callbackExecutorMap[android ] = value.callbackExecutor
97+ callbackExecutorMap[native ] = value.callbackExecutor
9598 }
9699
97- actual fun collection (collectionPath : String ) = CollectionReference ( NativeCollectionReference (android .collection(collectionPath) ))
100+ actual fun collection (collectionPath : String ) = NativeCollectionReference (native .collection(collectionPath))
98101
99- actual fun collectionGroup (collectionId : String ) = Query (android .collectionGroup(collectionId).native)
102+ actual fun collectionGroup (collectionId : String ) = native .collectionGroup(collectionId).native
100103
101- actual fun document (documentPath : String ) = DocumentReference ( NativeDocumentReference (android .document(documentPath) ))
104+ actual fun document (documentPath : String ) = NativeDocumentReference (native .document(documentPath))
102105
103- actual fun batch () = WriteBatch ( NativeWriteBatch (android .batch() ))
106+ actual fun batch () = NativeWriteBatch (native .batch())
104107
105108 actual fun setLoggingEnabled (loggingEnabled : Boolean ) =
106109 com.google.firebase.firestore.FirebaseFirestore .setLoggingEnabled(loggingEnabled)
107110
108- actual suspend fun <T > runTransaction (func : suspend Transaction .() -> T ): T =
109- android .runTransaction { runBlocking { Transaction ( NativeTransaction (it) ).func() } }.await()
111+ actual suspend fun <T > runTransaction (func : suspend NativeTransaction .() -> T ): T =
112+ native .runTransaction { runBlocking { NativeTransaction (it).func() } }.await()
110113
111114 actual suspend fun clearPersistence () =
112- android .clearPersistence().await().run { }
115+ native .clearPersistence().await().run { }
113116
114117 actual fun useEmulator (host : String , port : Int ) {
115- android .useEmulator(host, port)
118+ native .useEmulator(host, port)
116119 }
117120
118121 actual suspend fun disableNetwork () =
119- android .disableNetwork().await().run { }
122+ native .disableNetwork().await().run { }
120123
121124 actual suspend fun enableNetwork () =
122- android .enableNetwork().await().run { }
125+ native .enableNetwork().await().run { }
123126
124127}
125128
126- actual data class FirestoreSettings (
129+ val FirebaseFirestore .android get() = native
130+
131+ actual data class FirebaseFirestoreSettings (
127132 actual val sslEnabled : Boolean ,
128133 actual val host : String ,
129134 actual val cacheSettings : LocalCacheSettings ,
130135 val callbackExecutor : Executor ,
131136) {
132137
133- actual companion object {}
134-
135- actual interface Builder {
136- actual var sslEnabled: Boolean
137- actual var host: String
138- actual var cacheSettings: LocalCacheSettings
139- var callbackExecutor: Executor
140-
141- actual fun build (): FirestoreSettings
138+ actual companion object {
139+ actual val CACHE_SIZE_UNLIMITED : Long = - 1L
140+ internal actual val DEFAULT_HOST : String = " firestore.googleapis.com"
141+ internal actual val MINIMUM_CACHE_BYTES : Long = 1 * 1024 * 1024
142+ internal actual val DEFAULT_CACHE_SIZE_BYTES : Long = 100 * 1024 * 1024
142143 }
143144
144- internal class BuilderImpl : Builder {
145- override var sslEnabled: Boolean = true
146- override var host: String = FirestoreSettings .DEFAULT_HOST
147- override var cacheSettings: LocalCacheSettings = LocalCacheSettings .Persistent (CACHE_SIZE_UNLIMITED )
148- override var callbackExecutor: Executor = TaskExecutors .MAIN_THREAD
145+ actual class Builder internal constructor(
146+ actual var sslEnabled : Boolean ,
147+ actual var host : String ,
148+ actual var cacheSettings : LocalCacheSettings ,
149+ var callbackExecutor : Executor ,
150+ ) {
151+
152+ actual constructor () : this (
153+ true ,
154+ DEFAULT_HOST ,
155+ persistentCacheSettings { },
156+ TaskExecutors .MAIN_THREAD
157+ )
158+ actual constructor (settings: FirebaseFirestoreSettings ) : this (settings.sslEnabled, settings.host, settings.cacheSettings, settings.callbackExecutor)
149159
150- override fun build () = FirestoreSettings (sslEnabled, host, cacheSettings, callbackExecutor)
160+ actual fun build (): FirebaseFirestoreSettings = FirebaseFirestoreSettings (sslEnabled, host, cacheSettings, callbackExecutor)
151161 }
152162}
153163
154164actual fun firestoreSettings (
155- settings : FirestoreSettings ? ,
156- builder : FirestoreSettings .Builder .() -> Unit
157- ): FirestoreSettings = FirestoreSettings . BuilderImpl ().apply {
165+ settings : FirebaseFirestoreSettings ? ,
166+ builder : FirebaseFirestoreSettings .Builder .() -> Unit
167+ ): FirebaseFirestoreSettings = FirebaseFirestoreSettings . Builder ().apply {
158168 settings?.let {
159169 sslEnabled = it.sslEnabled
160170 host = it.host
0 commit comments