mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-11 20:27:47 +00:00
Cleanup GlobalSearchViewModel
This commit is contained in:
@@ -8,7 +8,6 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.Telephony.Mms.Addr
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.core.os.bundleOf
|
import androidx.core.os.bundleOf
|
||||||
|
@@ -6,22 +6,18 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
|||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.channels.BufferOverflow
|
import kotlinx.coroutines.channels.BufferOverflow
|
||||||
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.MutableSharedFlow
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.buffer
|
import kotlinx.coroutines.flow.buffer
|
||||||
import kotlinx.coroutines.flow.combine
|
|
||||||
import kotlinx.coroutines.flow.consumeAsFlow
|
|
||||||
import kotlinx.coroutines.flow.flatMapLatest
|
import kotlinx.coroutines.flow.flatMapLatest
|
||||||
import kotlinx.coroutines.flow.flowOf
|
import kotlinx.coroutines.flow.flowOf
|
||||||
import kotlinx.coroutines.flow.launchIn
|
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.mapLatest
|
import kotlinx.coroutines.flow.mapLatest
|
||||||
import kotlinx.coroutines.flow.merge
|
import kotlinx.coroutines.flow.merge
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.plus
|
import kotlinx.coroutines.plus
|
||||||
import org.session.libsignal.utilities.SettableFuture
|
import org.session.libsignal.utilities.SettableFuture
|
||||||
@@ -35,29 +31,11 @@ import javax.inject.Inject
|
|||||||
class GlobalSearchViewModel @Inject constructor(
|
class GlobalSearchViewModel @Inject constructor(
|
||||||
private val searchRepository: SearchRepository,
|
private val searchRepository: SearchRepository,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
private val scope = viewModelScope + SupervisorJob()
|
||||||
private val executor = viewModelScope + SupervisorJob()
|
|
||||||
|
|
||||||
private val _result: MutableStateFlow<GlobalSearchResult> = MutableStateFlow(GlobalSearchResult.EMPTY)
|
|
||||||
|
|
||||||
val result: StateFlow<GlobalSearchResult> = _result
|
|
||||||
|
|
||||||
private val refreshes = MutableSharedFlow<Unit>()
|
private val refreshes = MutableSharedFlow<Unit>()
|
||||||
|
private val _queryText = MutableStateFlow<CharSequence>("")
|
||||||
|
|
||||||
private val _queryText: MutableStateFlow<CharSequence> = MutableStateFlow("")
|
val result = _queryText
|
||||||
|
|
||||||
fun setQuery(charSequence: CharSequence) {
|
|
||||||
_queryText.value = charSequence
|
|
||||||
}
|
|
||||||
|
|
||||||
fun refresh() {
|
|
||||||
viewModelScope.launch {
|
|
||||||
refreshes.emit(Unit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
init {
|
|
||||||
_queryText
|
|
||||||
.reEmit(refreshes)
|
.reEmit(refreshes)
|
||||||
.buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST)
|
.buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST)
|
||||||
.mapLatest { query ->
|
.mapLatest { query ->
|
||||||
@@ -78,10 +56,16 @@ class GlobalSearchViewModel @Inject constructor(
|
|||||||
GlobalSearchResult(query.toString())
|
GlobalSearchResult(query.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.onEach { result ->
|
}.stateIn(scope, SharingStarted.Lazily, GlobalSearchResult.EMPTY)
|
||||||
// update the latest _result value
|
|
||||||
_result.value = result
|
fun setQuery(charSequence: CharSequence) {
|
||||||
}.launchIn(executor)
|
_queryText.value = charSequence
|
||||||
|
}
|
||||||
|
|
||||||
|
fun refresh() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
refreshes.emit(Unit)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user