diff --git a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsListState.kt b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsListState.kt index bed242faa49..0f0f5309de6 100644 --- a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsListState.kt +++ b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsListState.kt @@ -13,6 +13,14 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.withContext import kotlin.coroutines.CoroutineContext +/** + * Each value of [this] [Flow] will trigger [applyDiff] to the result [SnapshotStateList] + * + * @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [SnapshotStateList] + * @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that + * change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default + * @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler + */ @Suppress("NOTHING_TO_INLINE") inline fun Flow>.asMutableComposeListState( scope: CoroutineScope, @@ -33,6 +41,16 @@ inline fun Flow>.asMutableComposeListState( return state } +/** + * In fact, it is just classcast of [asMutableComposeListState] to [List] + * + * @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [List] + * @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that + * change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default + * @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler + * + * @return Changing in time [List] which follow [Flow] values + */ @Suppress("NOTHING_TO_INLINE") inline fun Flow>.asComposeList( scope: CoroutineScope, diff --git a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsState.kt b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsState.kt index 9bbee0e172c..3bf1fecc7a7 100644 --- a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsState.kt +++ b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowAsState.kt @@ -1,6 +1,7 @@ package dev.inmo.micro_utils.coroutines.compose import androidx.compose.runtime.* +import dev.inmo.micro_utils.common.compose.asState import dev.inmo.micro_utils.coroutines.ExceptionHandler import dev.inmo.micro_utils.coroutines.defaultSafelyWithoutExceptionHandlerWithNull import dev.inmo.micro_utils.coroutines.doInUI @@ -12,6 +13,15 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.withContext import kotlin.coroutines.CoroutineContext +/** + * Will map [this] [Flow] as [MutableState]. Returned [MutableState] WILL NOT change source [Flow] + * + * @param initial First value which will be passed to the result [MutableState] + * @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [MutableState] + * @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that + * change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default + * @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler + */ fun Flow.asMutableComposeState( initial: T, scope: CoroutineScope, @@ -32,6 +42,15 @@ fun Flow.asMutableComposeState( return state } +/** + * Will map [this] [StateFlow] as [MutableState]. Returned [MutableState] WILL NOT change source [StateFlow]. + * This conversation will pass its [StateFlow.value] as the first value + * + * @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [MutableState] + * @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that + * change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default + * @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler + */ @Suppress("NOTHING_TO_INLINE") inline fun StateFlow.asMutableComposeState( scope: CoroutineScope, @@ -39,6 +58,15 @@ inline fun StateFlow.asMutableComposeState( noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, ): MutableState = asMutableComposeState(value, scope, useContextOnChange, onException) +/** + * Will create [MutableState] using [asMutableComposeState] and use [asState] to convert it as immutable state + * + * @param initial First value which will be passed to the result [State] + * @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [State] + * @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that + * change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default + * @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler + */ fun Flow.asComposeState( initial: T, scope: CoroutineScope, @@ -46,9 +74,17 @@ fun Flow.asComposeState( onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, ): State { val state = asMutableComposeState(initial, scope, useContextOnChange, onException) - return derivedStateOf { state.value } + return state.asState() } +/** + * Will map [this] [StateFlow] as [State]. This conversation will pass its [StateFlow.value] as the first value + * + * @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [State] + * @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that + * change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default + * @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler + */ @Suppress("NOTHING_TO_INLINE") inline fun StateFlow.asComposeState( scope: CoroutineScope,