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
/** | |
* Created by Luca Nicoletti | |
* © 17/10/2018 | |
* Copied from: https://gist.github.com/nesquena/d09dc68ff07e845cc622; added some little edits for Kotlin | |
*/ | |
abstract class EndlessRecyclerViewScrollListener(private var visibleThreshold: Int = 5): RecyclerView.OnScrollListener() { | |
private var currentPage = 0 | |
private var previousTotalItemCount = 0 | |
private var loading = true |
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
sealed class MyViewState { | |
class Error(val cause: String): MyViewState() | |
object Loading: MyViewState() | |
class DataLoaded(data: Data): MyViewState() | |
} | |
[...MyView...] | |
fun render(viewState: MyViewState) { | |
when (viewState) { | |
MyViewState.Error -> { // handle error state |
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
sealed class MVIJetpackComposeViewState { | |
@Composable | |
abstract fun buildUI() | |
object Loading : MyViewState() { | |
@Composable | |
override fun buildUI() { | |
// Loading which is not provided yet | |
} | |
} |
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 MVIJetpackComposeViewModel : ViewModel() { | |
private val _jetpackComposeViewState = MutableLiveData<MVIJetpackComposeViewState>() | |
private val useCase = UseCase() | |
val jetpackComposeViewState: LiveData<MVIJetpackComposeViewState> | |
get() = _jetpackComposeViewState | |
init { | |
loadData() | |
} |
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 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 { |
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
@Composable | |
fun MyApp() { | |
MaterialTheme { Text("Hello there!") } | |
} |
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 MyActivity: Activity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
CraneWrapper { | |
MyApp() | |
} | |
} | |
} | |
} |
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
sealed class MVIViewState { | |
object Loading: MVIViewState() | |
class Error(val reason: String): MVIViewState() | |
class Success(val result: String): MVIViewState() | |
} |
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 MVIViewModel : ViewModel() { | |
val viewState: MutableLiveData<MVIViewState>() | |
fun fetchData() { | |
myDataSource.fetchData() | |
.doOnSubscribe { | |
viewState.postValue(MVIViewState.Loading) | |
} | |
.observe( | |
{ |
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 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 |
OlderNewer