package com.android.onboarding.tasks import java.util.concurrent.ConcurrentHashMap /** * Manages the states of onboarding tasks in a centralized and thread-safe manner. * * [OnboardingTaskStateManager] allows you to track/update the progress and results of individual * onboarding tasks, identified by unique [OnboardingTaskToken] instances. */ class OnboardingTaskStateManager { private val taskStates = ConcurrentHashMap>() /** * Updates the state of an onboarding task. * * @param token The [OnboardingTaskToken] identifying the task. * @param state The updated [OnboardingTaskState] object representing the task's new state. The * type of the result within the state object is flexible. */ fun updateTaskState(token: OnboardingTaskToken, state: OnboardingTaskState<*>) { taskStates[token] = state } /** * Retrieves the current state of an onboarding task. * * @param token The [OnboardingTaskToken] identifying the task. * @return The [OnboardingTaskState] object associated with the task. If no state is found for the * provided token, a [OnboardingTaskState.Failed] with the error message * [ERROR_TASK_CANT_BE_FOUND] is returned. */ fun getTaskState(token: OnboardingTaskToken): OnboardingTaskState { return taskStates[token] as? OnboardingTaskState ?: OnboardingTaskState.Failed(ERROR_TASK_CANT_BE_FOUND) } }