renames in compose states creation

This commit is contained in:
InsanusMokrassar 2022-06-22 16:52:19 +06:00
parent 91a5af6a9a
commit c655107681
4 changed files with 43 additions and 3 deletions

View File

@ -4,7 +4,8 @@
* `Coroutines`: * `Coroutines`:
* `Compose`: * `Compose`:
* Add extension `StateFlow#toMutableListState` * Add extension `StateFlow#asMutableComposeListState` and `StateFlow#asComposeList`
* Add extension `StateFlow#asMutableComposeState`/`StateFlow#asComposeState`
## 0.11.3 ## 0.11.3

View File

@ -5,11 +5,10 @@ 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>>.toMutableListState( inline fun <reified T> StateFlow<List<T>>.asMutableComposeListState(
scope: CoroutineScope scope: CoroutineScope
): SnapshotStateList<T> { ): SnapshotStateList<T> {
val state = mutableStateListOf(*value.toTypedArray()) val state = mutableStateListOf(*value.toTypedArray())
@ -19,3 +18,8 @@ inline fun <reified T> StateFlow<List<T>>.toMutableListState(
return state return state
} }
@Suppress("NOTHING_TO_INLINE")
inline fun <reified T> StateFlow<List<T>>.asComposeList(
scope: CoroutineScope
): List<T> = asMutableComposeListState(scope)

View File

@ -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 <T> Flow<T>.asMutableComposeState(
initial: T,
scope: CoroutineScope
): MutableState<T> {
val state = mutableStateOf(initial)
subscribeSafelyWithoutExceptions(scope) { state.value = it }
return state
}
@Suppress("NOTHING_TO_INLINE")
inline fun <T> StateFlow<T>.asMutableComposeState(
scope: CoroutineScope
): MutableState<T> = asMutableComposeState(value, scope)
fun <T> Flow<T>.asComposeState(
initial: T,
scope: CoroutineScope
): State<T> {
val state = asMutableComposeState(initial, scope)
return derivedStateOf { state.value }
}
@Suppress("NOTHING_TO_INLINE")
inline fun <T> StateFlow<T>.asComposeState(
scope: CoroutineScope
): State<T> = asComposeState(value, scope)