Skip to content

Instantly share code, notes, and snippets.

View ch8n's full-sized avatar
💻
Always on to writing my next article.

Chetan Gupta ch8n

💻
Always on to writing my next article.
View GitHub Profile
@ch8n
ch8n / gist:e35244707bbf4b769970bc6caabd76ad
Last active December 5, 2023 17:09
Sharing Custom Logic with Compose
// create properties
data class SharedToolbarProps(
val title: String,
val subtitle: String,
val isMenuIconVisible: Boolean,
val onMenuIconClicked: () -> Unit
) {
companion object {
val default: SharedToolbarProps = SharedToolbarProps(
title = "Lorem Title",
@ch8n
ch8n / Scrollbar.kt
Created November 11, 2022 06:10 — forked from mxalbert1996/Scrollbar.kt
Modifiers to draw scrollbars in Jetpack Compose
/*
* MIT License
*
* Copyright (c) 2022 Albert Chang
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@ch8n
ch8n / destructureBackground.kt
Created October 18, 2022 04:35
Equivalent code
@Composable
fun Foo() {
val countState = remember { mutableStateOf(0) }
Button(onClick = {
val count = countState.value //👈 count is 0
val setCount = { lastestCount: Int ->
countState.value = lastestCount
@ch8n
ch8n / delegate state.kt
Last active October 21, 2022 22:12
Delegate mutable state
// Create a counter state
var count by remember {
mutableStateOf(value = 0)
}
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.White),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
@ch8n
ch8n / onClick.kt
Created October 17, 2022 13:40
button click destruct
Button(
onClick = {
// thing that is happening is :
setCount(count + 1) //👉 0 + 1
setCount(count + 1) //👉 0 + 1
}
) {
Text(text = "Increment by 2")
} // button end
@ch8n
ch8n / destructure2.kt
Created October 17, 2022 13:39
increment by 2 using destrcuture
// Create a counter state
val(count, setCount) = remember {
mutableStateOf(0)
}
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.White),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
@ch8n
ch8n / increment1-destruct.kt
Created October 17, 2022 13:38
increment by 1 using destructure
// Create a counter state
val(count, setCount) = remember {
mutableStateOf(0)
}
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.White),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
@ch8n
ch8n / stateVs.kt
Created October 17, 2022 13:37
delegate Vs destructuring
val(isChecked, setChecked) = remember {
mutableStateOf(value = false)
}
Checkbox(
checked = isChecked,
onCheckedChange = setChecked
)
// vs
@ch8n
ch8n / readwrite.kt
Created October 17, 2022 13:36
read and write from state delegate
// dont forget these imports
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
// for read only from state
val count by mutableStateOf(0)
// for read and write on state
var count by mutableStateOf(0)
@ch8n
ch8n / delegate.kt
Created October 17, 2022 13:36
state and mutable state delegates
inline operator fun <T> State<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value
inline operator fun <T> MutableState<T>.setValue(thisObj: Any?, property: KProperty<*>, value: T) {
this.value = value
}