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 / LuhnValidator.kt
Created October 25, 2019 11:53
KOTLIN function to validate credit card numbers
class LuhnValidator {
fun isValid(cardNumber: String): Boolean {
var s1 = 0
var s2 = 0
val reverse = StringBuffer(cardNumber).reverse().toString()
for (i in reverse.indices) {
val digit = Character.digit(reverse[i], 10)
when {
i % 2 == 0 -> s1 += digit
else -> {
@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)
}
@EmmanuelGuther
EmmanuelGuther / ApiRefreshToken.kt
Last active May 15, 2023 09:01
Retrofit builder to handle calls with authorization requirements and interceptor with refresh token.
interface ApiRefreshToken {
companion object {
private const val REFRESH_TOKEN = "/refreshToken"
}
@FormUrlEncoded
@POST(REFRESH_TOKEN)
fun refreshToken(@Field("refreshToken") refreshToken: String?): Call<TokenModel>
}
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 / BadgeTabLayout.kt
Last active December 14, 2022 03:08
Add badge to tab layout
package es.cityride.mantapp
import android.content.Context
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.support.design.widget.TabLayout
import android.util.AttributeSet
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
@EmmanuelGuther
EmmanuelGuther / AndroidToolbarActivityTest.txt
Created December 13, 2017 10:34
Test your toolbar back button espresso
To not depend on the app locale, you can use the code from Matt Logan by replacing "Navigate up" with R.string.abc_action_bar_up_description:
onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());
This helped me a lot because I have an app in more than 5 languages and I had to act like this.