This file contains hidden or 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 Event { | |
object Increment : Event() | |
object Decrement : Event() | |
} | |
data class State( | |
val counter: Int = 0 | |
) | |
class ViewModel { |
This file contains hidden or 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 ViewModel { | |
private val events = Channel<Event>() | |
val state = MutableStateFlow(State()) | |
init { | |
events.receiveAsFlow() | |
.onEach(::updateState) | |
.launchIn(viewModelScope) |
This file contains hidden or 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 ViewModel { | |
private val events = Channel<Event>() | |
val state = events.receiveAsFlow() | |
.runningFold(State(), ::reduceState) | |
.stateIn(viewModelScope, Eagerly, State()) | |
fun handleEvent(event: Event) { | |
events.trySend(event) |
This file contains hidden or 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
interface StateReducerFlow<STATE, EVENT> : StateFlow<STATE> { | |
fun handleEvent(event: EVENT) | |
} |
This file contains hidden or 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
private class StateReducerFlowImpl<STATE, EVENT>( | |
initialState: STATE, | |
reduceState: (STATE, EVENT) -> STATE, | |
scope: CoroutineScope | |
) : StateReducerFlow<STATE, EVENT> { | |
private val events = Channel<EVENT>() | |
private val stateFlow = events | |
.receiveAsFlow() |
This file contains hidden or 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
fun <STATE, EVENT> ViewModel.StateReducerFlow( | |
initialState: STATE, | |
reduceState: (STATE, EVENT) -> STATE, | |
): StateReducerFlow<STATE, EVENT> = StateReducerFlowImpl(initialState, reduceState, viewModelScope) |
This file contains hidden or 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 ViewModel { | |
val state = StateReducerFlow( | |
initialState = State(), | |
reduceState = ::reduceState | |
) | |
private fun reduceState(currentState: State, event: Event): State { | |
return when (event) { | |
is Increment -> currentState.copy(counter = currentState.counter + 1) |
This file contains hidden or 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 ExampleActivity : Activity() { | |
private val viewModel = ViewModel() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
viewModel.state.handleEvent(ExampleEvent) | |
} | |
} |
This file contains hidden or 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 <ITEM, KEY> AnimatedVerticalGrid( | |
items: List<ITEM>, | |
itemKey: (ITEM) -> KEY, | |
columns: Int, | |
rows: Int | |
) |
This file contains hidden or 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
val itemSize = remember(columns, rows) { | |
val itemWidth = (maxWidth) / rows | |
val itemHeight = (maxHeight) / columns | |
DpSize(itemWidth, itemHeight) | |
} |
OlderNewer