Is it possible to use a component into anothers without adding it as a child? #573
-
I have a ItemComponent, it's show the list of items and it own a store &... I want to show it as a bottomsheet or dialog any where in other components without adding ItemComponent as a child because it can called from more than 5 components. class ItemComponent(
componentContext: ComponentContext,
storeFactory: StoreFactory,
val selectMode: Boolean = false,
val searchValue: String?,
private val output: (Output) -> Unit
): ComponentContext by componentContext {
private val store =
instanceKeeper.getStore {
ItemStoreFactory(
storeFactory = storeFactory,
searchValue = searchValue,
selectMode = selectMode,
).create()
}
@OptIn(ExperimentalCoroutinesApi::class)
val state: StateFlow<ItemStore.State> = store.stateFlow
fun onEvent(event: ItemStore.Intent) {
store.accept(event)
}
fun onOutput(output: Output) {
output(output)
}
sealed class Output {
data class ItemsSelected(val items: List<Item>) : Output()
}
} @Composable
internal fun ItemSelectScreen(component: ItemComponent) {
val state by component.state.collectAsState()
ItemSelectContent(
state = state,
onEvent = component::onEvent,
onOutput = component::onOutput
)
} @OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun ItemSelectContent(
state: ItemStore.State,
onEvent: (ItemStore.Intent) -> Unit,
onOutput: (ItemComponent.Output) -> Unit,
) {
val modalBottomSheetState = rememberModalBottomSheetState()
ModalBottomSheet(
onDismissRequest = {
onOutput(ItemComponent.Output.ItemsSelected(state.selected))
},
sheetState = modalBottomSheetState,
dragHandle = { BottomSheetDefaults.DragHandle() },
) {
...
SimplePagingVerticalGrid(
itemList = state.list,
loadMoreItems = {
if (state.list.isEmpty()) return@SimplePagingVerticalGrid
val nextPage = state.page + 1
onEvent(ItemStore.Intent.LoadByPage(page = nextPage))
},
isLoading = state.isLoading,
) { item, brush ->
SimpleListItemContent(
onClick = {
if (state.selectMode) {
onEvent(ItemStore.Intent.UpdateSelected(item = item))
} else {
item.id?.let {
onEvent(ItemStore.Intent.Edit(id = item.id))
}
}
},
....
)
}
}
}
Now I want show this from every where I called, but I don't know what is a best practice to create a component instance and show it in UI. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
What prevents you from adding |
Beta Was this translation helpful? Give feedback.
Thanks for the detailed description. I believe there could be multiple solutions.
childSlot
function in hosting components…