Skip to content

Instantly share code, notes, and snippets.

@EmmanuelGuther
EmmanuelGuther / errorParcelableEncounteredIOExceptionWritingSerializableObject
Created April 10, 2024 10:02
android.os.BadParcelableException: Parcelable encountered IOException writing serializable object
android.os.BadParcelableException: Parcelable encountered IOException writing serializable object (name = com.xx.xx.auxiliar.ErrorData)
at android.os.Parcel.writeSerializable(Parcel.java:2797)
at android.os.Parcel.writeValue(Parcel.java:2563)
at android.os.Parcel.writeValue(Parcel.java:2362)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:1298)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1843)
at android.os.Bundle.writeToParcel(Bundle.java:1389)
@EmmanuelGuther
EmmanuelGuther / animateScrollAndCentralizeItemCompose.kt
Created May 31, 2023 10:56
Compose Animate Scroll And Centralize Item
fun LazyListState.animateScrollAndCentralizeItem(index: Int, scope: CoroutineScope) {
val itemInfo = this.layoutInfo.visibleItemsInfo.firstOrNull { it.index == index }
scope.launch {
when {
itemInfo != null -> {
val center = this@animateScrollAndCentralizeItem.layoutInfo.viewportEndOffset / 2
val childCenter = itemInfo.offset + itemInfo.size / 2
this@animateScrollAndCentralizeItem.animateScrollBy((childCenter - center).toFloat())
}
else -> {
@EmmanuelGuther
EmmanuelGuther / LazyColumnAnimationExample.kt
Created May 31, 2023 10:53
compose Animate lazycolumn
items(rankingUIModel.rankingUIModel.challengeRankingItems) {
ItemQualifying(modifier= Modifier.animateItemPlacement(
spring(
stiffness = Spring.StiffnessVeryLow,
visibilityThreshold = IntOffset.VisibilityThreshold
)
), item = it, onRankingItemPressed = onRankingItemPressed)
}
Viewmodel-->
private var _data: Channel<PagingData<Event>> = Channel()
var data = _data.receiveAsFlow()
fun callApiExample(){
usecaseFoo... .collect{ _data.send(it)}
}
Fragment-->
private val displayMetrics: DisplayMetrics by lazy { Resources.getSystem().displayMetrics }
/**
* Returns boundary of the screen in pixels (px).
*/
val screenRectPx: Rect
get() = displayMetrics.run { Rect(0, 0, widthPixels, heightPixels) }
/**
* Returns boundary of the screen in density independent pixels (dp).
.background(
brush = Brush.verticalGradient(
colors = listOf(
MaterialTheme.colors.primary,
MaterialTheme.colors.primary.copy(0.2f)
)
)
)
@EmmanuelGuther
EmmanuelGuther / composePreviewDevices.kt
Created October 13, 2022 12:46
jetpack compose preview devices
@Preview(name = "NEXUS_7", device = Devices.NEXUS_7)
@Preview(name = "NEXUS_7_2013", device = Devices.NEXUS_7_2013)
@Preview(name = "NEXUS_5", device = Devices.NEXUS_5)
@Preview(name = "NEXUS_6", device = Devices.NEXUS_6)
@Preview(name = "NEXUS_9", device = Devices.NEXUS_9)
@Preview(name = "NEXUS_10", device = Devices.NEXUS_10)
@Preview(name = "NEXUS_5X", device = Devices.NEXUS_5X)
@Preview(name = "NEXUS_6P", device = Devices.NEXUS_6P)
@Preview(name = "PIXEL_C", device = Devices.PIXEL_C)
@Preview(name = "PIXEL", device = Devices.PIXEL)
@EmmanuelGuther
EmmanuelGuther / gist:6ccdc51bf004a8a2cc605b209e890fc4
Created August 25, 2022 16:04
Coroutine Scope and dispatchers
Dispatchers.Default - Uses a thread pool equivalent to number of CPU cores. Should be used for CPU bound workload.
Dispatchers.IO - Uses a pool of 64 threads. Ideal for IO bound workload, where the thread is usually waiting; maybe for network request or disk read/write.
Dispatchers.Main (Android only): Uses main thread to execute the coroutines. Ideal for updating UI elements.
GlobalScope.launch(Dispatchers.IO): Launches a top-level coroutine on Dispatchers.IO. Coroutine is unbound and keeps running until finished or cancelled. Often discouraged since programmer has to maintain a reference to join() or cancel().
@Composable
fun TopAppBar(modifier: Modifier, backgroundColor: Color, onClickProfile: () -> Unit, onSearch: (String) -> Unit) {
TopAppBar(title = {
Row {
Text(
text = "AUDI APP STORE",
color = MaterialTheme.colors.onPrimary,
modifier = Modifier
.fillMaxWidth()
.height(60.dp)
@Composable
fun AnimatedText(text: String, modifier: Modifier, tween: Int = 1000, color: Color, textStyle: TextStyle) {
val textScale by rememberInfiniteTransition().animateFloat(
initialValue = 0.9f, targetValue = 1f, animationSpec = infiniteRepeatable(
animation = tween(tween), repeatMode = RepeatMode.Reverse
)
)
Text(
modifier = modifier.scale(textScale), text = text, color = color, style = textStyle
)