Skip to content

Instantly share code, notes, and snippets.

View cbeyls's full-sized avatar

Christophe Beyls cbeyls

View GitHub Profile
@cbeyls
cbeyls / LoopingAnimatedVectorPainterResources.kt
Created April 27, 2025 17:04
Looping animated vector image for Compose (experimental)
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package be.digitalia.compose.animation.graphics
import androidx.compose.animation.core.SeekableTransitionState
import androidx.compose.animation.core.rememberTransition
import androidx.compose.animation.graphics.vector.AnimatedImageVector
import androidx.compose.animation.graphics.vector.StateVectorConfig
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
@cbeyls
cbeyls / PaddingValuesExtensions.kt
Created March 6, 2025 13:31
PaddingValues plus operator
package be.digitalia.compose.ui
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Stable
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
operator fun PaddingValues.plus(other: PaddingValues): PaddingValues =
CompositePaddingValues(this, other)
@cbeyls
cbeyls / JsonStreamingConverterFactory.kt
Created January 5, 2025 16:04
A Retrofit Converter.Factory using the Json format from the Kotlin Serialization library, optimized to stream response bodies
package be.digitalia.retrofit2
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.serializer
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
@cbeyls
cbeyls / LazyRequestBodyConverterFactory.kt
Last active January 5, 2025 15:38
A Retrofit Converter.Factory which lazily serializes the RequestBody handled by the next factory to ensure serialization does not occur on the main thread
package be.digitalia.retrofit2
import okhttp3.MediaType
import okhttp3.RequestBody
import okio.BufferedSink
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type
/**
@cbeyls
cbeyls / StateDataStore.kt
Last active January 2, 2025 13:07
DataStore wrapper for instant updates
package be.digitalia.common.flow
import androidx.datastore.core.DataStore
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
@cbeyls
cbeyls / MaterialClickableText.kt
Created December 18, 2023 23:41
Material 2 ClickableText
@Composable
fun MaterialClickableText(
text: AnnotatedString,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
@cbeyls
cbeyls / OptimizedDesugaring.kts
Created December 1, 2023 14:25
Automatically patch Android desugaring configuration to set "support_all_callbacks_from_library" flag to false
fun patchDesugarConfig(config: Property<String>) {
val defaultConfig = config as org.gradle.api.internal.provider.DefaultProperty<String>
val patchedDesugarConfig = defaultConfig.provider.map {
it.replace(
"\"support_all_callbacks_from_library\":true",
"\"support_all_callbacks_from_library\":false"
)
}
config.set(patchedDesugarConfig)
}
@cbeyls
cbeyls / ScrollableAlertDialog.kt
Created April 20, 2023 18:31
Material 2 AlertDialog for Jetpack Compose with support for scrollable content on overflow
package be.digitalia.common.ui
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ContentAlpha
import androidx.compose.material.LocalContentAlpha
@cbeyls
cbeyls / ViewLifecycleLazy.kt
Last active July 8, 2024 11:04
A lazy property that gets cleaned up when the fragment's view is destroyed
package be.digitalia.common.fragment
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
/**
* A lazy property that gets cleaned up when the fragment's view is destroyed.
@cbeyls
cbeyls / FlowExt.kt
Created May 28, 2022 16:32
Synchronize Flow emissions with SharedFlow's subscriptionCount
package be.digitalia.flow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow