DevicePoseProvider: improve getLocation() (#1454)

* Only take updated location when it is of better quality

* Rename to AndroidLocation for clarification

* be pedantic about licenses

* fix race condition in DevicePoseProvider.kt

* add possibility to explicitly skip cache upon requesting location updates for WeatherRepository.kt

* LocationsRepository.kt: use getLocation() in combineTransform() to properly wait for & update on new locations

* LocationItem.kt: fix getAzimuthDegrees() usage for userHeading
This commit is contained in:
shtrophic
2025-07-04 11:55:20 +02:00
committed by GitHub
parent dd14c6cbaa
commit 31a1580db9
7 changed files with 119 additions and 54 deletions
@@ -11,21 +11,15 @@ import de.mm20.launcher2.search.Location
import de.mm20.launcher2.search.SearchableRepository
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combineTransform
import kotlinx.coroutines.flow.coroutineContext
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.newCoroutineContext
import kotlinx.coroutines.supervisorScope
internal class LocationsRepository(
@@ -35,6 +29,7 @@ internal class LocationsRepository(
private val permissionsManager: PermissionsManager,
) : SearchableRepository<Location> {
@OptIn(FlowPreview::class)
override fun search(
query: String,
allowNetwork: Boolean
@@ -45,16 +40,16 @@ internal class LocationsRepository(
val hasPermission = permissionsManager.hasPermission(PermissionGroup.Location)
return combineTransform(settings.data, hasPermission) { settingsData, permission ->
return combineTransform(
poseProvider.getLocation(minDistanceM = 50.0f),
settings.data,
hasPermission
) { userLocation, settingsData, permission ->
emit(persistentListOf())
if (!permission || settingsData.providers.isEmpty()) {
return@combineTransform
}
val userLocation = poseProvider.getLocation().firstOrNull()
?: poseProvider.lastLocation
?: return@combineTransform
val providers = settingsData.providers.map {
when (it) {
"openstreetmaps" -> OsmLocationProvider(context, settings)