Skip to content

Instantly share code, notes, and snippets.

@chenzhang2006
chenzhang2006 / ComposeStateSkeleton.kt
Last active August 12, 2022 16:17
Compose State Skeleton
val listState = rememberLazyListState()
// Horizontal List
LazyRow(state = listState) { ... }
// Vertical List
LazyColumn(state = listState) { ... }
@chenzhang2006
chenzhang2006 / ComposeListsShareState.kt
Last active August 12, 2022 16:17
Compose Row & Column Share Same LazyListState
val items: List<Room> = rooms
val listState = rememberLazyListState()
// Horizontal List
LazyRow(state = listState) {
items(items = items) { item -> RoomItem(item) }
}
// Vertical List
LazyColumn(state = listState) {
@chenzhang2006
chenzhang2006 / RecyclerViewPositionSync.kt
Last active August 1, 2022 20:00
RecyclerView Sync Scroll Position
// Adapter and data set
class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { ... }
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { ... }
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { ... }
override fun getItemCount() = dataSet.size
}
val rooms: List<Room> = rooms
val recyclerViewAdapter = CustomAdapter(rooms)
val backdropState = rememberBackdropScaffoldState(BackdropValue.Concealed)
val offset by backdropState.offset
val halfHeightDp = LocalConfiguration.current.screenHeightDp / 2
val halfHeightPx = with(LocalDensity.current) { halfHeightDp.dp.toPx() }
//...
// Current alpha for vertical list
val verticalListAlpha = ((halfHeightPx - offset) / halfHeightPx).coerceIn(0f..1f)
@chenzhang2006
chenzhang2006 / RevealBackdrop.kt
Created March 30, 2022 20:12
Side Effect to Reveal Backdrop
// BackdropScaffoldState to be observed by Compose Runtime
val backdropState = rememberBackdropScaffoldState(BackdropValue.Concealed)
//Launch coroutine to animate revealling backdrop
LaunchedEffect(backdropState) {
backdropState.reveal()
}
@chenzhang2006
chenzhang2006 / CrashReportingTree.kt
Last active February 28, 2022 14:54
Firebase Timber tree
class CrashlyticsReportTree : Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (priority == Log.ERROR) { // only for error level
with(Firebase.crashlytics) {
// optional: setCustomKey("CUSTOME_TAG", any)
recordException(it)
}
}
}
}
@chenzhang2006
chenzhang2006 / GqlApolloHelper.kt
Last active April 1, 2022 17:38
Helper function for GQL
import com.apollographql.apollo.api.Response
internal suspend inline fun <S> runApolloRequest(block: () -> Response<S>): Result<S> = try {
val value = handleApolloRequest{(block()}
Result.success(value)
} catch (e: Exception) {
if (e is CancellationException) throw e
Timber.e("Exception from coroutine (${coroutineContext[CoroutineName]?.name}): " +
"${e.stackTraceToString()}")
Result.failure(e)
@chenzhang2006
chenzhang2006 / FlowHelper.kt
Last active April 1, 2022 17:36
Helper function for Flow with default value
inline fun <T> Flow<T>.catchLog(default: T? = null): Flow<T> = this.catch { e ->
if (e is CancellationException) throw e
Timber.e(
e,
"Error from flow (${coroutineContext[CoroutineName]?.name}): " +
"${e.stackTraceToString()}"
)
default?.let { emit(it) }
}
@chenzhang2006
chenzhang2006 / FullHelper.kt
Last active April 1, 2022 17:33
Full helper to handle exceptions
internal suspend inline fun <T> getResult(block: () -> T): Result<T> = try {
block().let { Result.success(it) }
} catch (e: Exception) {
if (e is CancellationException) throw e
Timber.e("Error from coroutine (${coroutineContext[CoroutineName]?.name}): " +
"${e.stackTraceToString()}")
Result.failure(e)
}
@chenzhang2006
chenzhang2006 / CoroutineWithName.kt
Last active February 18, 2022 22:19
Specify coroutine name at coroutine builder
viewModelScope.launch(CoroutineName("GetFavorites")) {
postsRepository.observeFavorites().collect { favorites ->
viewModelState.update { it.copy(favorites = favorites) }
}
}
// or within your custom coroutine scope
launch(CoroutineName("GetFavorites")) {
postsRepository.observeFavorites().collect { favorites ->
viewModelState.update { it.copy(favorites = favorites) }