diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dffd0b9740..3b41260eb6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ * `Coroutines`: * `Compose`: - * Add extension `StateFlow#toMutableListState` + * Add extension `StateFlow#asMutableComposeListState` and `StateFlow#asComposeList` + * Add extension `StateFlow#asMutableComposeState`/`StateFlow#asComposeState` ## 0.11.3 diff --git a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToListState.kt b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsListState.kt similarity index 71% rename from coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToListState.kt rename to coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsListState.kt index 1635b6b0ea4..6004f9d918f 100644 --- a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToListState.kt +++ b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsListState.kt @@ -5,11 +5,10 @@ import androidx.compose.runtime.snapshots.SnapshotStateList import dev.inmo.micro_utils.common.applyDiff import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.StateFlow @Suppress("NOTHING_TO_INLINE") -inline fun StateFlow>.toMutableListState( +inline fun StateFlow>.asMutableComposeListState( scope: CoroutineScope ): SnapshotStateList { val state = mutableStateListOf(*value.toTypedArray()) @@ -19,3 +18,8 @@ inline fun StateFlow>.toMutableListState( return state } +@Suppress("NOTHING_TO_INLINE") +inline fun StateFlow>.asComposeList( + scope: CoroutineScope +): List = asMutableComposeListState(scope) + diff --git a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsState.kt b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsState.kt new file mode 100644 index 00000000000..334c5dcb6ea --- /dev/null +++ b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsState.kt @@ -0,0 +1,35 @@ +package dev.inmo.micro_utils.coroutines.compose + +import androidx.compose.runtime.* +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.StateFlow + +fun Flow.asMutableComposeState( + initial: T, + scope: CoroutineScope +): MutableState { + val state = mutableStateOf(initial) + subscribeSafelyWithoutExceptions(scope) { state.value = it } + return state +} + +@Suppress("NOTHING_TO_INLINE") +inline fun StateFlow.asMutableComposeState( + scope: CoroutineScope +): MutableState = asMutableComposeState(value, scope) + +fun Flow.asComposeState( + initial: T, + scope: CoroutineScope +): State { + val state = asMutableComposeState(initial, scope) + return derivedStateOf { state.value } +} + +@Suppress("NOTHING_TO_INLINE") +inline fun StateFlow.asComposeState( + scope: CoroutineScope +): State = asComposeState(value, scope) + diff --git a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToState.kt b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToMutableState.kt similarity index 100% rename from coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToState.kt rename to coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToMutableState.kt