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 / MutableState.kt
Created October 17, 2022 13:35
Mutable state signature
@Stable
interface MutableState<T> : State<T> {
override var value: T
operator fun component1(): T
operator fun component2(): (T) -> Unit
}
@ch8n
ch8n / headRecursion.kt
Created September 3, 2022 18:37
Head Recursion - Kotlin
fun main() {
val list = listOf(1, 2, 3, 4, 5, 6)
list.headRecursion { print(it) }
}
fun <T> List<T>.headRecursion(index: Int = lastIndex, next: (item: T) -> Unit) {
if (index >= 0) {
headRecursion(index - 1, next)
next.invoke(get(index))
}
@ch8n
ch8n / Download_Manager.kt
Created February 1, 2022 18:32
Sealed class for Download Manager Status
sealed class DownloadManagerStatus {
sealed class DownloadStatus : DownloadManagerStatus() {
sealed class StatusPaused : DownloadStatus() {
object QueuedForWifi : StatusPaused()
object WaitingForNetwork : StatusPaused()
object WaitingToRetry : StatusPaused()
object Unknown : StatusPaused()
}
@ExperimentalComposeUiApi
@Composable
fun OtpBugView() {
val (editValue, setEditValue) = remember { mutableStateOf("") }
val otpLength = remember { 4 }
val focusRequester = remember { FocusRequester() }
val keyboard = LocalSoftwareKeyboardController.current
// hidden TextField for controlling OTP Cells UI
@Composable
fun OtpCell(
modifier: Modifier,
value: String,
isCursorVisible: Boolean = false
) {
val scope = rememberCoroutineScope()
val (cursorSymbol, setCursorSymbol) = remember { mutableStateOf("")
@Composable
fun CaptureBitmap(
content: @Composable ()->Unit
) : () -> Bitmap // <---- this will return a callback which returns a bitmap
{
val composeView = ComposeView(...)
//callback that convert view to bitmap
fun captureBitmap() = composeView.drawToBitmap()
@Composable
fun CaptureBitmap(
content: @Composable ()->Unit // <--- we use callback as parameter
){
val composeView = ComposeView(...)
AndroidView(
factory = {
composeView.apply {
@Composable
fun CaptureBitmap(...){
// we will use compose view as target to get bitmap
val composeView = ComposeView(...)
// put compose native view to compose ui using AndroidView
AndroidView(
factory = {
composeView.apply {
// Native View which accepts Composable as its View Content
ComposeView(context).apply {
setContent {
// composable goes here!
}
}
Coloumn {
val snapShot = CaptureBitmap {
Button(
onClick = {},
modifier = Modifier
.fillMaxWidth()
.padding(24.dp)
.height(55.dp),
) {
Text(text = "Capture my image")