Junior — Middle
Tell about your experience implementing the Model-View-Intent (MVI) architectural pattern.
sobes.tech AI
Answer from AI
Model-View-Intent (MVI) is an architectural pattern that helps manage UI state through a stream of user intentions (intents).
In my experience implementing MVI in Android:
- Intent — user actions or events sent to the system.
- Model — business logic and state that process intents and form a new state.
- View — displays the state and generates intents.
For example, when a button is pressed, the View sends an Intent, the Model processes it and updates the State, which the View then displays.
This approach makes the UI predictable and easy to test.
A simplified code example:
sealed class Intent {
object LoadData : Intent()
data class Submit(val text: String) : Intent()
}
data class ViewState(val isLoading: Boolean, val data: String?)
class Model {
private val state = MutableStateFlow(ViewState(false, null))
fun process(intent: Intent) {
when (intent) {
is Intent.LoadData -> loadData()
is Intent.Submit -> submitData(intent.text)
}
}
private fun loadData() {
state.value = ViewState(true, null)
// data loading
state.value = ViewState(false, "Data loaded")
}
private fun submitData(text: String) {
// handle submission
}
fun getState(): StateFlow<ViewState> = state
}