Skip to content
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

trying to implement count function #702

Closed
Closed
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
@@ -1,6 +1,8 @@
package dev.gitlive.firebase.firestore.internal

import com.google.android.gms.tasks.TaskExecutors
import com.google.android.gms.tasks.Tasks
import com.google.firebase.firestore.AggregateSource
import com.google.firebase.firestore.FieldPath
import com.google.firebase.firestore.MetadataChanges
import com.google.firebase.firestore.Query
Expand Down Expand Up @@ -41,6 +43,12 @@ internal actual open class NativeQueryWrapper internal actual constructor(actual
actual suspend fun get(source: Source): QuerySnapshot =
QuerySnapshot(native.get(source.toAndroidSource()).await())

actual suspend fun count(): Long {
val aggregateQuery = native.count()
val task = aggregateQuery.get(AggregateSource.SERVER)
return Tasks.await(task).count
}

actual fun where(filter: Filter) = native.where(filter.toAndroidFilter())

private fun Filter.toAndroidFilter(): com.google.firebase.firestore.Filter = when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal expect open class NativeQueryWrapper internal constructor(native: Nativ
val snapshots: Flow<QuerySnapshot>
fun snapshots(includeMetadataChanges: Boolean = false): Flow<QuerySnapshot>
suspend fun get(source: Source = Source.DEFAULT): QuerySnapshot
suspend fun count(): Long

fun where(filter: Filter): NativeQuery

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.gitlive.firebase.firestore.internal

import cocoapods.FirebaseFirestoreInternal.FIRAggregateSource
import cocoapods.FirebaseFirestoreInternal.FIRFilter
import dev.gitlive.firebase.firestore.Direction
import dev.gitlive.firebase.firestore.EncodedFieldPath
Expand All @@ -13,7 +14,9 @@ import dev.gitlive.firebase.firestore.awaitResult
import dev.gitlive.firebase.firestore.toException
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.suspendCancellableCoroutine
import platform.Foundation.NSNull
import kotlin.coroutines.resume

internal actual open class NativeQueryWrapper internal actual constructor(actual open val native: NativeQuery) {

Expand All @@ -22,6 +25,18 @@ internal actual open class NativeQueryWrapper internal actual constructor(actual
actual suspend fun get(source: Source) =
QuerySnapshot(awaitResult { native.getDocumentsWithSource(source.toIosSource(), it) })

actual suspend fun count(): Long = suspendCancellableCoroutine { continuation ->
val aggregateQuery = native.count()

aggregateQuery.aggregationWithSource(FIRAggregateSource.FIRAggregateSourceServer) { snapshot, error ->
if (error != null) {
continuation.resume(0L)
} else {
continuation.resume(snapshot?.count?.longValue ?: 0L)
}
}
}

actual val snapshots get() = callbackFlow {
val listener = native.addSnapshotListener { snapshot, error ->
snapshot?.let { trySend(QuerySnapshot(snapshot)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ internal actual open class NativeQueryWrapper internal actual constructor(actual

actual suspend fun get(source: Source) = rethrow { QuerySnapshot(js.get(source).await()) }

actual suspend fun count(): Long = rethrow {
val snapshot = js.get(Source.DEFAULT).await()
snapshot.docs.size.toLong()
}

actual fun limit(limit: Number) = query(
js,
dev.gitlive.firebase.firestore.externals.limit(limit),
Expand Down