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
val snapShot = CaptureBitmap {
//.. wite composable you want to capture
// it would be visible on view as well
}
// Caution : needs to be done on click action
// ui must be visible/laid out before calling this
val bitmap = snapShot.invoke()
@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
package com.proptiger.ui.components.edittext
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
/**
* Represent ViewTypes which user provides
*/
sealed class ViewType {
object ListItemHeader : ViewType()
object ListItemContent : ViewType()
}
/**
* Test View Holders
class LayoutManager(private val recycler: Recycler) {
fun getViewForPosition(pos: Int): View {
return recycler.getView(pos)
}
}
class Recycler(
private val adapter: Adapter,
private val recyclerPool: RecyclerPool,
private val viewCache: ViewCache
class LayoutManager(private val recycler: Recycler) {
fun getViewForPosition(pos: Int): View {
return recycler.getView(pos)
}
}
class RecyclerView(
private val adapter: Adapter,
private val recyclerPool: RecyclerPool,
private val viewCache: ViewCache
// layout manager calls function getViewForPosition over recycler
// thus has a recycler.
class LayoutManager(private val recycler: Recycler) {
fun getViewForPosition(pos: Int): View {
return recycler.getView(pos)
}
}
class RecyclerView(
private val viewCache: ViewCache
/**
* Layout Manager :
* - Position the Views
* - Scrolling Views
*/
class LayoutManager
/**
* Recycler View :
* - Coordinator of all component
* - Interaction with Elements
@ch8n
ch8n / threadSafeSingleton.kt
Last active July 17, 2021 09:27
legacy Singleton threadsafe
class SingletonThreadSafe private constructor() {
companion object {
private var _instance: SingletonThreadSafe? = null
fun getInstance(): SingletonThreadSafe {
val cachedInstance = _instance
return cachedInstance ?: synchronized(this) {
cachedInstance ?: SingletonThreadSafe().also {
_instance = it
}
}
...
// it looks ugly 😵 IMO
fun setup(binding:MainLayoutBinding?) = binding?.run {
textName.text = "ch8n"
textTwitter.text = "twitter@ch8n2"
}