Skip to content

Instantly share code, notes, and snippets.

Luca Nicoletti lnicolet

Block or report user

Report or block lnicolet

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View RallyActivity.kt
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
View buildUI.kt
@Composable
abstract fun buildUI()
View MyActivity.kt
class MyActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupViewModel()
}
private fun setupViewModel() {
viewModel = //factory fancy way to get the instance of ViewModel
viewModel.viewState.observe(this, Observer {
when (it) {
MVIViewState.Loading -> { // show loading
View MVIViewModel.kt
class MVIViewModel : ViewModel() {
val viewState: MutableLiveData<MVIViewState>()
fun fetchData() {
myDataSource.fetchData()
.doOnSubscribe {
viewState.postValue(MVIViewState.Loading)
}
.observe(
{
View MVIViewState.kt
sealed class MVIViewState {
object Loading: MVIViewState()
class Error(val reason: String): MVIViewState()
class Success(val result: String): MVIViewState()
}
View MyActivityCompose.kt
class MyActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CraneWrapper {
MyApp()
}
}
}
}
View MyAppCompose.kt
@Composable
fun MyApp() {
MaterialTheme { Text("Hello there!") }
}
View MVIJetpackComposeActivity.kt
class MVIJetpackComposeActivity : AppCompatActivity() {
lateinit var myViewModel: MVIJetpackComposeViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
myViewModel = // factory to instantiate viewmodel
// just call setContent once
setContent {
CraneWrapper {
View MVIJetpackComposeViewModel.kt
class MVIJetpackComposeViewModel : ViewModel() {
private val _jetpackComposeViewState = MutableLiveData<MVIJetpackComposeViewState>()
private val useCase = UseCase()
val jetpackComposeViewState: LiveData<MVIJetpackComposeViewState>
get() = _jetpackComposeViewState
init {
loadData()
}
View MVIJetpackComposeViewState.kt
sealed class MVIJetpackComposeViewState {
@Composable
abstract fun buildUI()
object Loading : MyViewState() {
@Composable
override fun buildUI() {
// Loading which is not provided yet
}
}
You can’t perform that action at this time.