Fix excessive querying

pull/2672/head
Alex Baker 2 years ago
parent ef27a50e42
commit c8d81b44b6

@ -15,10 +15,11 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers 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.filter import kotlinx.coroutines.flow.distinctUntilChanged
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
@ -109,15 +110,20 @@ class TaskListViewModel @Inject constructor(
localBroadcastManager.registerRefreshReceiver(refreshReceiver) localBroadcastManager.registerRefreshReceiver(refreshReceiver)
_state _state
.filter { it.filter != null } .mapNotNull { state -> state.filter?.let { it to state.searchQuery } }
.distinctUntilChanged()
.throttleLatest(333) .throttleLatest(333)
.map { .map { (filter, searchQuery) ->
val filter = when { taskDao.fetchTasks {
it.searchQuery == null -> it.filter!! getQuery(
it.searchQuery.isBlank() -> BuiltInFilterExposer.getMyTasksFilter(context.resources) preferences = preferences,
else -> context.createSearchQuery(it.searchQuery) filter = when {
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