Skip to content

Instantly share code, notes, and snippets.

@uteke
Last active September 2, 2023 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uteke/1850cfa293b035ce311765138a486d48 to your computer and use it in GitHub Desktop.
Save uteke/1850cfa293b035ce311765138a486d48 to your computer and use it in GitHub Desktop.
Model delegate implementation
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