From c226a6549854a5415caddb4cd19e9ae800610386 Mon Sep 17 00:00:00 2001 From: Shobhit Agarwal Date: Thu, 9 Jan 2025 13:00:06 +0530 Subject: [PATCH 1/3] Add logic to cache generated task sequence --- .../ui/datacollection/TaskSequenceHandler.kt | 22 +++++++++++++++---- .../datacollection/TaskSequenceHandlerTest.kt | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt index b67d8a739e..9c0a56daef 100644 --- a/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt +++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt @@ -36,6 +36,9 @@ class TaskSequenceHandler( (task: Task, taskValueOverride: Pair?) -> Boolean, ) { + private var taskSequence: Sequence = sequenceOf() + private var isSequenceInitialized = false + init { require(tasks.isNotEmpty()) { "Can't generate a sequence from an empty task list." } } @@ -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? = null): Sequence = + fun createTaskSequence(taskValueOverride: Pair? = null): Sequence = tasks.filter { task -> shouldIncludeTask(task, taskValueOverride) }.asSequence() + fun getTaskSequence(): Sequence { + if (!isSequenceInitialized) { + taskSequence = createTaskSequence() + 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. * diff --git a/ground/src/test/java/com/google/android/ground/ui/datacollection/TaskSequenceHandlerTest.kt b/ground/src/test/java/com/google/android/ground/ui/datacollection/TaskSequenceHandlerTest.kt index 43c0dabe3a..b6503e8d01 100644 --- a/ground/src/test/java/com/google/android/ground/ui/datacollection/TaskSequenceHandlerTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/datacollection/TaskSequenceHandlerTest.kt @@ -66,7 +66,7 @@ class TaskSequenceHandlerTest { } @Test - fun `getTaskSequence filters tasks based on shouldIncludeTask and taskValueOverride`() { + fun `createTaskSequence filters tasks based on shouldIncludeTask and taskValueOverride`() { val handler = createHandler( shouldIncludeTask = { task, taskValueOverride -> @@ -75,7 +75,7 @@ class TaskSequenceHandlerTest { !(task.id == "task3" && taskValueOverride?.first == "task3") } ) - val sequence = handler.getTaskSequence(taskValueOverride = "task3" to null) + val sequence = handler.createTaskSequence(taskValueOverride = "task3" to null) assertThat(sequence.toList()).isEqualTo(listOf(task1, task5)) } From 694b6cd5cef8057673bd65facbda312dbe7559ae Mon Sep 17 00:00:00 2001 From: Shobhit Agarwal Date: Thu, 9 Jan 2025 18:24:37 +0530 Subject: [PATCH 2/3] Rename method --- .../android/ground/ui/datacollection/TaskSequenceHandler.kt | 4 ++-- .../ground/ui/datacollection/TaskSequenceHandlerTest.kt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt index 9c0a56daef..add88bd6c2 100644 --- a/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt +++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt @@ -62,12 +62,12 @@ class TaskSequenceHandler( * 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 createTaskSequence(taskValueOverride: Pair? = null): Sequence = + fun generateTaskSequence(taskValueOverride: Pair? = null): Sequence = tasks.filter { task -> shouldIncludeTask(task, taskValueOverride) }.asSequence() fun getTaskSequence(): Sequence { if (!isSequenceInitialized) { - taskSequence = createTaskSequence() + taskSequence = generateTaskSequence() isSequenceInitialized = true } return taskSequence diff --git a/ground/src/test/java/com/google/android/ground/ui/datacollection/TaskSequenceHandlerTest.kt b/ground/src/test/java/com/google/android/ground/ui/datacollection/TaskSequenceHandlerTest.kt index b6503e8d01..8690c74058 100644 --- a/ground/src/test/java/com/google/android/ground/ui/datacollection/TaskSequenceHandlerTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/datacollection/TaskSequenceHandlerTest.kt @@ -66,7 +66,7 @@ class TaskSequenceHandlerTest { } @Test - fun `createTaskSequence filters tasks based on shouldIncludeTask and taskValueOverride`() { + fun `generateTaskSequence filters tasks based on shouldIncludeTask and taskValueOverride`() { val handler = createHandler( shouldIncludeTask = { task, taskValueOverride -> @@ -75,7 +75,7 @@ class TaskSequenceHandlerTest { !(task.id == "task3" && taskValueOverride?.first == "task3") } ) - val sequence = handler.createTaskSequence(taskValueOverride = "task3" to null) + val sequence = handler.generateTaskSequence(taskValueOverride = "task3" to null) assertThat(sequence.toList()).isEqualTo(listOf(task1, task5)) } From 347e6e187cc1948cdec92b159239d1d1b4ac99e4 Mon Sep 17 00:00:00 2001 From: Shobhit Agarwal Date: Fri, 10 Jan 2025 10:31:40 +0530 Subject: [PATCH 3/3] Replace sequenceOf with emptySequence --- .../android/ground/ui/datacollection/TaskSequenceHandler.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt index add88bd6c2..2bf2ad4445 100644 --- a/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt +++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/TaskSequenceHandler.kt @@ -36,7 +36,7 @@ class TaskSequenceHandler( (task: Task, taskValueOverride: Pair?) -> Boolean, ) { - private var taskSequence: Sequence = sequenceOf() + private var taskSequence: Sequence = emptySequence() private var isSequenceInitialized = false init {