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
class MyCustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : AbstractComposeView(context, attrs, defStyle) {
init {
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
class MyFragment : Fragment() {
// …
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = ComposeView(requireContext()).apply {
@arriolac
arriolac / composeview_setviewcompositionstrategy.kt
Created May 4, 2023 20:50
How to change the ViewCompositionStrategy of a ComposeView
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// Change ViewCompositionStrategy to DisposeOnViewTreeLifecycleDestroyed
val composeView = ComposeView(context = context)
composeView.setViewCompositionStrategy(
ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed
)
@arriolac
arriolac / vcs_table.md
Created May 4, 2023 20:48
ViewCompositionStrategy Table
ViewCompositionStrategy Description
DisposeOnDetachedFromWindow The Composition will be disposed when the underlying ComposeView is detached from the window. Has since been superseded by DisposeOnDetachedFromWindowOrReleasedFromPool.

Interop scenario:

* ComposeView whether it’s the sole element in the View hierarchy, or in the context of a mixed View/Compose screen (not in Fragment).
DisposeOnDetachedFromWindowOrReleasedFromPool (Default) Similar to DisposeOnDetachedFromWindow, when the Composition is not in a pooling container, such as a RecyclerView. If it is in a pooling container, it will dispose when either the pooling container itself detaches from the window
@arriolac
arriolac / SunflowerNavGraph.kt
Last active January 30, 2023 21:02
The navigation graph for Sunflower
// Copyright 2023 Google LLC.
// SPDX-License-Identifier: Apache-2.0
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "home") {
composable("home") {
HomeScreen(/* ... */)
}
composable(
"plantDetail/{plantId}",
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Preview(
name = "Small font",
group = "Font scales",
fontScale = 0.5f
)
@Preview(
name = "Large font",
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun SurveyAnswer() {
Row {
Image(painterResource(id = R.drawable.spark), contentDescription = "")
Text(text = "Spark", color = Color(0xFF448866))
RadioButton(selected = false, onClick = { /* … */ })
}
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun SurveyAnswer() {
Image(painterResource(id = R.drawable.lenz), contentDescription = "")
}
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun SurveyAnswer() {
// ...
}
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
@Composable
fun SurveyAnswer(answer: Answer) {
Surface(
border = BorderStroke(
1.dp,
MaterialTheme.colorScheme.outline
),