diff --git a/selector/common/src/commonMain/kotlin/dev/inmo/micro_utils/selector/SelectorItemsFlow.kt b/selector/common/src/commonMain/kotlin/dev/inmo/micro_utils/selector/SelectorItemsFlow.kt new file mode 100644 index 00000000000..29fd3917cb1 --- /dev/null +++ b/selector/common/src/commonMain/kotlin/dev/inmo/micro_utils/selector/SelectorItemsFlow.kt @@ -0,0 +1,17 @@ +package dev.inmo.micro_utils.selector + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.* + +/** + * @return Returned [SharedFlow] will emit true when [element] has been selected in [this] [Selector] and will emit + * false when this [element] was deselected + * + * @see [Selector] + * @see [Selector.itemSelected] + * @see [Selector.itemUnselected] + */ +fun Selector.itemSelectionFlow(element: T, scope: CoroutineScope): SharedFlow = MutableSharedFlow().apply { + itemSelected.onEach { if (it == element) emit(true) }.launchIn(scope) + itemUnselected.onEach { if (it == element) emit(false) }.launchIn(scope) +}.asSharedFlow()