Skip to content

add doc changes #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,18 @@ external object firebase {

open class QuerySnapshot {
val docs: Array<DocumentSnapshot>
fun docChanges(): Array<DocumentChange>
val empty: Boolean
val metadata: SnapshotMetadata
}

open class DocumentChange {
val doc: DocumentSnapshot
val newIndex: Int
val oldIndex: Int
val type: String
}

open class DocumentSnapshot {
val id: String
val ref: DocumentReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ actual open class Query(open val android: com.google.firebase.firestore.Query) {
}

actual typealias Direction = com.google.firebase.firestore.Query.Direction
actual typealias ChangeType = com.google.firebase.firestore.DocumentChange.Type

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

Expand All @@ -364,9 +365,22 @@ actual typealias FirestoreExceptionCode = com.google.firebase.firestore.Firebase
actual class QuerySnapshot(val android: com.google.firebase.firestore.QuerySnapshot) {
actual val documents
get() = android.documents.map { DocumentSnapshot(it) }
actual val documentChanges
get() = android.documentChanges.map { DocumentChange(it) }
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(android.metadata)
}

actual class DocumentChange(val android: com.google.firebase.firestore.DocumentChange) {
actual val document: DocumentSnapshot
get() = DocumentSnapshot(android.document)
actual val newIndex: Int
get() = android.newIndex
actual val oldIndex: Int
get() = android.oldIndex
actual val type: ChangeType
get() = android.type
}

@Suppress("UNCHECKED_CAST")
actual class DocumentSnapshot(val android: com.google.firebase.firestore.DocumentSnapshot) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,23 @@ expect enum class Direction {

expect class QuerySnapshot {
val documents: List<DocumentSnapshot>
val documentChanges: List<DocumentChange>
val metadata: SnapshotMetadata
}

expect enum class ChangeType {
ADDED ,
MODIFIED,
REMOVED
}

expect class DocumentChange {
val document: DocumentSnapshot
val newIndex: Int
val oldIndex: Int
val type: ChangeType
}

expect class DocumentSnapshot {

inline fun <reified T> get(field: String): T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package dev.gitlive.firebase.firestore

import cocoapods.FirebaseFirestore.*
import cocoapods.FirebaseFirestore.FIRDocumentChangeType.*
import dev.gitlive.firebase.*
import kotlinx.cinterop.*
import kotlinx.coroutines.CompletableDeferred
Expand Down Expand Up @@ -288,6 +289,12 @@ actual enum class Direction {
DESCENDING
}

actual enum class ChangeType(internal val ios: FIRDocumentChangeType) {
ADDED(FIRDocumentChangeTypeAdded),
MODIFIED(FIRDocumentChangeTypeModified),
REMOVED(FIRDocumentChangeTypeRemoved)
}

fun NSError.toException() = when(domain) {
FIRFirestoreErrorDomain -> when(code) {
FIRFirestoreErrorCodeOK -> FirestoreExceptionCode.OK
Expand Down Expand Up @@ -315,9 +322,22 @@ fun NSError.toException() = when(domain) {
actual class QuerySnapshot(val ios: FIRQuerySnapshot) {
actual val documents
get() = ios.documents.map { DocumentSnapshot(it as FIRDocumentSnapshot) }
actual val documentChanges
get() = ios.documentChanges.map { DocumentChange(it as FIRDocumentChange) }
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(ios.metadata)
}

actual class DocumentChange(val ios: FIRDocumentChange) {
actual val document: DocumentSnapshot
get() = DocumentSnapshot(ios.document)
actual val newIndex: Int
get() = ios.newIndex.toInt()
actual val oldIndex: Int
get() = ios.oldIndex.toInt()
actual val type: ChangeType
get() = ChangeType.values().first { it.ios == ios.type }
}

@Suppress("UNCHECKED_CAST")
actual class DocumentSnapshot(val ios: FIRDocumentSnapshot) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,22 @@ actual val FirebaseFirestoreException.code: FirestoreExceptionCode get() = code
actual class QuerySnapshot(val js: firebase.firestore.QuerySnapshot) {
actual val documents
get() = js.docs.map { DocumentSnapshot(it) }
actual val documentChanges
get() = js.docChanges().map { DocumentChange(it) }
actual val metadata: SnapshotMetadata get() = SnapshotMetadata(js.metadata)
}

actual class DocumentChange(val js: firebase.firestore.DocumentChange) {
actual val document: DocumentSnapshot
get() = DocumentSnapshot(js.doc)
actual val newIndex: Int
get() = js.newIndex
actual val oldIndex: Int
get() = js.oldIndex
actual val type: ChangeType
get() = ChangeType.values().first { it.jsString == js.type }
}

actual class DocumentSnapshot(val js: firebase.firestore.DocumentSnapshot) {

actual val id get() = rethrow { js.id }
Expand Down Expand Up @@ -423,6 +436,12 @@ actual enum class Direction(internal val jsString : String) {
DESCENDING("desc");
}

actual enum class ChangeType(internal val jsString : String) {
ADDED("added"),
MODIFIED("modified"),
REMOVED("removed");
}

inline fun <T, R> T.rethrow(function: T.() -> R): R = dev.gitlive.firebase.firestore.rethrow { function() }

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