Fix excessive querying (again)

pull/2672/head
Alex Baker 6 months ago
parent c8d81b44b6
commit 52c54b1eac

@ -16,10 +16,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -110,20 +110,17 @@ class TaskListViewModel @Inject constructor(
localBroadcastManager.registerRefreshReceiver(refreshReceiver) localBroadcastManager.registerRefreshReceiver(refreshReceiver)
_state _state
.mapNotNull { state -> state.filter?.let { it to state.searchQuery } } .filter { it.filter != null }
.map { it.copy(tasks = TasksResults.Loading) }
.distinctUntilChanged() .distinctUntilChanged()
.throttleLatest(333) .throttleLatest(333)
.map { (filter, searchQuery) -> .map {
taskDao.fetchTasks { val filter = when {
getQuery( it.searchQuery == null -> it.filter!!
preferences = preferences, it.searchQuery.isBlank() -> BuiltInFilterExposer.getMyTasksFilter(context.resources)
filter = when { else -> context.createSearchQuery(it.searchQuery)
searchQuery == null -> filter
searchQuery.isBlank() -> BuiltInFilterExposer.getMyTasksFilter(context.resources)
else -> context.createSearchQuery(searchQuery)
}
)
} }
taskDao.fetchTasks { getQuery(preferences, filter) }
} }
.onEach { tasks -> .onEach { tasks ->
_state.update { _state.update {

Loading…
Cancel
Save