android / android/architecture-samples

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

Open
#987 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
45.8k
Forks
11.9k
PR merge metrics
No merged PRs in 30d

Description

[Example](https://github.com/android/architecture-samples/blob/master/app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailViewModel.kt)

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 = 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
)
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start with app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailViewModel.kt and inspect how getTaskStream(taskId), _taskAsync, and uiState are connected. Determine the intended retry entry point for the Try again button; done means the retry behavior for the shown Error state is clearly documented or demonstrated.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.