Skip to content

Commit

Permalink
[Data collection] Restore position to last active task when restoring…
Browse files Browse the repository at this point in the history
… from draft (#2871)

* Save currentTaskId to DraftSubmission

* Set currentTaskId from draftSubmission to DataCollectionFragment

* Update unit test

* Add schema for db config 121

* Update unit tests

* Update ktdoc formatting

---------

Co-authored-by: Gino Miceli <228050+gino-m@users.noreply.github.com>
  • Loading branch information
shobhitagarwal1612 and gino-m authored Dec 5, 2024
1 parent 859283c commit 85ac644
Show file tree
Hide file tree
Showing 13 changed files with 1,152 additions and 8 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ground/src/main/java/com/google/android/ground/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object Config {
const val SHARED_PREFS_MODE = Context.MODE_PRIVATE

// Local db settings.
const val DB_VERSION = 120
const val DB_VERSION = 121
const val DB_NAME = "ground.db"

// Firebase Cloud Firestore settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ data class DraftSubmission(
val loiName: String?,
val surveyId: String,
val deltas: List<ValueDelta>,
val currentTaskId: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.ground.persistence.local.room

import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
Expand Down Expand Up @@ -89,6 +90,7 @@ import com.google.android.ground.persistence.local.room.fields.TileSetEntityStat
],
version = Config.DB_VERSION,
exportSchema = true,
autoMigrations = [AutoMigration(from = 120, to = 121)],
)
@TypeConverters(
TaskEntityType::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ fun DraftSubmissionEntity.toModelObject(survey: Survey): DraftSubmission {
loiName = loiName,
surveyId = surveyId,
deltas = SubmissionDeltasConverter.fromString(job, deltas),
currentTaskId = currentTaskId,
)
}

Expand All @@ -498,4 +499,5 @@ fun DraftSubmission.toLocalDataStoreObject() =
loiName = loiName,
surveyId = surveyId,
deltas = SubmissionDeltasConverter.toString(deltas),
currentTaskId = currentTaskId,
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ data class DraftSubmissionEntity(
@ColumnInfo(name = "survey_id") val surveyId: String,
@ColumnInfo(name = "deltas") val deltas: String?,
@ColumnInfo(name = "loi_name") val loiName: String?,
@ColumnInfo(name = "current_task_id") val currentTaskId: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ constructor(
surveyId: String,
deltas: List<ValueDelta>,
loiName: String?,
currentTaskId: String,
) {
val newId = uuidGenerator.generateUuid()
val draft = DraftSubmission(newId, jobId, loiId, loiName, surveyId, deltas)
val draft = DraftSubmission(newId, jobId, loiId, loiName, surveyId, deltas, currentTaskId)
localSubmissionStore.saveDraftSubmission(draftSubmission = draft)
localValueStore.draftSubmissionId = newId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class DataCollectionFragment : AbstractFragment(), BackPressListener {
if (currentAdapter == null || currentAdapter.tasks != tasks) {
viewPager.adapter = viewPagerAdapterFactory.create(this, tasks)
}
updateProgressBar(taskPosition, false)
viewPager.doOnLayout { onTaskChanged(taskPosition) }
}

private fun onTaskChanged(taskPosition: TaskPosition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ internal constructor(

/** Moves back to the previous task in the sequence if the current value is valid or empty. */
suspend fun onPreviousClicked(taskViewModel: AbstractTaskViewModel) {
check(getPositionInTaskSequence().first != 0)

val task = taskViewModel.task
val taskValue = taskViewModel.taskTaskData.firstOrNull()

Expand Down Expand Up @@ -287,6 +285,7 @@ internal constructor(
surveyId = surveyId,
deltas = getDeltas(),
loiName = customLoiName,
currentTaskId = currentTaskId.value,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class HomeScreenFragment :
draft.jobId,
true,
SubmissionDeltasConverter.toString(draft.deltas),
draft.currentTaskId ?: "",
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class HomeScreenMapContainerFragment : AbstractMapContainerFragment() {
cardUiData.loi.job.id,
false,
null,
"",
)
)
is MapCardUiData.AddLoiCardUiData ->
Expand All @@ -315,6 +316,7 @@ class HomeScreenMapContainerFragment : AbstractMapContainerFragment() {
cardUiData.job.id,
false,
null,
"",
)
)
}
Expand Down
8 changes: 8 additions & 0 deletions ground/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
android:name="draftValues"
type="string"
app:nullable="true" />
<argument
android:name="currentTaskId"
type="string"
app:nullable="true" />

<fragment
android:id="@+id/data_collection_fragment"
Expand All @@ -130,6 +134,10 @@
android:name="draftValues"
type="string"
app:nullable="true" />
<argument
android:name="currentTaskId"
type="string"
app:nullable="true" />
</fragment>

<fragment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class DataCollectionFragmentTest : BaseHiltTest() {
fun `Next click saves draft`() = runWithTestDispatcher {
runner().inputText(TASK_1_RESPONSE).clickNextButton()

assertDraftSaved(listOf(TASK_1_VALUE_DELTA))
assertDraftSaved(listOf(TASK_1_VALUE_DELTA), currentTaskId = TASK_ID_2)
}

@Test
Expand All @@ -130,7 +130,7 @@ class DataCollectionFragmentTest : BaseHiltTest() {
.selectMultipleChoiceOption(TASK_2_OPTION_LABEL)
.clickPreviousButton()

assertDraftSaved(listOf(TASK_1_VALUE_DELTA, TASK_2_VALUE_DELTA))
assertDraftSaved(listOf(TASK_1_VALUE_DELTA, TASK_2_VALUE_DELTA), currentTaskId = TASK_ID_1)
}

@Test
Expand Down Expand Up @@ -159,6 +159,7 @@ class DataCollectionFragmentTest : BaseHiltTest() {
JOB.id,
true,
SubmissionDeltasConverter.toString(expectedDeltas),
"",
)
.build()
.toBundle()
Expand Down Expand Up @@ -273,7 +274,7 @@ class DataCollectionFragmentTest : BaseHiltTest() {
)
}

private suspend fun assertDraftSaved(valueDeltas: List<ValueDelta>) {
private suspend fun assertDraftSaved(valueDeltas: List<ValueDelta>, currentTaskId: String) {
val draftId = submissionRepository.getDraftSubmissionsId()
assertThat(draftId).isNotEmpty()

Expand All @@ -288,6 +289,7 @@ class DataCollectionFragmentTest : BaseHiltTest() {
loiName = LOCATION_OF_INTEREST_NAME,
surveyId = SURVEY.id,
deltas = valueDeltas,
currentTaskId = currentTaskId,
)
)
}
Expand All @@ -314,6 +316,7 @@ class DataCollectionFragmentTest : BaseHiltTest() {
JOB.id,
false,
null,
"",
)
.build()
.toBundle()
Expand Down

0 comments on commit 85ac644

Please sign in to comment.