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
10
import com.google.firebase.firestore.MetadataChanges
10
11
import com.google.firebase.firestore.firestoreSettings
11
12
import com.google.firebase.firestore.memoryCacheSettings
@@ -21,6 +22,7 @@ import kotlinx.coroutines.flow.callbackFlow
21
22
import kotlinx.coroutines.runBlocking
22
23
import kotlinx.coroutines.tasks.await
23
24
import kotlinx.serialization.Serializable
25
+ import java.lang.IllegalArgumentException
24
26
import java.util.concurrent.ConcurrentHashMap
25
27
import java.util.concurrent.Executor
26
28
import com.google.firebase.firestore.FieldPath as AndroidFieldPath
@@ -35,16 +37,14 @@ actual fun Firebase.firestore(app: FirebaseApp) =
35
37
36
38
val LocalCacheSettings .android: com.google.firebase.firestore.LocalCacheSettings get() = when (this ) {
37
39
is LocalCacheSettings .Persistent -> persistentCacheSettings {
38
- sizeBytes?. let { setSizeBytes(it) }
40
+ setSizeBytes(sizeBytes)
39
41
}
40
42
is LocalCacheSettings .Memory -> memoryCacheSettings {
41
43
setGcSettings(
42
44
when (garbaseCollectorSettings) {
43
- is LocalCacheSettings .Memory .GarbageCollectorSettings .Eager -> memoryEagerGcSettings { }
44
- is LocalCacheSettings .Memory .GarbageCollectorSettings .LRUGC -> memoryLruGcSettings {
45
- garbaseCollectorSettings.sizeBytes?.let {
46
- setSizeBytes(it)
47
- }
45
+ is GarbageCollectorSettings .Eager -> memoryEagerGcSettings { }
46
+ is GarbageCollectorSettings .LRUGC -> memoryLruGcSettings {
47
+ setSizeBytes(garbaseCollectorSettings.sizeBytes)
48
48
}
49
49
}
50
50
)
@@ -56,16 +56,42 @@ private val callbackExecutorMap = ConcurrentHashMap<com.google.firebase.firestor
56
56
57
57
actual class FirebaseFirestore (val android : com.google.firebase.firestore.FirebaseFirestore ) {
58
58
59
- actual data class Settings (
60
- actual val sslEnabled : Boolean? = null ,
61
- actual val host : String? = null ,
62
- actual val cacheSettings : LocalCacheSettings ? = null ,
63
- val callbackExecutor : Executor = TaskExecutors .MAIN_THREAD ,
64
- ) {
65
- actual companion object {
66
- actual fun create (sslEnabled : Boolean? , host : String? , cacheSettings : LocalCacheSettings ? ) = Settings (sslEnabled, host, cacheSettings)
59
+ actual var settings: FirestoreSettings
60
+ get() = with (android.firestoreSettings) {
61
+ FirestoreSettings (
62
+ isSslEnabled,
63
+ host,
64
+ cacheSettings?.let { localCacheSettings ->
65
+ when (localCacheSettings) {
66
+ is com.google.firebase.firestore.MemoryCacheSettings -> {
67
+ 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
+ else -> throw IllegalArgumentException (" Existing settings does not have valid GarbageCollectionSettings" )
71
+ }
72
+ LocalCacheSettings .Memory (garbageCollectionSettings)
73
+ }
74
+ is com.google.firebase.firestore.PersistentCacheSettings -> LocalCacheSettings .Persistent (localCacheSettings.sizeBytes)
75
+ else -> throw IllegalArgumentException (" Existing settings is not of a valid type" )
76
+ }
77
+ } ? : kotlin.run {
78
+ when {
79
+ isPersistenceEnabled -> LocalCacheSettings .Persistent (cacheSizeBytes)
80
+ cacheSizeBytes == FirestoreSettings .CACHE_SIZE_UNLIMITED -> LocalCacheSettings .Memory (GarbageCollectorSettings .Eager )
81
+ else -> LocalCacheSettings .Memory (GarbageCollectorSettings .LRUGC (cacheSizeBytes))
82
+ }
83
+ },
84
+ callbackExecutorMap[android] ? : TaskExecutors .MAIN_THREAD
85
+ )
86
+ }
87
+ set(value) {
88
+ android.firestoreSettings = firestoreSettings {
89
+ isSslEnabled = value.sslEnabled
90
+ host = value.host
91
+ setLocalCacheSettings(value.cacheSettings.android)
92
+ }
93
+ callbackExecutorMap[android] = value.callbackExecutor
67
94
}
68
- }
69
95
70
96
actual fun collection (collectionPath : String ) = CollectionReference (NativeCollectionReference (android.collection(collectionPath)))
71
97
@@ -89,31 +115,6 @@ actual class FirebaseFirestore(val android: com.google.firebase.firestore.Fireba
89
115
android.firestoreSettings = firestoreSettings { }
90
116
}
91
117
92
- actual fun setSettings (settings : Settings ) {
93
- android.firestoreSettings = firestoreSettings {
94
- settings.sslEnabled?.let { isSslEnabled = it }
95
- settings.host?.let { host = it }
96
- settings.cacheSettings?.let { setLocalCacheSettings(it.android) }
97
- }
98
- callbackExecutorMap[android] = settings.callbackExecutor
99
- }
100
-
101
- @Suppress(" DEPRECATION" )
102
- actual fun updateSettings (settings : Settings ) {
103
- android.firestoreSettings = firestoreSettings {
104
- isSslEnabled = settings.sslEnabled ? : android.firestoreSettings.isSslEnabled
105
- host = settings.host ? : android.firestoreSettings.host
106
- val cacheSettings = settings.cacheSettings?.android ? : android.firestoreSettings.cacheSettings
107
- cacheSettings?.let {
108
- setLocalCacheSettings(it)
109
- } ? : kotlin.run {
110
- isPersistenceEnabled = android.firestoreSettings.isPersistenceEnabled
111
- cacheSizeBytes = android.firestoreSettings.cacheSizeBytes
112
- }
113
- }
114
- callbackExecutorMap[android] = settings.callbackExecutor
115
- }
116
-
117
118
actual suspend fun disableNetwork () =
118
119
android.disableNetwork().await().run { }
119
120
@@ -122,6 +123,46 @@ actual class FirebaseFirestore(val android: com.google.firebase.firestore.Fireba
122
123
123
124
}
124
125
126
+ actual data class FirestoreSettings (
127
+ actual val sslEnabled : Boolean ,
128
+ actual val host : String ,
129
+ actual val cacheSettings : LocalCacheSettings ,
130
+ val callbackExecutor : Executor ,
131
+ ) {
132
+
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
142
+ }
143
+
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
149
+
150
+ override fun build () = FirestoreSettings (sslEnabled, host, cacheSettings, callbackExecutor)
151
+ }
152
+ }
153
+
154
+ actual fun firestoreSettings (
155
+ settings : FirestoreSettings ? ,
156
+ builder : FirestoreSettings .Builder .() -> Unit
157
+ ): FirestoreSettings = FirestoreSettings .BuilderImpl ().apply {
158
+ settings?.let {
159
+ sslEnabled = it.sslEnabled
160
+ host = it.host
161
+ cacheSettings = it.cacheSettings
162
+ callbackExecutor = it.callbackExecutor
163
+ }
164
+ }.apply (builder).build()
165
+
125
166
internal val SetOptions .android: com.google.firebase.firestore.SetOptions ? get() = when (this ) {
126
167
is SetOptions .Merge -> com.google.firebase.firestore.SetOptions .merge()
127
168
is SetOptions .Overwrite -> null
0 commit comments