Skip to content

Instantly share code, notes, and snippets.

View Sottti's full-sized avatar
💡
"Success is simply a matter of luck. Ask any failure"

Pablo Costa Sottti

💡
"Success is simply a matter of luck. Ask any failure"
View GitHub Profile
val isInitialLoad = items.loadState.refresh is Loading && items.itemCount == 0
val isError = items.loadState.refresh is Error && items.itemCount == 0
val isListEmpty = items.loadState.refresh is NotLoading &&
items.loadState.append.endOfPaginationReached &&
items.itemCount == 0
internal class ChipStateProvider : PreviewParameterProvider<ChipState> {
override val values: Sequence<ChipState> = sequence {
leadingIconValues().forEach { leadingIcon ->
selectedValues().forEach { enabled ->
expandedValues().forEach { expanded ->
labelResIdValues().forEach { checked ->
yield(
ChipState(
expanded = expanded,
labelResId = checked,
import android.content.ComponentCallbacks
import android.content.Context
import android.content.res.Configuration
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.distinctUntilChanged
public fun <T> Context.observeConfigurationChanges(
@Sottti
Sottti / ColorFactory.kt
Created October 18, 2025 08:52
Tired of if (Build.VERSION.SDK_INT >= ...) checks cluttering your code? 🤔 The @ChecksSdkIntAtLeast annotation is a lifesaver! It tells the lint tool that a boolean property is your version check.
@Composable
@ReadOnlyComposable
public fun colors(
colorContrast: ResolvedColorContrast,
darkTheme: Boolean,
dynamicColor: ResolvedDynamicColor,
): ColorScheme = when {
dynamicColor.enabled -> dynamicColorScheme(darkTheme)
darkTheme -> colorContrast.darkColorScheme()
else -> colorContrast.lightColorScheme()
public fun Modifier.centerInParent(): Modifier =
this then layout { measurable, constraints ->
val maxWidthAllowedByParent = constraints.maxWidth
val maxHeightAllowedByTheParent = constraints.maxHeight
val placeable = measurable.measure(constraints)
val start = maxWidthAllowedByParent / 2 - placeable.width / 2
val top = maxHeightAllowedByTheParent / 2 - placeable.height / 2
layout(width = placeable.width, height = placeable.height) {
placeable.placeRelative(x = start, y = top)
}
context(viewModel: ViewModel)
public fun <T> Flow<T>.stateInWhileSubscribed(
initialValue: T,
): StateFlow<T> = stateIn(
scope = viewModel.viewModelScope,
started = WhileSubscribed(stopTimeoutMillis = 5_000),
initialValue = initialValue,
)
@Sottti
Sottti / Corner.kt
Last active September 20, 2025 09:21
A Jetpack Compose Material 3 Card that allows concave corners on top of the normal convex/sharp corners.
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.ZeroCornerSize
import androidx.compose.ui.unit.Dp
public sealed interface Corner {
public val cornerSize: CornerSize
public data class Concave(override val cornerSize: CornerSize) : Corner
public data class Rounded(override val cornerSize: CornerSize) : Corner
public data class Cut(override val cornerSize: CornerSize) : Corner
internal val PIXEL_10_PRO_XL: DeviceConfig =
DeviceConfig(
screenHeight = 2992,
screenWidth = 1344,
xdpi = 486,
ydpi = 486,
orientation = ScreenOrientation.PORTRAIT,
uiMode = UiMode.NORMAL,
nightMode = NightMode.NOTNIGHT,
density = Density.create(560),
@Sottti
Sottti / ListItemAllRoundedSurfacePreview.kt
Created February 3, 2025 09:57
One of the trickiest cases to write previews
@Composable
@Preview(
group = "Light Theme - All Rounded Surface",
uiMode = Configuration.UI_MODE_NIGHT_NO,
)
@Preview(
group = "Dark Theme - All Rounded Surface",
uiMode = Configuration.UI_MODE_NIGHT_YES,
)
internal fun ListItemAllRoundedSurfacePreview(
@Sottti
Sottti / ListItemPreview.kt
Created February 3, 2025 09:55
One of the trickiest cases to write previews
internal class ListItemAllRoundedSurfaceContentProvider :
PreviewParameterProvider<ListItemPreviewViewState> {
override val values: Sequence<ListItemPreviewViewState> =
listItemContentProviderValues(AllRounded)
}
internal class ListItemTopRoundedSurfaceContentProvider :
PreviewParameterProvider<ListItemPreviewViewState> {
override val values: Sequence<ListItemPreviewViewState> =
listItemContentProviderValues(TopRounded)