-
Notifications
You must be signed in to change notification settings - Fork 0
MvRx
Devrath edited this page Apr 10, 2024
·
10 revisions
- Mavericks is a
MVI-framework
/** State classes contain all of the data you need to render a screen. */
data class CounterState(val count: Int = 0) : MavericksState
/** ViewModels are where all of your business logic lives. It has a simple lifecycle and is easy to test. */
class CounterViewModel(initialState: CounterState) : MavericksViewModel<CounterState>(initialState) {
fun incrementCount() = setState { copy(count = count + 1) }
}
/**
* Fragments in Mavericks are simple and rarely do more than bind your state to views.
* Mavericks works well with Fragments but you can use it with whatever view architecture you use.
*/
class CounterFragment : Fragment(R.layout.counter_fragment), MavericksView {
private val viewModel: CounterViewModel by fragmentViewModel()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
counterText.setOnClickListener {
viewModel.incrementCount()
}
}
override fun invalidate() = withState(viewModel) { state ->
counterText.text = "Count: ${state.count}"
}
}Mastering Mavericks only requires using three classes
MavericksStateMavericksViewModelMavericksView
- This is the first step in creating a Screen by modeling it as a function of state.
- The Mavericks state doesn't do anything but signal the intention of your class to be used as a state.