dave08
05/08/2023, 2:47 PMintent { }
block (like from a Compose button?Guilherme Delgado
05/08/2023, 2:50 PMpostSideEffect
and reduce
can only be called inside ìntent
. You can have a public fun from the ViewModel that does thatdave08
05/08/2023, 2:53 PMcontainterHost.container.intent { postSideEffect(...) }
straight from the button callback. But you're right, I don't know why I thought I saw it exist on the ContainerHost...Guilherme Delgado
05/08/2023, 2:53 PMGuilherme Delgado
05/08/2023, 2:54 PMdave08
05/08/2023, 2:55 PMshould hoist everythingI made an interface of my ContainerHost's action functions that I pass down through the composables... or do you think it's better to have a bunch of lambdas in the function params?
Guilherme Delgado
05/08/2023, 2:57 PM@Composable
fun OverviewScreen(
...
viewModel: OverviewViewModel = hiltViewModel(),
) {
...
with(viewModel.state.collectAsStateWithLifecycle().value) {
MyComposalbe(
...
onLoadMore = { viewModel.loadMore() },
onDelete = { viewModel.deleteWorkOrder(it) },
)
}
}
LaunchedEffect(Unit) {
viewModel.sideEffect.collect {
...
}
}
}
Guilherme Delgado
05/08/2023, 2:57 PMGuilherme Delgado
05/08/2023, 2:58 PMfun deleteWorkOrder(id: Long) {
containerHost.intent {
...
reduce { state.copy(...) }
postSideEffect( ... )
}
}
}
}
Guilherme Delgado
05/08/2023, 2:58 PMGuilherme Delgado
05/08/2023, 2:58 PMGuilherme Delgado
05/08/2023, 2:59 PMprotected val containerHost = containerHostVisibilityWrapper<STATE, SIDE_EFFECT>(initialState, savedStateHandle) { initContainerHost() }
val state: StateFlow<STATE> = containerHost.container.stateFlow
val sideEffect: Flow<SIDE_EFFECT> = containerHost.container.sideEffectFlow
Guilherme Delgado
05/08/2023, 2:59 PMGuilherme Delgado
05/08/2023, 2:59 PMdave08
05/08/2023, 3:02 PMGuilherme Delgado
05/08/2023, 3:04 PMdave08
05/08/2023, 3:04 PMinterface Actions {
fun onLoadMore(...)
fun onDelete (...)
}
class ContainerHostImpl : ContainerHost..., Actions {
override fun onLoadMore(...) = intent {...}
...
}
Guilherme Delgado
05/08/2023, 3:06 PM/**
* Just to keep it private without public access.
*/
fun <STATE : Parcelable, SIDE_EFFECT : Any> ViewModel.containerHostVisibilityWrapper(
initialState: STATE,
savedStateHandle: SavedStateHandle,
onCreate: ((state: STATE) -> Unit)? = null
) =
object : ContainerHost<STATE, SIDE_EFFECT> {
override val container =
this@containerHostVisibilityWrapper.container<STATE, SIDE_EFFECT>(initialState, savedStateHandle = savedStateHandle, onCreate = onCreate)
}