mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-06-02 07:55:13 +00:00
26 lines
814 B
Kotlin
26 lines
814 B
Kotlin
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.StateFlow
|
|
|
|
@Suppress("NOTHING_TO_INLINE")
|
|
inline fun <reified T> StateFlow<List<T>>.asMutableComposeListState(
|
|
scope: CoroutineScope
|
|
): SnapshotStateList<T> {
|
|
val state = mutableStateListOf(*value.toTypedArray())
|
|
subscribeSafelyWithoutExceptions(scope) {
|
|
state.applyDiff(it)
|
|
}
|
|
return state
|
|
}
|
|
|
|
@Suppress("NOTHING_TO_INLINE")
|
|
inline fun <reified T> StateFlow<List<T>>.asComposeList(
|
|
scope: CoroutineScope
|
|
): List<T> = asMutableComposeListState(scope)
|
|
|