Skip to content

Instantly share code, notes, and snippets.

@chenzhang2006
chenzhang2006 / StateFlow.kt
Last active December 9, 2022 17:53
StateFlow to observe LoginState
private val _loginState = MutableStateFlow<Boolean>(false)
val loginStateFlow: StateFlow<Boolean> = _loginState.asStateFlow()
fun loggedIn() {
_loginState.value = true
}
fun loggedOut() {
_loginState.value = false
}
@chenzhang2006
chenzhang2006 / AccountRepository.kt
Last active December 19, 2022 01:10
Repository with in-memory cache to support concurrency
private val lock = Mutex() // #1
private var cachedAccount: Account? = null
suspend fun getAccount(): Account? {
return cachedAccount ?: mutex.withLock { // #2, #3, #6
cachedAccount ?: withContext(Dispatchers.IO) { // #4, #5, #6
val networkModel = ... // perform network query and response parsing
cachedAccout = networkModel.mapToAccountDomain()
cachedAccount
}
@chenzhang2006
chenzhang2006 / IntroBounce.kt
Last active October 19, 2022 00:02
Introductory Bounce Effect
val backdropState = rememberBackdropScaffoldState(Revealed)
var offset by (backdropState.offset as MutableState)
// Optional conditions: ex. should-bounce flag, data-loading finished, etc.
if (backdropState.isRevealed) {
// remember the original offset position to go back to
val revealedOffset = remember { backdropState.offset.value }
// value holder Animatable for the actual offset
val offsetAnimatable = remember { Animatable(revealedOffset) }
@chenzhang2006
chenzhang2006 / ComposableFunction.kt
Created August 4, 2022 12:19
Composable Function
@Composable
fun FunctionName(input: T) {
...
}
@chenzhang2006
chenzhang2006 / LazyColumn.kt
Last active August 3, 2022 19:02
Compose LazyColumn
val items: List<Room> = rooms
val listState = rememberLazyListState()
LazyColumn(state = listState) {
items(items = items) { item -> RoomItem(item) }
}
@Composable
fun RoomItem(item: Room) {
Text(...)
@chenzhang2006
chenzhang2006 / BindRecyclerView.kt
Created August 3, 2022 18:47
RecyclerView Binding
val rooms: List<Room> = rooms
val recyclerViewAdapter = CustomAdapter(rooms)
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = recyclerViewAdapter
@chenzhang2006
chenzhang2006 / CustomAdapter.kt
Created August 3, 2022 18:44
RecyclerView Adapter
class CustomAdapter(private val dataSet: Array<String>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView
init {
textView = view.findViewById(R.id.textView)
}
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(viewGroup.context)
@chenzhang2006
chenzhang2006 / row_item.xml
Last active August 3, 2022 18:35
RecyclerView Row Item
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/element_text"/>
</FrameLayout>
@chenzhang2006
chenzhang2006 / recycler_view.xml
Created August 3, 2022 18:30
RecyclerView xml
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
@chenzhang2006
chenzhang2006 / RecyclerViewStateSyncSkeleton.kt
Last active August 1, 2022 20:01
RecyclerView State Sync Skeleton
// Horizontle RecyclerView
val horizontalRecyclerView: RecyclerView = findViewById(R.id.horizontal_recycler_view)
val horizontalLayoutManager: LinearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
horizontalRecyclerView.layoutManager = horizontalLayoutManager
// Vertical RecyclerView
val verticalRecyclerView: RecyclerView = findViewById(R.id.vertical_recycler_view)
val verticalLayoutManager: LinearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
verticalRecyclerView.layoutManager = verticalLayoutManager