Skip to content

Instantly share code, notes, and snippets.

Avatar

Christophe Beyls cbeyls

View GitHub Profile
@cbeyls
cbeyls / FlowLayout.kt
Created Sep 17, 2020
FlowLayout implementation for Jetpack Compose
View FlowLayout.kt
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 Aug 1, 2020
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.
View LayerCrossfadeTransition.kt
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
Example of type-safe Fragment result contract extensions
View TypedFragmentResults.kt
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 May 7, 2020
Extension function to enforce a single scroll direction for a RecyclerView
View RecyclerViewExt.kt
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 Jan 3, 2020
Utility class to enforce a single scroll direction for a RecyclerView or a ViewPager2
View RecyclerViewUtils.java
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 {
@cbeyls
cbeyls / BinaryTrees.kt
Created Aug 18, 2019
Kotlin implementation of the binary-trees benchmark from The Computer Language Benchmarks Game
View BinaryTrees.kt
/**
* The Computer Language Benchmarks Game
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
*
* based on Jarkko Miettinen's Java program
* contributed by Christophe Beyls
*/
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
@cbeyls
cbeyls / Parcelers.kt
Created Jun 18, 2019
Kotlin Parcelize extensions
View Parcelers.kt
package be.digitalia.sample
import android.os.Parcel
import kotlinx.android.parcel.Parceler
import java.math.BigDecimal
import java.math.BigInteger
import java.util.*
inline fun <T> Parcel.readNullable(reader: () -> T) =
if (readInt() != 0) reader() else null
View DogRiddle.kt
sealed class Animal {
// Cats are simple. A cat is a cat.
object Cat : Animal()
sealed class Dog : Animal() {
abstract val breed: String?
class DogWithBreed(override val breed: String) : Dog()
@cbeyls
cbeyls / LiveDataFactory.java
Created Apr 27, 2019
A Factory providing utility LiveData instances
View LiveDataFactory.java
package be.digitalia.common.livedata;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import java.util.concurrent.TimeUnit;
@cbeyls
cbeyls / ComputableLiveData.java
Created Mar 11, 2018
A LiveData class that can be invalidated & computed on demand (based on an internal architecture components class)
View ComputableLiveData.java
package be.digitalia.arch.lifecycle;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.os.AsyncTask;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
You can’t perform that action at this time.