Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to cache generated task sequence #2994

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading