Skip to content

Instantly share code, notes, and snippets.

@fluxtah
Last active March 2, 2020 10:56
Show Gist options
  • Save fluxtah/dc147d82786b90185f55462d4e9a5eae to your computer and use it in GitHub Desktop.
Save fluxtah/dc147d82786b90185f55462d4e9a5eae to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val hello = remember { HelloModel(HelloResult.Pending) }
observe(getHello()) {
onResult {
if(hello.state != result) hello.state = result
}
}
hello.state.let { state ->
when (state) {
is HelloResult.Pending -> Text("Loading...")
is HelloResult.Hello -> Text("Hello")
is HelloResult.Error -> Text("Error")
}
}
}
fun getHello(): LiveData<HelloResult> {
return MutableLiveData(HelloResult.Hello())
}
sealed class HelloResult {
object Pending : HelloResult()
object Hello : HelloResult()
object Error : HelloResult()
}
@Model
data class HelloModel(
var state: HelloResult = HelloResult.Pending
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment