Skip to content

Instantly share code, notes, and snippets.

@dimsuz
Last active February 4, 2022 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimsuz/179d50e4eb04a0e5dba2928362429cb4 to your computer and use it in GitHub Desktop.
Save dimsuz/179d50e4eb04a0e5dba2928362429cb4 to your computer and use it in GitHub Desktop.
enum class TaskState { Idle, Running }
interface Task {
val state: Flow<TaskState>
val results: Flow<Result<Int, Throwable>>
fun start()
}
@Composable
fun Screen(task: Task) {
val loadingState by task.state.collectAsState(initial = TaskState.Idle)
val result by task.results.collectAsState(initial = null)
Box {
if (loadingState == TaskState.Running) {
CircularProgressIndicator()
}
if (result != null && loadingState == TaskState.Idle) {
when (result) {
is Ok -> {
Text("Result is ${result.value}")
}
is Err -> {
Column {
Text("Error ${result.error}")
Button(onClick = { task.start() }) {
Text("Retry")
}
}
}
null -> Unit
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment