|
|
|
@ -6,22 +6,18 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
|
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
|
|
|
import kotlinx.coroutines.SupervisorJob
|
|
|
|
|
import kotlinx.coroutines.channels.BufferOverflow
|
|
|
|
|
import kotlinx.coroutines.channels.Channel
|
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.SharingStarted
|
|
|
|
|
import kotlinx.coroutines.flow.buffer
|
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
|
import kotlinx.coroutines.flow.consumeAsFlow
|
|
|
|
|
import kotlinx.coroutines.flow.flatMapLatest
|
|
|
|
|
import kotlinx.coroutines.flow.flowOf
|
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
|
import kotlinx.coroutines.flow.mapLatest
|
|
|
|
|
import kotlinx.coroutines.flow.merge
|
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
|
import kotlinx.coroutines.flow.stateIn
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import kotlinx.coroutines.plus
|
|
|
|
|
import org.session.libsignal.utilities.SettableFuture
|
|
|
|
@ -35,29 +31,11 @@ import javax.inject.Inject
|
|
|
|
|
class GlobalSearchViewModel @Inject constructor(
|
|
|
|
|
private val searchRepository: SearchRepository,
|
|
|
|
|
) : ViewModel() {
|
|
|
|
|
|
|
|
|
|
private val executor = viewModelScope + SupervisorJob()
|
|
|
|
|
|
|
|
|
|
private val _result: MutableStateFlow<GlobalSearchResult> = MutableStateFlow(GlobalSearchResult.EMPTY)
|
|
|
|
|
|
|
|
|
|
val result: StateFlow<GlobalSearchResult> = _result
|
|
|
|
|
|
|
|
|
|
private val scope = viewModelScope + SupervisorJob()
|
|
|
|
|
private val refreshes = MutableSharedFlow<Unit>()
|
|
|
|
|
private val _queryText = MutableStateFlow<CharSequence>("")
|
|
|
|
|
|
|
|
|
|
private val _queryText: MutableStateFlow<CharSequence> = MutableStateFlow("")
|
|
|
|
|
|
|
|
|
|
fun setQuery(charSequence: CharSequence) {
|
|
|
|
|
_queryText.value = charSequence
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun refresh() {
|
|
|
|
|
viewModelScope.launch {
|
|
|
|
|
refreshes.emit(Unit)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
|
_queryText
|
|
|
|
|
val result = _queryText
|
|
|
|
|
.reEmit(refreshes)
|
|
|
|
|
.buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST)
|
|
|
|
|
.mapLatest { query ->
|
|
|
|
@ -78,10 +56,16 @@ class GlobalSearchViewModel @Inject constructor(
|
|
|
|
|
GlobalSearchResult(query.toString())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}.onEach { result ->
|
|
|
|
|
// update the latest _result value
|
|
|
|
|
_result.value = result
|
|
|
|
|
}.launchIn(executor)
|
|
|
|
|
}.stateIn(scope, SharingStarted.Lazily, GlobalSearchResult.EMPTY)
|
|
|
|
|
|
|
|
|
|
fun setQuery(charSequence: CharSequence) {
|
|
|
|
|
_queryText.value = charSequence
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun refresh() {
|
|
|
|
|
viewModelScope.launch {
|
|
|
|
|
refreshes.emit(Unit)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|