diff --git a/CHANGELOG.md b/CHANGELOG.md index 8efa8d77640..a0166baf150 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.11.4 +* Add extension `StateFlow#toMutableListState` + ## 0.11.3 * `Ktor`: 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/FlowToListState.kt new file mode 100644 index 00000000000..1635b6b0ea4 --- /dev/null +++ b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToListState.kt @@ -0,0 +1,21 @@ +package dev.inmo.micro_utils.coroutines.compose + +import androidx.compose.runtime.* +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( + scope: CoroutineScope +): SnapshotStateList { + val state = mutableStateListOf(*value.toTypedArray()) + subscribeSafelyWithoutExceptions(scope) { + state.applyDiff(it) + } + return state +} +