Skip to content

Commit e45ecd6

Browse files
committed
Add Modifier.thenIfNotNull, OnChange
1 parent 7baab72 commit e45ecd6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

composed/src/main/kotlin/com/w2sv/composed/FlowCollection.kt renamed to composed/src/main/kotlin/com/w2sv/composed/LaunchedEffect.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.w2sv.composed
33
import android.annotation.SuppressLint
44
import androidx.compose.runtime.Composable
55
import androidx.compose.runtime.LaunchedEffect
6+
import androidx.compose.runtime.getValue
7+
import androidx.compose.runtime.rememberUpdatedState
68
import kotlinx.coroutines.flow.Flow
79
import kotlinx.coroutines.flow.FlowCollector
810
import kotlinx.coroutines.flow.collectLatest
@@ -37,4 +39,18 @@ fun <T> CollectLatestFromFlow(
3739
LaunchedEffect(flow, key1, key2) {
3840
flow.collectLatest(action)
3941
}
42+
}
43+
44+
@Composable
45+
fun <T> OnChange(
46+
value: T,
47+
key1: Any? = null,
48+
key2: Any? = null,
49+
callback: suspend (T) -> Unit
50+
) {
51+
val updatedCallback by rememberUpdatedState(newValue = callback)
52+
53+
LaunchedEffect(value, key1, key2) {
54+
updatedCallback(value)
55+
}
4056
}

composed/src/main/kotlin/com/w2sv/composed/extensions/Modifier.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ inline fun Modifier.thenIf(
2525
condition: Boolean,
2626
onTrue: Modifier.() -> Modifier,
2727
): Modifier =
28-
thenIf(condition = condition, onFalse = { this }, onTrue = onTrue)
28+
thenIf(condition = condition, onFalse = { this }, onTrue = onTrue)
29+
30+
/**
31+
* A convenience function that invokes the Modifier receiver function [onNotNull] which depends on an optional [instance], if that [instance] is not null.
32+
*/
33+
inline fun <T> Modifier.thenIfNotNull(
34+
instance: T?,
35+
onNotNull: Modifier.(T) -> Modifier,
36+
): Modifier =
37+
instance?.let { onNotNull(it) } ?: this

0 commit comments

Comments
 (0)