From 86e74c0a6fc3abbbfb826f006e481d9be2d0195d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 22 Jun 2022 16:09:52 +0600 Subject: [PATCH] add extension toMutableListState --- CHANGELOG.md | 2 ++ .../coroutines/compose/FlowToListState.kt | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowToListState.kt 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 +} +