Skip to content

Commit

Permalink
Merge pull request #472 from thgoebel/keep-state
Browse files Browse the repository at this point in the history
Restore zoom + camera position state when returning to map
  • Loading branch information
mjaakko authored Feb 1, 2025
2 parents 7d66eb0 + 21a165f commit adf75e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
12 changes: 11 additions & 1 deletion app/src/main/java/xyz/malkki/neostumbler/common/LatLng.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ package xyz.malkki.neostumbler.common
data class LatLng(
val latitude: Double,
val longitude: Double
)
) {
companion object {
val ORIGIN = LatLng(0.0, 0.0)
}

fun isOrigin(): Boolean = this == ORIGIN

fun asMapLibreLatLng(): org.maplibre.android.geometry.LatLng {
return org.maplibre.android.geometry.LatLng(latitude, longitude)
}
}
21 changes: 9 additions & 12 deletions app/src/main/java/xyz/malkki/neostumbler/ui/screens/MapScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,16 @@ fun MapScreen(mapViewModel: MapViewModel = viewModel()) {
mapView.lifecycle = lifecycle

mapView.getMapAsync { map ->
if (map.cameraPosition.target == null || (map.cameraPosition.target?.latitude == 0.0 && map.cameraPosition.target?.longitude == 0.0)) {
if (latestReportPosition.value != null) {
map.cameraPosition = CameraPosition.Builder()
.target(latestReportPosition.value?.let { LatLng(it.latitude, it.longitude) })
.zoom(10.0)
.build()
} else {
map.cameraPosition = CameraPosition.Builder()
.target(LatLng(mapViewModel.mapCenter.value.latitude, mapViewModel.mapCenter.value.longitude))
.zoom(mapViewModel.zoom.value)
.build()
}
// Call repeatedly in update() because latestReportPosition may not be available in factory()
val target = if (mapViewModel.mapCenter.value.isOrigin()) {
latestReportPosition.value
} else {
mapViewModel.mapCenter.value
}
map.cameraPosition = CameraPosition.Builder()
.target(target?.asMapLibreLatLng())
.zoom(mapViewModel.zoom.value)
.build()

if (myLocation.value != null && map.locationComponent.isLocationComponentActivated) {
map.locationComponent.forceLocationUpdate(myLocation.value!!.location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class MapViewModel(application: Application) : AndroidViewModel(application) {

private val showMyLocation = MutableStateFlow(getApplication<StumblerApplication>().checkMissingPermissions(Manifest.permission.ACCESS_COARSE_LOCATION).isEmpty())

private val _mapCenter = MutableStateFlow<LatLng>(LatLng(0.0, 0.0))
private val _mapCenter = MutableStateFlow<LatLng>(LatLng.ORIGIN)
val mapCenter: StateFlow<LatLng>
get() = _mapCenter.asStateFlow()

private val _zoom = MutableStateFlow(5.0)
private val _zoom = MutableStateFlow(12.0)
val zoom: StateFlow<Double>
get() = _zoom.asStateFlow()

Expand Down

0 comments on commit adf75e1

Please sign in to comment.