Locations: reduce polls (#1560)

* .take(3).timeout(1.minutes)

* debounce location search by 250ms to reduce location queries

* reduce timeout to 30 seconds

* keep code and comments in sync ...

* Move empty query condition check back to LocationsRepository

---------

Co-authored-by: MM20 <15646950+MM2-0@users.noreply.github.com>
This commit is contained in:
shtrophic
2025-08-31 20:43:29 +02:00
committed by GitHub
parent 2668cf8afc
commit 55365fbc59
2 changed files with 18 additions and 9 deletions

View File

@@ -18,9 +18,12 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combineTransform
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.timeout
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import kotlin.time.Duration.Companion.seconds
internal class LocationsRepository(
private val context: Context,
@@ -34,19 +37,24 @@ internal class LocationsRepository(
query: String,
allowNetwork: Boolean
): Flow<ImmutableList<Location>> {
if (query.isBlank()) {
if (query.isBlank() && query.length > 1) {
return flowOf(persistentListOf())
}
val hasPermission = permissionsManager.hasPermission(PermissionGroup.Location)
return combineTransform(
poseProvider.getLocation(minDistanceM = 50.0f),
settings.data,
hasPermission
) { userLocation, settingsData, permission ->
poseProvider
.getLocation(minTimeMs = 2000, minDistanceM = 50.0f)
// 1st location: lastCachedLocation of poseProvider, if available
// 2nd location: LocationManager.getLastKnownLocation(), if available and better than lastCachedLocation
// 3rd location: live location from LocationManager.requestLocationUpdates() that is better than any of the previous
.take(3)
// only request locations for 30 seconds
.timeout(30.seconds),
permissionsManager.hasPermission(PermissionGroup.Location),
settings.data
) { userLocation, hasPermission, settingsData ->
emit(persistentListOf())
if (!permission || settingsData.providers.isEmpty()) {
if (!hasPermission || settingsData.providers.isEmpty()) {
return@combineTransform
}

View File

@@ -235,6 +235,7 @@ internal class SearchServiceImpl(
}
if (filters.places) {
launch {
delay(250)
locationRepository.search(query, filters.allowNetwork)
.combine(customAttrResults) { locations, customAttrs ->
if (customAttrs.locations != null) locations + customAttrs.locations