Skip to content

Instantly share code, notes, and snippets.

@arriolac
Created October 10, 2023 17:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arriolac/8ef8a472497e8fff100d83b82981bef3 to your computer and use it in GitHub Desktop.
Save arriolac/8ef8a472497e8fff100d83b82981bef3 to your computer and use it in GitHub Desktop.
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
/**
* State holder for DropDownCheckBoxMenu
*/
class DropDownCheckboxMenuState(
var items: List<String>,
) {
// Internal set of selected items managed by wrapper
internal var _selectedItems by mutableStateOf(emptySet<Int>())
// External read-only set of selected items in the menu
public val selectedItems: Set<Int>
get() = _selectedItems
}
@Composable
fun DropDownCheckboxMenu(
state: DropDownCheckboxMenuState,
onItemToggled: (Boolean, Int) -> Unit,
modifier: Modifier = Modifier
) {
val currentOnItemToggled by rememberUpdatedState(newValue = onItemToggled)
AndroidView(
factory = {
// Create DropDownCheckboxMenuView here
val view = DropDownCheckboxMenuView(it)
view.setOnItemToggled { selected, index ->
if (selected) {
state._selectedItems += index
} else {
state._selectedItems -= index
}
currentOnItemToggled(selected, index)
}
view
},
// ...
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment