Skip to content

Commit

Permalink
Remove unused code related to loading submissions (#2503)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Jun 18, 2024
1 parent cdd50f1 commit 270470b
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.google.android.ground.model.TermsOfService
import com.google.android.ground.model.User
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.model.mutation.Mutation
import com.google.android.ground.model.submission.Submission
import kotlinx.coroutines.flow.Flow

/**
Expand Down Expand Up @@ -49,12 +48,6 @@ interface RemoteDataStore {
/** Returns LOIs created by the specified email in the specified survey. Main-safe. */
suspend fun loadUserDefinedLois(survey: Survey, creatorEmail: String): List<LocationOfInterest>

/**
* Returns a list of all submissions associated with the specified LOI, or an empty list if none
* are found.
*/
suspend fun loadSubmissions(locationOfInterest: LocationOfInterest): List<Submission>

/**
* Applies the provided mutations to the remote data store in a single batched transaction. If one
* update fails, none of the mutations will be applied.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import com.google.android.ground.model.Survey
import com.google.android.ground.model.SurveyListItem
import com.google.android.ground.model.TermsOfService
import com.google.android.ground.model.User
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.model.mutation.LocationOfInterestMutation
import com.google.android.ground.model.mutation.Mutation
import com.google.android.ground.model.mutation.SubmissionMutation
import com.google.android.ground.model.submission.Submission
import com.google.android.ground.model.toListItem
import com.google.android.ground.persistence.remote.RemoteDataStore
import com.google.android.ground.persistence.remote.firebase.schema.GroundFirestore
Expand Down Expand Up @@ -59,15 +57,6 @@ internal constructor(
override suspend fun loadSurvey(surveyId: String): Survey =
withContext(ioDispatcher) { db().surveys().survey(surveyId).get() }

override suspend fun loadSubmissions(locationOfInterest: LocationOfInterest): List<Submission> =
withContext(ioDispatcher) {
db()
.surveys()
.survey(locationOfInterest.surveyId)
.submissions()
.submissionsByLocationOfInterestId(locationOfInterest)
}

override suspend fun loadTermsOfService(): TermsOfService? =
withContext(ioDispatcher) { db().termsOfService().terms().get() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package com.google.android.ground.persistence.remote.firebase.base

import com.google.android.ground.system.NetworkManager
import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.Query
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.tasks.await
import timber.log.Timber

open class FluentCollectionReference
protected constructor(
Expand All @@ -32,35 +27,6 @@ protected constructor(
protected val defaultDispatcher: CoroutineDispatcher = Dispatchers.Default,
) {

private val context = reference.firestore.app.applicationContext

/**
* Runs the specified query, returning a Single containing a List of values created by applying
* the mappingFunction to all results. Fails immediately with an error if an active network is not
* available.
*/
protected suspend fun <T> runQuery(
query: Query,
mappingFunction: (snapshot: DocumentSnapshot) -> T,
): List<T> {
NetworkManager(context).requireNetworkConnection()
val querySnapshot = query.get().await()
return querySnapshot.documents
.filter { it.exists() }
.mapNotNull { applyFunctionAndIgnoreFailures(it, mappingFunction) }
}

private fun <T> applyFunctionAndIgnoreFailures(
value: DocumentSnapshot,
mappingFunction: (snapshot: DocumentSnapshot) -> T,
): T? =
try {
mappingFunction(value)
} catch (e: Throwable) {
Timber.e(e, "Skipping corrupt remote document: ${value.id}")
null
}

protected fun reference(): CollectionReference = reference

override fun toString(): String = reference.path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,11 @@

package com.google.android.ground.persistence.remote.firebase.schema

import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.model.submission.Submission
import com.google.android.ground.persistence.remote.firebase.base.FluentCollectionReference
import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.FieldPath
import com.google.firebase.firestore.Query

class SubmissionCollectionReference internal constructor(ref: CollectionReference) :
FluentCollectionReference(ref) {

fun submission(id: String) = SubmissionDocumentReference(reference().document(id))

suspend fun submissionsByLocationOfInterestId(
locationOfInterest: LocationOfInterest
): List<Submission> =
runQuery(byLoiId(locationOfInterest.id)) {
SubmissionConverter.toSubmission(locationOfInterest, it)
}

private fun byLoiId(loiId: String): Query =
reference().whereEqualTo(FieldPath.of(SubmissionMutationConverter.LOI_ID), loiId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import timber.log.Timber
/** Converts between Firestore maps used to merge updates and [SubmissionMutation] instances. */
internal object SubmissionMutationConverter {

const val LOI_ID = "loiId"
private const val LOI_ID = "loiId"
private const val JOB_ID = "jobId"
private const val DATA = "data"
private const val CREATED = "created"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.google.android.ground.model.TermsOfService
import com.google.android.ground.model.User
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.model.mutation.Mutation
import com.google.android.ground.model.submission.Submission
import com.google.android.ground.model.toListItem
import com.google.android.ground.persistence.remote.RemoteDataStore
import javax.inject.Inject
Expand Down Expand Up @@ -52,10 +51,6 @@ class FakeRemoteDataStore @Inject internal constructor() : RemoteDataStore {

override suspend fun loadPredefinedLois(survey: Survey) = predefinedLois

override suspend fun loadSubmissions(locationOfInterest: LocationOfInterest): List<Submission> {
TODO("Missing implementation")
}

override suspend fun applyMutations(mutations: List<Mutation>, user: User) {
if (applyMutationError != null) {
throw applyMutationError as Error
Expand Down

0 comments on commit 270470b

Please sign in to comment.