Skip to content

Commit 96df881

Browse files
committed
PR remarks
1 parent d7b3719 commit 96df881

File tree

7 files changed

+329
-226
lines changed

7 files changed

+329
-226
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ actual class FirebaseDatabase internal constructor(val ios: FIRDatabase) {
7171
ios.setPersistenceCacheSizeBytes(cacheSizeInBytes.toULong())
7272
}
7373

74-
fun setCallbackQueue(callbackQueue: dispatch_queue_t) {
75-
ios.callbackQueue = callbackQueue
76-
}
77-
7874
actual fun setLoggingEnabled(enabled: Boolean) =
7975
FIRDatabase.setLoggingEnabled(enabled)
8076

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

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
package dev.gitlive.firebase.firestore
77

88
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
1012
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
1614
import dev.gitlive.firebase.Firebase
1715
import dev.gitlive.firebase.FirebaseApp
1816
import kotlinx.coroutines.channels.ProducerScope
@@ -21,13 +19,16 @@ import kotlinx.coroutines.flow.Flow
2119
import kotlinx.coroutines.flow.callbackFlow
2220
import kotlinx.coroutines.runBlocking
2321
import kotlinx.coroutines.tasks.await
24-
import kotlinx.serialization.Serializable
25-
import java.lang.IllegalArgumentException
2622
import java.util.concurrent.ConcurrentHashMap
2723
import java.util.concurrent.Executor
2824
import com.google.firebase.firestore.FieldPath as AndroidFieldPath
2925
import com.google.firebase.firestore.Filter as AndroidFilter
3026
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
3132

3233
actual 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

3839
val 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
5556
private 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

154164
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 {
158168
settings?.let {
159169
sslEnabled = it.sslEnabled
160170
host = it.host
Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,69 @@
11
package dev.gitlive.firebase.firestore
22

3-
sealed class LocalCacheSettings {
3+
sealed interface LocalCacheSettings {
44

5-
internal companion object {
6-
// Firestore cache defaults to 100MB
7-
const val DEFAULT_CACHE_SIZE = 100L*1024L*1024L
8-
}
9-
10-
data class Persistent(val sizeBytes: Long) : LocalCacheSettings() {
5+
data class Persistent internal constructor(val sizeBytes: Long) : LocalCacheSettings {
116

127
companion object {
13-
fun newBuilder(): Builder = BuilderImpl()
8+
fun newBuilder(): Builder = Builder()
149
}
1510

16-
interface Builder {
17-
var sizeBytes: Long
18-
fun build(): Persistent
19-
}
20-
21-
private class BuilderImpl(
22-
override var sizeBytes: Long = DEFAULT_CACHE_SIZE
23-
) : Builder {
24-
override fun build(): Persistent = Persistent(sizeBytes)
11+
class Builder internal constructor() {
12+
var sizeBytes: Long = FirebaseFirestoreSettings.DEFAULT_CACHE_SIZE_BYTES
13+
fun build(): Persistent = Persistent(sizeBytes)
2514
}
2615
}
27-
data class Memory(val garbaseCollectorSettings: GarbageCollectorSettings) : LocalCacheSettings() {
16+
data class Memory internal constructor(val garbaseCollectorSettings: MemoryGarbageCollectorSettings) : LocalCacheSettings {
2817

2918
companion object {
30-
fun newBuilder(): Builder = BuilderImpl()
19+
fun newBuilder(): Builder = Builder()
3120
}
3221

33-
interface Builder {
34-
35-
var gcSettings: GarbageCollectorSettings
22+
class Builder internal constructor() {
3623

37-
fun build(): Memory
38-
}
24+
var gcSettings: MemoryGarbageCollectorSettings = MemoryGarbageCollectorSettings.Eager.newBuilder().build()
3925

40-
private class BuilderImpl(
41-
override var gcSettings: GarbageCollectorSettings = GarbageCollectorSettings.Eager
42-
) : Builder {
43-
override fun build(): Memory = Memory(gcSettings)
26+
fun build(): Memory = Memory(gcSettings)
4427
}
4528
}
4629
}
4730

48-
sealed class GarbageCollectorSettings {
49-
data object Eager : GarbageCollectorSettings() {
31+
typealias PersistentCacheSettings = LocalCacheSettings.Persistent
32+
typealias MemoryCacheSettings = LocalCacheSettings.Memory
5033

51-
fun newBuilder(): Builder = BuilderImpl()
34+
sealed interface MemoryGarbageCollectorSettings {
35+
data object Eager : MemoryGarbageCollectorSettings {
5236

53-
interface Builder {
54-
fun build(): Eager
55-
}
37+
fun newBuilder(): Builder = Builder()
5638

57-
private class BuilderImpl : Builder {
58-
override fun build(): Eager = Eager
39+
class Builder internal constructor() {
40+
fun build(): Eager = Eager
5941
}
6042
}
61-
data class LRUGC(val sizeBytes: Long) : GarbageCollectorSettings() {
43+
data class LRUGC internal constructor(val sizeBytes: Long) : MemoryGarbageCollectorSettings {
6244

6345
companion object {
64-
fun newBuilder(): Builder = BuilderImpl()
46+
fun newBuilder(): Builder = Builder()
6547
}
6648

67-
interface Builder {
68-
var sizeBytes: Long
69-
fun build(): LRUGC
70-
}
71-
72-
private class BuilderImpl(
73-
override var sizeBytes: Long = LocalCacheSettings.DEFAULT_CACHE_SIZE
74-
) : Builder {
75-
override fun build(): LRUGC = LRUGC(sizeBytes)
49+
class Builder internal constructor() {
50+
var sizeBytes: Long = FirebaseFirestoreSettings.DEFAULT_CACHE_SIZE_BYTES
51+
fun build(): LRUGC = LRUGC(sizeBytes)
7652
}
7753
}
7854
}
7955

56+
typealias MemoryEagerGcSettings = MemoryGarbageCollectorSettings.Eager
57+
typealias MemoryLruGcSettings = MemoryGarbageCollectorSettings.LRUGC
58+
8059
fun memoryCacheSettings(builder: LocalCacheSettings.Memory.Builder.() -> Unit): LocalCacheSettings.Memory =
8160
LocalCacheSettings.Memory.newBuilder().apply(builder).build()
8261

83-
fun memoryEagerGcSettings(builder: GarbageCollectorSettings.Eager.Builder.() -> Unit) =
84-
GarbageCollectorSettings.Eager.newBuilder().apply(builder).build()
62+
fun memoryEagerGcSettings(builder: MemoryGarbageCollectorSettings.Eager.Builder.() -> Unit) =
63+
MemoryGarbageCollectorSettings.Eager.newBuilder().apply(builder).build()
8564

86-
fun memoryLruGcSettings(builder: GarbageCollectorSettings.LRUGC.Builder.() -> Unit) =
87-
GarbageCollectorSettings.LRUGC.newBuilder().apply(builder).build()
65+
fun memoryLruGcSettings(builder: MemoryGarbageCollectorSettings.LRUGC.Builder.() -> Unit) =
66+
MemoryGarbageCollectorSettings.LRUGC.newBuilder().apply(builder).build()
8867

8968
fun persistentCacheSettings(builder: LocalCacheSettings.Persistent.Builder.() -> Unit) =
9069
LocalCacheSettings.Persistent.newBuilder().apply(builder).build()

0 commit comments

Comments
 (0)