MVI란 무엇인가?

Model, View, Intent의 앞 글자를 따서 만든 아키텍처 패턴을 일컫는 용어입니다. 관심사 분리를 위해 3개의 키워드로 나눠지며, GUI 프로그래밍에서 주로 언급되는 패턴입니다.

MVI는 순수함수

Untitled

단방향 데이터 흐름 UDF

State Reducer

Reducer = (State, Event) → State State Reducer는 새로운 상태를 만드는 로직의 집합입니다. 즉, 기존의 상태를 새로운 상태로 변환하고, 상태를 한 곳에서 관리합니다.

MVI 예제코드

sealed class Event {
		object Increment : Evnent()
		object Decrement : Evnent()
}

data class State(val counter: Int = 0)

class ViewModel {
		val state = MutableStateFlow(State())
		
		// Intent를 호출하는 트리거 
		fun handleEvent(event: Event) {
				when (event) {
						is Increment -> state.update { it.copy(counter = it.counter + 1) }
						is Decrement -> state.update { it.copy(counter = it.counter - 1) }
				}
		}
}