Use SharedMutableFlow in GlobalSearch

This commit is contained in:
Andrew 2024-06-18 12:23:56 +09:30
parent 5d07a58085
commit 7ae95a953a

View File

@ -9,6 +9,7 @@ import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.buffer
@ -41,7 +42,7 @@ class GlobalSearchViewModel @Inject constructor(
val result: StateFlow<GlobalSearchResult> = _result val result: StateFlow<GlobalSearchResult> = _result
private val refreshes = Channel<Unit>() private val refreshes = MutableSharedFlow<Unit>()
private val _queryText: MutableStateFlow<CharSequence> = MutableStateFlow("") private val _queryText: MutableStateFlow<CharSequence> = MutableStateFlow("")
@ -50,7 +51,7 @@ class GlobalSearchViewModel @Inject constructor(
} }
fun refresh() { fun refresh() {
executor.launch { refreshes.send(Unit) } refreshes.tryEmit(Unit)
} }
init { init {
@ -86,4 +87,4 @@ class GlobalSearchViewModel @Inject constructor(
* Re-emit whenever refreshes emits. * Re-emit whenever refreshes emits.
* */ * */
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
private fun <T> Flow<T>.reEmit(refreshes: Channel<Unit>) = flatMapLatest { query -> merge(flowOf(query), refreshes.consumeAsFlow().map { query }) } private fun <T> Flow<T>.reEmit(refreshes: Flow<Unit>) = flatMapLatest { query -> merge(flowOf(query), refreshes.map { query }) }