improve compose list state creation

This commit is contained in:
InsanusMokrassar 2022-06-22 16:56:51 +06:00
parent c655107681
commit 9cf01ab54f

View File

@ -5,13 +5,14 @@ import androidx.compose.runtime.snapshots.SnapshotStateList
import dev.inmo.micro_utils.common.applyDiff import dev.inmo.micro_utils.common.applyDiff
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun <reified T> StateFlow<List<T>>.asMutableComposeListState( inline fun <reified T> Flow<List<T>>.asMutableComposeListState(
scope: CoroutineScope scope: CoroutineScope
): SnapshotStateList<T> { ): SnapshotStateList<T> {
val state = mutableStateListOf(*value.toTypedArray()) val state = mutableStateListOf<T>()
subscribeSafelyWithoutExceptions(scope) { subscribeSafelyWithoutExceptions(scope) {
state.applyDiff(it) state.applyDiff(it)
} }
@ -19,7 +20,7 @@ inline fun <reified T> StateFlow<List<T>>.asMutableComposeListState(
} }
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun <reified T> StateFlow<List<T>>.asComposeList( inline fun <reified T> Flow<List<T>>.asComposeList(
scope: CoroutineScope scope: CoroutineScope
): List<T> = asMutableComposeListState(scope) ): List<T> = asMutableComposeListState(scope)