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 a method to update the cached sequence whenever necessary. #2993

Open
shobhitagarwal1612 opened this issue Jan 9, 2025 · 1 comment
Open
Assignees

Comments

@shobhitagarwal1612
Copy link
Member

shobhitagarwal1612 commented Jan 9, 2025

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


  }

  /**
   * Generates the task sequence based on whether a task should be included or not.
   *
   * 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 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.
   *

@shobhitagarwal1612
Copy link
Member Author

shobhitagarwal1612 commented Jan 9, 2025

TaskSequence is generated from the list of all tasks. Tasks whose condition isn't satisfied are filtered out. For evaluating this condition, we use the data object in DataCollectionViewModel which stores taskdata corresponding to each task fragment.

So, we only need to update the sequence if data is mutated. Until then, the cached value can be used.

Proposal:

  1. Rename current getTaskSequence() in TaskSequenceHandler to createTaskSequence()
  2. Introduce a new method refreshSequence() in the same class which calls createTaskSequence() and saves the value in memory.
  3. Create a new wrapper class for data called TaskDataHandler which exposes setData(k, v) and getData() for the DataCollectionViewModel for saving/fetching task data.
  4. Only call refreshSequence() when setData() is called to ensure that the sequence is updated only when the data is mutated.
  5. Refactor the existing code base to use getTaskSequence() instead of createTaskSequence() (This would be done when integrating these classes with DataCollectionViewModel

@anandwana001 @scolsen @gino-m Wdyt?

@github-actions github-actions bot changed the title Cache task sequence in TaskSequenceHandler Add a method to update the cached sequence whenever necessary. Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant