Skip to content

Commit

Permalink
Cleanup SubmissionRepository (#2495)
Browse files Browse the repository at this point in the history
* Use proto when creating an empty submission

* Simplify SubmissionRepository by removing transient submission instance

* Update unit test

* Revert import
  • Loading branch information
shobhitagarwal1612 authored Jun 16, 2024
1 parent 2e37e9a commit c22c06e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.google.android.ground.repository

import com.google.android.ground.model.AuditInfo
import com.google.android.ground.model.Survey
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.model.mutation.Mutation
Expand Down Expand Up @@ -48,37 +47,27 @@ constructor(
private val uuidGenerator: OfflineUuidGenerator,
) {

suspend fun createSubmission(surveyId: String, locationOfInterestId: String): Submission {
val auditInfo = AuditInfo(userRepository.getAuthenticatedUser())
val loi = locationOfInterestRepository.getOfflineLoi(surveyId, locationOfInterestId)
return Submission(uuidGenerator.generateUuid(), surveyId, loi, loi.job, auditInfo, auditInfo)
}

private suspend fun createOrUpdateSubmission(
submission: Submission,
deltas: List<ValueDelta>,
isNew: Boolean,
) =
applyAndEnqueue(
SubmissionMutation(
job = submission.job,
submissionId = submission.id,
deltas = deltas,
type = if (isNew) Mutation.Type.CREATE else Mutation.Type.UPDATE,
syncStatus = SyncStatus.PENDING,
surveyId = submission.surveyId,
locationOfInterestId = submission.locationOfInterest.id,
userId = submission.lastModified.user.id,
)
)

/** Creates a new submission in the local data store and enqueues a sync worker. */
suspend fun saveSubmission(
surveyId: String,
locationOfInterestId: String,
deltas: List<ValueDelta>,
) {
val submission = createSubmission(surveyId, locationOfInterestId)
createOrUpdateSubmission(submission, deltas, isNew = true)
val newId = uuidGenerator.generateUuid()
val userId = userRepository.getAuthenticatedUser().id
val job = locationOfInterestRepository.getOfflineLoi(surveyId, locationOfInterestId).job
val mutation =
SubmissionMutation(
job = job,
submissionId = newId,
deltas = deltas,
type = Mutation.Type.CREATE,
syncStatus = SyncStatus.PENDING,
surveyId = surveyId,
locationOfInterestId = locationOfInterestId,
userId = userId,
)
applyAndEnqueue(mutation)
}

suspend fun getDraftSubmission(draftSubmissionId: String, survey: Survey): DraftSubmission? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import org.mockito.Mock
import org.mockito.kotlin.eq
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import org.robolectric.shadows.ShadowToast

Expand Down Expand Up @@ -277,9 +276,6 @@ class DataCollectionFragmentTest : BaseHiltTest() {
}

private fun setupSubmission() = runWithTestDispatcher {
whenever(submissionRepository.createSubmission(SURVEY.id, LOCATION_OF_INTEREST.id))
.thenReturn(SUBMISSION)

fakeRemoteDataStore.surveys = listOf(SURVEY)
fakeRemoteDataStore.predefinedLois = listOf(LOCATION_OF_INTEREST)
activateSurvey(SURVEY.id)
Expand Down Expand Up @@ -378,7 +374,6 @@ class DataCollectionFragmentTest : BaseHiltTest() {
)

private val JOB = FakeData.JOB.copy(tasks = TASKS.associateBy { it.id })
private val SUBMISSION = FakeData.SUBMISSION.copy(job = JOB)
private val SURVEY = FakeData.SURVEY.copy(jobMap = mapOf(Pair(JOB.id, JOB)))
}
}

0 comments on commit c22c06e

Please sign in to comment.