Skip to content

Commit a89cd93

Browse files
authoredJan 2, 2021
add doc changes (#128)
* add doc changes * fix ios comp errors * fix ios comp errors
1 parent 2a673e4 commit a89cd93

File tree

5 files changed

+75
-0
lines changed
  • firebase-common/src/jsMain/kotlin/dev/gitlive/firebase
  • firebase-firestore/src

5 files changed

+75
-0
lines changed
 

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

+8
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,18 @@ external object firebase {
370370

371371
open class QuerySnapshot {
372372
val docs: Array<DocumentSnapshot>
373+
fun docChanges(): Array<DocumentChange>
373374
val empty: Boolean
374375
val metadata: SnapshotMetadata
375376
}
376377

378+
open class DocumentChange {
379+
val doc: DocumentSnapshot
380+
val newIndex: Int
381+
val oldIndex: Int
382+
val type: String
383+
}
384+
377385
open class DocumentSnapshot {
378386
val id: String
379387
val ref: DocumentReference

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

+14
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ actual open class Query(open val android: com.google.firebase.firestore.Query) {
346346
}
347347

348348
actual typealias Direction = com.google.firebase.firestore.Query.Direction
349+
actual typealias ChangeType = com.google.firebase.firestore.DocumentChange.Type
349350

350351
actual class CollectionReference(override val android: com.google.firebase.firestore.CollectionReference) : Query(android) {
351352

@@ -372,9 +373,22 @@ actual typealias FirestoreExceptionCode = com.google.firebase.firestore.Firebase
372373
actual class QuerySnapshot(val android: com.google.firebase.firestore.QuerySnapshot) {
373374
actual val documents
374375
get() = android.documents.map { DocumentSnapshot(it) }
376+
actual val documentChanges
377+
get() = android.documentChanges.map { DocumentChange(it) }
375378
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(android.metadata)
376379
}
377380

381+
actual class DocumentChange(val android: com.google.firebase.firestore.DocumentChange) {
382+
actual val document: DocumentSnapshot
383+
get() = DocumentSnapshot(android.document)
384+
actual val newIndex: Int
385+
get() = android.newIndex
386+
actual val oldIndex: Int
387+
get() = android.oldIndex
388+
actual val type: ChangeType
389+
get() = android.type
390+
}
391+
378392
@Suppress("UNCHECKED_CAST")
379393
actual class DocumentSnapshot(val android: com.google.firebase.firestore.DocumentSnapshot) {
380394

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

+14
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,23 @@ expect enum class Direction {
165165

166166
expect class QuerySnapshot {
167167
val documents: List<DocumentSnapshot>
168+
val documentChanges: List<DocumentChange>
168169
val metadata: SnapshotMetadata
169170
}
170171

172+
expect enum class ChangeType {
173+
ADDED ,
174+
MODIFIED,
175+
REMOVED
176+
}
177+
178+
expect class DocumentChange {
179+
val document: DocumentSnapshot
180+
val newIndex: Int
181+
val oldIndex: Int
182+
val type: ChangeType
183+
}
184+
171185
expect class DocumentSnapshot {
172186

173187
inline fun <reified T> get(field: String): T

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

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package dev.gitlive.firebase.firestore
66

77
import cocoapods.FirebaseFirestore.*
8+
import cocoapods.FirebaseFirestore.FIRDocumentChangeType.*
89
import dev.gitlive.firebase.*
910
import kotlinx.cinterop.*
1011
import kotlinx.coroutines.CompletableDeferred
@@ -296,6 +297,12 @@ actual enum class Direction {
296297
DESCENDING
297298
}
298299

300+
actual enum class ChangeType(internal val ios: FIRDocumentChangeType) {
301+
ADDED(FIRDocumentChangeTypeAdded),
302+
MODIFIED(FIRDocumentChangeTypeModified),
303+
REMOVED(FIRDocumentChangeTypeRemoved)
304+
}
305+
299306
fun NSError.toException() = when(domain) {
300307
FIRFirestoreErrorDomain -> when(code) {
301308
FIRFirestoreErrorCodeOK -> FirestoreExceptionCode.OK
@@ -323,9 +330,22 @@ fun NSError.toException() = when(domain) {
323330
actual class QuerySnapshot(val ios: FIRQuerySnapshot) {
324331
actual val documents
325332
get() = ios.documents.map { DocumentSnapshot(it as FIRDocumentSnapshot) }
333+
actual val documentChanges
334+
get() = ios.documentChanges.map { DocumentChange(it as FIRDocumentChange) }
326335
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(ios.metadata)
327336
}
328337

338+
actual class DocumentChange(val ios: FIRDocumentChange) {
339+
actual val document: DocumentSnapshot
340+
get() = DocumentSnapshot(ios.document)
341+
actual val newIndex: Int
342+
get() = ios.newIndex.toInt()
343+
actual val oldIndex: Int
344+
get() = ios.oldIndex.toInt()
345+
actual val type: ChangeType
346+
get() = ChangeType.values().first { it.ios == ios.type }
347+
}
348+
329349
@Suppress("UNCHECKED_CAST")
330350
actual class DocumentSnapshot(val ios: FIRDocumentSnapshot) {
331351

‎firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+19
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,22 @@ actual val FirebaseFirestoreException.code: FirestoreExceptionCode get() = code
346346
actual class QuerySnapshot(val js: firebase.firestore.QuerySnapshot) {
347347
actual val documents
348348
get() = js.docs.map { DocumentSnapshot(it) }
349+
actual val documentChanges
350+
get() = js.docChanges().map { DocumentChange(it) }
349351
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(js.metadata)
350352
}
351353

354+
actual class DocumentChange(val js: firebase.firestore.DocumentChange) {
355+
actual val document: DocumentSnapshot
356+
get() = DocumentSnapshot(js.doc)
357+
actual val newIndex: Int
358+
get() = js.newIndex
359+
actual val oldIndex: Int
360+
get() = js.oldIndex
361+
actual val type: ChangeType
362+
get() = ChangeType.values().first { it.jsString == js.type }
363+
}
364+
352365
actual class DocumentSnapshot(val js: firebase.firestore.DocumentSnapshot) {
353366

354367
actual val id get() = rethrow { js.id }
@@ -425,6 +438,12 @@ actual enum class Direction(internal val jsString : String) {
425438
DESCENDING("desc");
426439
}
427440

441+
actual enum class ChangeType(internal val jsString : String) {
442+
ADDED("added"),
443+
MODIFIED("modified"),
444+
REMOVED("removed");
445+
}
446+
428447
inline fun <T, R> T.rethrow(function: T.() -> R): R = dev.gitlive.firebase.firestore.rethrow { function() }
429448

430449
inline fun <R> rethrow(function: () -> R): R {

0 commit comments

Comments
 (0)
Failed to load comments.