Skip to content

Instantly share code, notes, and snippets.

View arriolac's full-sized avatar
🌍

Chris Arriola arriolac

🌍
View GitHub Profile
// 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
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun DropDownCheckboxMenu(
items: List<String>,
selectedItems: Set<Int>,
onItemToggled: (Boolean, Int) -> Unit,
modifier: Modifier = Modifier
) {
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
class DropDownCheckboxMenuView(context: Context) : View(context) {
/**
* The set of selected items in the menu
*/
fun setSelectedItems(items: Set<Int>) { /* ... */ }
@arriolac
arriolac / CheckBox.kt
Last active October 10, 2023 17:44
Refactoring CheckBox to not be stateful
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
class CheckBox(context: Context) : View(context) {
override fun performClick(): Boolean {
// Refactor: prevent the checkbox from toggling when clicked
// toggleCheck()
return super.performClick()
}
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
var checked by remember { mutableStateOf(false)}
Checkbox(checked = checked, onCheckedChange = {
checked = it
})
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun DropDownCheckboxMenu(
items: List<String>,
onItemToggled: (Boolean, Int) -> Unit,
modifier: Modifier = Modifier
) {
AndroidView(
@arriolac
arriolac / lib_compose_benefits.md
Last active October 10, 2023 17:22
Benefits of adding explicit support for Compose for your View-based library
Benefit Summary
Improve the developer experience Allow your consumers to take advantage of the benefits of Compose by providing an intuitive and declarative API for integrating your library.
Reduce boilerplate code Reduce the code your consumers have to write when using your library. Instead of having to use interoperability APIs, your consumers can directly use a declarative API through composable functions you provide.
Fewer bugs It can be challenging to switch between Compose's declarative style of thinking and View's imperative style of thinking. As a result, when consumers write interoperability code to interact with your View-based library, they can easily introduce bugs. Adding support for Compose guarantees correct interoperability, as this is not the responsibility of the consumer anymore.
Future-proof Compose is the modern recommended UI framework for building Android apps. By supporting Compose, you are preparing your library for a Compose-first fut
@arriolac
arriolac / DropDownCheckboxMenuUsage.kt
Last active October 10, 2023 17:29
DropDownCheckboxMenu composable
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
DropDownCheckboxMenu(
items = items,
onItemToggled = onItemToggled,
modifier = Modifier.width(400.dp)
)
@arriolac
arriolac / DropDownCheckboxMenuViewAndroidView.kt
Created October 10, 2023 17:08
Using DropDownCheckboxMenuView in Compose using AndroidView
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
AndroidView(
factory = { context ->
// Create DropDownCheckboxMenuView here
DropDownCheckboxMenuView(context)
},
update = { view ->
// Update DropDownCheckboxMenuView when `items` or `onItemToggled` changes here
@arriolac
arriolac / DropDownCheckboxMenuView.kt
Last active October 10, 2023 17:06
API for DropDownCheckboxMenuView
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
class DropDownCheckboxMenuView(context: Context) : View(context) {
/**
* The set of items to display in this menu
*/
fun setItems(items: List<String>) { /* ... */ }