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

How to perform retry for loading UI state when using .stateIn() approach? #987

Open
KoTius opened this issue Dec 13, 2023 · 1 comment
Open

Comments

@KoTius
Copy link

KoTius commented Dec 13, 2023

Example

He we load data model from repository. Usually we have here API IO so it can always fail.
In the example we map the fail into the Error class and later into error Ui State. Understandable.

Now assuming on the Error Ui we have the "Try again button" which will call onRetryClick() callback in the ViewModel.
How we supposed to re-start all that chain or only _taskAsync chain?

private val _taskAsync = taskRepository.getTaskStream(taskId)
        .map { handleTask(it) }
        .catch { emit(Async.Error(R.string.loading_task_error)) }

   val uiState: StateFlow<TaskDetailUiState> = combine(
        _userMessage, _isLoading, _isTaskDeleted, _taskAsync
    ) { userMessage, isLoading, isTaskDeleted, taskAsync ->
        when (taskAsync) {
            Async.Loading -> {
                TaskDetailUiState(isLoading = true)
            }
            is Async.Error -> {
                TaskDetailUiState(
                    userMessage = taskAsync.errorMessage,
                    isTaskDeleted = isTaskDeleted
                )
            }
            is Async.Success -> {
                TaskDetailUiState(
                    task = taskAsync.data,
                    isLoading = isLoading,
                    userMessage = userMessage,
                    isTaskDeleted = isTaskDeleted
                )
            }
        }
    }
@vatsasiddhartha
Copy link

class YourViewModel(private val taskRepository: TaskRepository) : ViewModel() {

private val _taskAsync = MutableStateFlow<Async<Task>>(Async.Uninitialized)

val uiState: StateFlow<TaskDetailUiState> = combine(
    _userMessage, _isLoading, _isTaskDeleted, _taskAsync
) { userMessage, isLoading, isTaskDeleted, taskAsync ->
    // Your existing code for combining state
    // ...
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), initialState)

init {
    loadTask()
}

private fun loadTask() {
    _taskAsync.value = Async.Loading
    viewModelScope.launch {
        try {
            val task = taskRepository.getTask(taskId)
            _taskAsync.value = Async.Success(task)
        } catch (e: Exception) {
            _taskAsync.value = Async.Error(R.string.loading_task_error)
        }
    }
}

fun onRetryClick() {
    loadTask()
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants