diff --git a/composed/src/main/kotlin/com/w2sv/composed/DisposableEffects.kt b/composed/src/main/kotlin/com/w2sv/composed/DisposableEffect.kt similarity index 100% rename from composed/src/main/kotlin/com/w2sv/composed/DisposableEffects.kt rename to composed/src/main/kotlin/com/w2sv/composed/DisposableEffect.kt diff --git a/composed/src/main/kotlin/com/w2sv/composed/FlowCollection.kt b/composed/src/main/kotlin/com/w2sv/composed/LaunchedEffect.kt similarity index 74% rename from composed/src/main/kotlin/com/w2sv/composed/FlowCollection.kt rename to composed/src/main/kotlin/com/w2sv/composed/LaunchedEffect.kt index bbfc86e..b440359 100644 --- a/composed/src/main/kotlin/com/w2sv/composed/FlowCollection.kt +++ b/composed/src/main/kotlin/com/w2sv/composed/LaunchedEffect.kt @@ -3,6 +3,8 @@ package com.w2sv.composed import android.annotation.SuppressLint import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.rememberUpdatedState import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.FlowCollector import kotlinx.coroutines.flow.collectLatest @@ -37,4 +39,18 @@ fun CollectLatestFromFlow( LaunchedEffect(flow, key1, key2) { flow.collectLatest(action) } +} + +@Composable +fun OnChange( + value: T, + key1: Any? = null, + key2: Any? = null, + callback: suspend (T) -> Unit +) { + val updatedCallback by rememberUpdatedState(newValue = callback) + + LaunchedEffect(value, key1, key2) { + updatedCallback(value) + } } \ No newline at end of file diff --git a/composed/src/main/kotlin/com/w2sv/composed/extensions/Modifier.kt b/composed/src/main/kotlin/com/w2sv/composed/extensions/Modifier.kt index 0aca75b..968a838 100644 --- a/composed/src/main/kotlin/com/w2sv/composed/extensions/Modifier.kt +++ b/composed/src/main/kotlin/com/w2sv/composed/extensions/Modifier.kt @@ -25,4 +25,13 @@ inline fun Modifier.thenIf( condition: Boolean, onTrue: Modifier.() -> Modifier, ): Modifier = - thenIf(condition = condition, onFalse = { this }, onTrue = onTrue) \ No newline at end of file + thenIf(condition = condition, onFalse = { this }, onTrue = onTrue) + +/** + * A convenience function that invokes the Modifier receiver function [onNotNull] which depends on an optional [instance], if that [instance] is not null. + */ +inline fun Modifier.thenIfNotNull( + instance: T?, + onNotNull: Modifier.(T) -> Modifier, +): Modifier = + instance?.let { onNotNull(it) } ?: this \ No newline at end of file