Last active
March 2, 2020 10:56
-
-
Save fluxtah/dc147d82786b90185f55462d4e9a5eae 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
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