diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionFragment.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionFragment.kt index 7cc1014ea3..d95de2c4f0 100644 --- a/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionFragment.kt @@ -100,17 +100,20 @@ class DataCollectionFragment : AbstractFragment(), BackPressListener { } // Reset progress bar - progressBar.progress = 0 - progressBar.max = (tasks.size - 1) * PROGRESS_SCALE + val (start, taskSize) = viewModel.getRelativePosition() + progressBar.progress = start + progressBar.max = (taskSize - 1) * PROGRESS_SCALE } private fun onTaskChanged() { - val index = viewModel.getAbsolutePosition() - viewPager.currentItem = index + viewPager.currentItem = viewModel.getAbsolutePosition() + // Reset progress bar + val (currIndex, taskSize) = viewModel.getRelativePosition() + progressBar.max = (taskSize - 1) * PROGRESS_SCALE progressBar.clearAnimation() - val progressAnimator = ValueAnimator.ofInt(progressBar.progress, index * PROGRESS_SCALE) + val progressAnimator = ValueAnimator.ofInt(progressBar.progress, currIndex * PROGRESS_SCALE) progressAnimator.duration = 400L progressAnimator.interpolator = FastOutSlowInInterpolator() diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionViewModel.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionViewModel.kt index 849bf91607..efef99ff00 100644 --- a/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionViewModel.kt +++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionViewModel.kt @@ -148,7 +148,7 @@ internal constructor( * the previous Data Collection screen if the user input was valid. */ fun onPreviousClicked(taskViewModel: AbstractTaskViewModel) { - check(getAbsolutePosition() != 0) + check(getRelativePosition().first != 0) val validationError = taskViewModel.validate() if (validationError != null) { @@ -198,6 +198,19 @@ internal constructor( return tasks.indexOf(tasks.first { it.id == currentTaskId.value }) } + /** Get the index of the current task within the (possibly conditional) task sequence. */ + fun getRelativePosition(): Pair { + var currentIndex = 0 + var size = 0 + getTaskSequence().forEachIndexed { index, task -> + if (task.id == currentTaskId.value) { + currentIndex = index + } + size++ + } + return currentIndex to size + } + /** * Retrieves the current task sequence given the inputs and conditions set on the tasks. Setting a * start ID will always generate a sequence with the start ID as the first element, and if