6
6
package dev.gitlive.firebase.firestore
7
7
8
8
import 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
10
12
import 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
16
14
import dev.gitlive.firebase.Firebase
17
15
import dev.gitlive.firebase.FirebaseApp
18
16
import kotlinx.coroutines.channels.ProducerScope
@@ -21,13 +19,16 @@ import kotlinx.coroutines.flow.Flow
21
19
import kotlinx.coroutines.flow.callbackFlow
22
20
import kotlinx.coroutines.runBlocking
23
21
import kotlinx.coroutines.tasks.await
24
- import kotlinx.serialization.Serializable
25
- import java.lang.IllegalArgumentException
26
22
import java.util.concurrent.ConcurrentHashMap
27
23
import java.util.concurrent.Executor
28
24
import com.google.firebase.firestore.FieldPath as AndroidFieldPath
29
25
import com.google.firebase.firestore.Filter as AndroidFilter
30
26
import 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
31
32
32
33
actual val Firebase .firestore get() =
33
34
FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore .getInstance())
@@ -36,14 +37,14 @@ actual fun Firebase.firestore(app: FirebaseApp) =
36
37
FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore .getInstance(app.android))
37
38
38
39
val LocalCacheSettings .android: com.google.firebase.firestore.LocalCacheSettings get() = when (this ) {
39
- is LocalCacheSettings .Persistent -> persistentCacheSettings {
40
+ is LocalCacheSettings .Persistent -> androidPersistentCacheSettings {
40
41
setSizeBytes(sizeBytes)
41
42
}
42
- is LocalCacheSettings .Memory -> memoryCacheSettings {
43
+ is LocalCacheSettings .Memory -> androidMemoryCacheSettings {
43
44
setGcSettings(
44
45
when (garbaseCollectorSettings) {
45
- is GarbageCollectorSettings .Eager -> memoryEagerGcSettings { }
46
- is GarbageCollectorSettings .LRUGC -> memoryLruGcSettings {
46
+ is MemoryGarbageCollectorSettings .Eager -> androidMemoryEagerGcSettings { }
47
+ is MemoryGarbageCollectorSettings .LRUGC -> androidMemoryLruGcSettings {
47
48
setSizeBytes(garbaseCollectorSettings.sizeBytes)
48
49
}
49
50
}
@@ -54,107 +55,116 @@ val LocalCacheSettings.android: com.google.firebase.firestore.LocalCacheSettings
54
55
// Since on iOS Callback threads are set as settings, we store the settings explicitly here as well
55
56
private val callbackExecutorMap = ConcurrentHashMap < com.google.firebase.firestore.FirebaseFirestore , Executor > ()
56
57
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 ) {
58
60
59
- actual var settings: FirestoreSettings
60
- get() = with (android .firestoreSettings) {
61
- FirestoreSettings (
61
+ actual var settings: FirebaseFirestoreSettings
62
+ get() = with (native .firestoreSettings) {
63
+ FirebaseFirestoreSettings (
62
64
isSslEnabled,
63
65
host,
64
66
cacheSettings?.let { localCacheSettings ->
65
67
when (localCacheSettings) {
66
- is com.google.firebase.firestore. MemoryCacheSettings -> {
68
+ is MemoryCacheSettings -> {
67
69
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)
70
72
else -> throw IllegalArgumentException (" Existing settings does not have valid GarbageCollectionSettings" )
71
73
}
72
74
LocalCacheSettings .Memory (garbageCollectionSettings)
73
75
}
74
- is com.google.firebase.firestore.PersistentCacheSettings -> LocalCacheSettings .Persistent (localCacheSettings.sizeBytes)
76
+
77
+ is PersistentCacheSettings -> LocalCacheSettings .Persistent (localCacheSettings.sizeBytes)
75
78
else -> throw IllegalArgumentException (" Existing settings is not of a valid type" )
76
79
}
77
80
} ? : kotlin.run {
78
81
@Suppress(" DEPRECATION" )
79
82
when {
80
83
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))
83
86
}
84
87
},
85
- callbackExecutorMap[android ] ? : TaskExecutors .MAIN_THREAD
88
+ callbackExecutorMap[native ] ? : TaskExecutors .MAIN_THREAD
86
89
)
87
90
}
88
91
set(value) {
89
- android .firestoreSettings = firestoreSettings {
92
+ native .firestoreSettings = androidFirestoreSettings {
90
93
isSslEnabled = value.sslEnabled
91
94
host = value.host
92
95
setLocalCacheSettings(value.cacheSettings.android)
93
96
}
94
- callbackExecutorMap[android ] = value.callbackExecutor
97
+ callbackExecutorMap[native ] = value.callbackExecutor
95
98
}
96
99
97
- actual fun collection (collectionPath : String ) = CollectionReference ( NativeCollectionReference (android .collection(collectionPath) ))
100
+ actual fun collection (collectionPath : String ) = NativeCollectionReference (native .collection(collectionPath))
98
101
99
- actual fun collectionGroup (collectionId : String ) = Query (android .collectionGroup(collectionId).native)
102
+ actual fun collectionGroup (collectionId : String ) = native .collectionGroup(collectionId).native
100
103
101
- actual fun document (documentPath : String ) = DocumentReference ( NativeDocumentReference (android .document(documentPath) ))
104
+ actual fun document (documentPath : String ) = NativeDocumentReference (native .document(documentPath))
102
105
103
- actual fun batch () = WriteBatch ( NativeWriteBatch (android .batch() ))
106
+ actual fun batch () = NativeWriteBatch (native .batch())
104
107
105
108
actual fun setLoggingEnabled (loggingEnabled : Boolean ) =
106
109
com.google.firebase.firestore.FirebaseFirestore .setLoggingEnabled(loggingEnabled)
107
110
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()
110
113
111
114
actual suspend fun clearPersistence () =
112
- android .clearPersistence().await().run { }
115
+ native .clearPersistence().await().run { }
113
116
114
117
actual fun useEmulator (host : String , port : Int ) {
115
- android .useEmulator(host, port)
118
+ native .useEmulator(host, port)
116
119
}
117
120
118
121
actual suspend fun disableNetwork () =
119
- android .disableNetwork().await().run { }
122
+ native .disableNetwork().await().run { }
120
123
121
124
actual suspend fun enableNetwork () =
122
- android .enableNetwork().await().run { }
125
+ native .enableNetwork().await().run { }
123
126
124
127
}
125
128
126
- actual data class FirestoreSettings (
129
+ val FirebaseFirestore .android get() = native
130
+
131
+ actual data class FirebaseFirestoreSettings (
127
132
actual val sslEnabled : Boolean ,
128
133
actual val host : String ,
129
134
actual val cacheSettings : LocalCacheSettings ,
130
135
val callbackExecutor : Executor ,
131
136
) {
132
137
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
142
143
}
143
144
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)
149
159
150
- override fun build () = FirestoreSettings (sslEnabled, host, cacheSettings, callbackExecutor)
160
+ actual fun build (): FirebaseFirestoreSettings = FirebaseFirestoreSettings (sslEnabled, host, cacheSettings, callbackExecutor)
151
161
}
152
162
}
153
163
154
164
actual 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 {
158
168
settings?.let {
159
169
sslEnabled = it.sslEnabled
160
170
host = it.host
0 commit comments