Skip to content

Instantly share code, notes, and snippets.

View cbeyls's full-sized avatar

Christophe Beyls cbeyls

View GitHub Profile
@cbeyls
cbeyls / StateDataStore.kt
Created June 9, 2024 15:40
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
@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