Skip to content

Commit

Permalink
Update progress bar to scale with conditional tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
sufyanAbbasi committed Feb 27, 2024
1 parent 45e2979 commit 933f24e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Int, Int> {
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
Expand Down

0 comments on commit 933f24e

Please sign in to comment.