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 / AlertDialogAndroidkotlinExtension.kt
Last active October 3, 2023 12:18
Android alert dialog kotlin Extension
//Utils - Declaration
object PromptUtils {
fun alertDialog(context: Context, @StyleRes style: Int, dialogBuilder: AlertDialog.Builder.() -> Unit): Dialog {
val builder = AlertDialog.Builder(context, style).also {
it.setCancelable(false)
it.dialogBuilder()
}
return builder.create()
}
@ch8n
ch8n / CaptureComposableAsBitmap.kt
Created October 16, 2021 10:26
Covert Composable into View then returns callback to get bitmap
@Composable
fun CaptureBitmap(
content: @Composable () -> Unit,
): () -> Bitmap {
val context = LocalContext.current
/**
* ComposeView that would take composable as its content
* Kept in remember so recomposition doesn't re-initialize it
@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 / 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 / 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 / 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