View mvi_framework_updated_counter_screen.kt
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 CounterScreen( | |
state: CounterState, | |
onDecreaseClick: () -> Unit, | |
onIncreaseClick: () -> Unit, | |
onGenerateRandom: () -> Unit, | |
modifier: Modifier = Modifier, | |
) { | |
Box( | |
contentAlignment = Alignment.Center, |
View mvi_framework_upadted_counter_middleware.kt
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 CounterViewModel( | |
// 1 | |
middleware: CounterMiddleware = CounterMiddleware(), | |
reducer: CounterReducer = CounterReducer(), | |
) : MviViewModel<CounterState, CounterAction>( | |
reducer = reducer, | |
// 2 | |
middlewares = listOf(middleware), | |
initialState = CounterState.initial, | |
) { |
View mvi_framework_updated_reducer.kt
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 CounterReducer : Reducer<CounterState, CounterAction> { | |
override fun reduce(state: CounterState, action: CounterAction): CounterState { | |
return when (action) { | |
// 1 | |
CounterAction.Loading -> state.copy(loading = true) | |
CounterAction.Decrement -> state.copy(counter = state.counter - 1) | |
CounterAction.Increment -> state.copy(counter = state.counter + 1) | |
// 2 | |
is CounterAction.CounterUpdated -> state.copy( |
View mvi_framework_counter_middleware.kt
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
// 1 | |
class CounterMiddleware : Middleware<CounterState, CounterAction>() { | |
// 2 | |
override suspend fun process(state: CounterState, action: CounterAction) { | |
// 3 | |
when (action) { | |
CounterAction.GenerateRandom -> generateRandom() | |
else -> { /* no-op */ } | |
} |
View mvi_framework_updated_actions.kt
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 interface CounterAction : Action { | |
data object Loading : CounterAction | |
data object Increment : CounterAction | |
data object Decrement : CounterAction | |
data object GenerateRandom : CounterAction | |
data class CounterUpdated(val value: Int) : CounterAction | |
} |
View mvi_framework_updated_counter_state.kt
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
data class CounterState( | |
// 1 | |
val loading: Boolean, | |
val counter: Int, | |
) : State { | |
companion object { | |
val initial: CounterState = CounterState( | |
// 2 | |
loading = false, | |
counter = 0, |
View mvi_framework_updated_viewmodel.kt
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
open class MviViewModel<S : State, A : Action>( | |
private val reducer: Reducer<S, A>, | |
// 1 | |
private val middlewares: List<Middleware<S, A>> = emptyList(), | |
initialState: S, | |
) : ViewModel(), Dispatcher<A> { | |
// 2 | |
private data class ActionImpl<S : State, A : Action>( | |
val state: S, |
View mvi_framework_middleware.kt
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
// 1 | |
interface Dispatcher<A : Action> { | |
fun dispatch(action: A) | |
} | |
// 2 | |
abstract class Middleware<S : State, A : Action>() { | |
// 3 | |
private lateinit var dispatcher: Dispatcher<A> | |
View mvi_framework_counter_screen.kt
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 CounterScreen( | |
viewModel: CounterViewModel, | |
modifier: Modifier = Modifier, | |
) { | |
CounterScreen( | |
state = viewModel.state, | |
onDecreaseClick = viewModel::onDecrement, | |
onIncreaseClick = viewModel::onIncrement, | |
modifier = modifier, |
View mvi_framework_counter_viewmodel.kt
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 CounterViewModel( | |
// 1 | |
reducer: CounterReducer = CounterReducer(), | |
// 2 | |
) : MviViewModel<CounterState, CounterAction>( | |
reducer = reducer, | |
initialState = CounterState.initial, | |
) { | |
// 3 | |
fun onDecrement() { |
NewerOlder