Skip to content

Instantly share code, notes, and snippets.

View cbeyls's full-sized avatar

Christophe Beyls cbeyls

View GitHub Profile
@cbeyls
cbeyls / OptimizedDesugaring.groovy
Last active April 20, 2023 19:07
Automatically patch Android desugaring configuration to set "support_all_callbacks_from_library" flag to false
static def patchDesugarConfig(Property<String> config) {
def patchedDesugarConfig = config.getProvider().map {
it.replace(
"\"support_all_callbacks_from_library\":true",
"\"support_all_callbacks_from_library\":false"
)
}
config.set(patchedDesugarConfig)
}
afterEvaluate {
@cbeyls
cbeyls / LifecycleViewModel.kt
Last active January 1, 2022 20:38
Extension to add a CompositeLifecycleOwner property to a ViewModel
package androidx.lifecycle
import be.digitalia.utils.CompositeLifecycleOwner
private const val VIEW_LIFECYCLE_KEY = "androidx.lifecycle.VIEW_LIFECYCLE"
val ViewModel.lifecycleOwner: CompositeLifecycleOwner
get() {
return getTag(VIEW_LIFECYCLE_KEY)
?: setTagIfAbsent(VIEW_LIFECYCLE_KEY, CompositeLifecycleOwner())
@cbeyls
cbeyls / CompositeLifecycleOwner.kt
Last active January 1, 2022 20:34
A LifecycleOwner composed of children LifecycleOwners
package be.digitalia.utils
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import java.io.Closeable
@cbeyls
cbeyls / ignore-jetified-proguard-rules.groovy
Created October 28, 2021 09:52
Ignore Proguard rules of jetified library dependencies
// Add the following to your Android app module build.gradle file, after the android section:
// Replace "proguard.txt" with the actual name of the proguard rules file in the library aar, if different
afterEvaluate {
// Update configuration of the minify tasks to exclude specific consumer Proguard rules files
tasks.findAll { task -> task.name.startsWith('minify') }.each { task ->
task.configure {
configurationFiles.from.collect().each { fileCollection ->
if (fileCollection instanceof FileCollection) {
configurationFiles.from.remove fileCollection
@cbeyls
cbeyls / StickyHeaderLinearLayoutManager.kt
Created September 27, 2021 13:03
Sticky headers LinearLayoutManager, adapted from Epoxy
package be.digitalia.util.stickyheader
import android.content.Context
import android.graphics.PointF
import android.os.Parcel
import android.os.Parcelable
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import androidx.recyclerview.widget.LinearLayoutManager
@cbeyls
cbeyls / FlowLayout.kt
Created September 17, 2020 00:30
FlowLayout implementation for Jetpack Compose
package be.digitalia.compose.layout
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Layout
import androidx.compose.ui.Modifier
@Composable
fun FlowLayout(
modifier: Modifier = Modifier,
@cbeyls
cbeyls / LayerCrossfadeTransition.kt
Last active August 1, 2020 02:32
A Transition for the Coil image loader that crossfades from the current drawable to a new one, and enables a hardware layer for the duration of the animation if the view is not opaque.
package be.digitalia.bifff.coil
import android.view.View
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import coil.annotation.ExperimentalCoilApi
import coil.decode.DataSource
import coil.drawable.CrossfadeDrawable
import coil.request.ErrorResult
import coil.request.RequestResult
@cbeyls
cbeyls / TypedFragmentResults.kt
Created May 19, 2020 20:55
Example of type-safe Fragment result contract extensions
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentResultListener
import androidx.lifecycle.LifecycleOwner
interface FragmentResultContract<T> {
fun toBundle(result: T): Bundle
@cbeyls
cbeyls / RecyclerViewExt.kt
Last active February 8, 2024 19:17
Extension function to enforce a single scroll direction for a RecyclerView
package be.digitalia.samples.utils
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener
import kotlin.math.abs
fun RecyclerView.enforceSingleScrollDirection() {
val enforcer = SingleScrollDirectionEnforcer()
addOnItemTouchListener(enforcer)
@cbeyls
cbeyls / RecyclerViewUtils.java
Last active May 2, 2025 06:23
Utility class to enforce a single scroll direction for a RecyclerView or a ViewPager2
package be.digitalia.samples.utils;
import android.view.MotionEvent;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
public class RecyclerViewUtils {