Skip to content

Commit

Permalink
Merge 9b5805b into cb34a56
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Jan 14, 2025
2 parents cb34a56 + 9b5805b commit 6972d36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class TaskSequenceHandler(
(task: Task, taskValueOverride: Pair<String, TaskData?>?) -> Boolean,
) {

private var taskSequence: Sequence<Task> = emptySequence()
private var isSequenceInitialized = false

init {
require(tasks.isNotEmpty()) { "Can't generate a sequence from an empty task list." }
}
Expand All @@ -50,18 +53,29 @@ class TaskSequenceHandler(
}

/**
* Retrieves the task sequence based on the provided inputs and conditions.
* Generates the task sequence based on whether a task should be included or not.
*
* This function determines the order of tasks to be presented, taking into account any overrides
* specified by [taskValueOverride].
* This determines the order of tasks to be presented to the user, taking into account any
* overrides specified by [taskValueOverride].
*
* @param taskValueOverride An optional pair where the first element is the task ID and the second
* element is the [TaskData] to override the default task data. If null, no override is applied.
* @return A [Sequence] of [Task] objects representing the ordered tasks.
*/
fun getTaskSequence(taskValueOverride: Pair<String, TaskData?>? = null): Sequence<Task> =
fun generateTaskSequence(taskValueOverride: Pair<String, TaskData?>? = null): Sequence<Task> =
tasks.filter { task -> shouldIncludeTask(task, taskValueOverride) }.asSequence()

fun getTaskSequence(): Sequence<Task> {
if (!isSequenceInitialized) {
taskSequence = generateTaskSequence()
isSequenceInitialized = true
}
return taskSequence
}

// TODO: Add a method to update the cached sequence whenever necessary.
// Issue URL: https://github.com/google/ground-android/issues/2993

/**
* Checks if the specified task is the first task in the displayed sequence.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TaskSequenceHandlerTest {
}

@Test
fun `getTaskSequence filters tasks based on shouldIncludeTask and taskValueOverride`() {
fun `generateTaskSequence filters tasks based on shouldIncludeTask and taskValueOverride`() {
val handler =
createHandler(
shouldIncludeTask = { task, taskValueOverride ->
Expand All @@ -75,7 +75,7 @@ class TaskSequenceHandlerTest {
!(task.id == "task3" && taskValueOverride?.first == "task3")
}
)
val sequence = handler.getTaskSequence(taskValueOverride = "task3" to null)
val sequence = handler.generateTaskSequence(taskValueOverride = "task3" to null)
assertThat(sequence.toList()).isEqualTo(listOf(task1, task5))
}

Expand Down

0 comments on commit 6972d36

Please sign in to comment.