Last active
February 4, 2022 11:03
-
-
Save dimsuz/179d50e4eb04a0e5dba2928362429cb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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