-
-
Save uteke/1850cfa293b035ce311765138a486d48 to your computer and use it in GitHub Desktop.
Model delegate implementation
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 ModelProperty<ViewState, Action, Mutation, Event>( | |
private val viewModel: ViewModel, | |
private val actionProcessors: Collection<ActionProcessor<Action, Mutation, Event>>, | |
private val reducers: Collection<Reducer<Mutation, ViewState>>, | |
private val viewMutableStateFlow: MutableStateFlow<ViewState>, | |
private val eventChannel: Channel<Event>, | |
) : ReadOnlyProperty<Any, Model<ViewState, Action, Mutation, Event>> { | |
override fun getValue(thisRef: Any, property: KProperty<*>) = | |
Model( | |
actionProcessors = actionProcessors, | |
reducers = reducers, | |
coroutineScope = viewModel.viewModelScope, | |
viewMutableStateFlow = viewMutableStateFlow, | |
eventChannel = eventChannel, | |
) | |
} | |
inline fun <reified ViewState, reified Action, reified Mutation, reified Event> ViewModel.model( | |
private val actionProcessor: Collection<ActionProcessor<Action, Mutation, Event>>, | |
private val reducers: Collection<Reducer<Mutation, ViewState>>, | |
private val initialState: ViewState, | |
) = | |
ModelProperty( | |
viewModel = this, | |
actionProcessors = actionProcessors, | |
reducers = reducers, | |
viewMutableStateFlow = MutableStateFlow(initialState), | |
eventChannel = Channel(Channel.BUFFERED), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment