Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
private var prevLongitude = 0.0
private var recentlyCameFromNearbyMap = false
private var shouldPerformMapReadyActionsOnResume = false
private var hadLocationPermissionOnPause = false
private var isWaitingForFirstLocation = false
private var presenter: ExploreMapPresenter? = null
private var binding: FragmentExploreMapBinding? = null
var mediaList: MutableList<Media>? = null
Expand Down Expand Up @@ -282,14 +284,32 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
requireActivity().registerReceiver(broadcastReceiver, intentFilter)
}
setSearchThisAreaButtonVisibility(false)

// Check if we need to wait for first location
val hasPermission = locationPermissionsHelper?.checkLocationPermission(requireActivity()) == true
val cachedLocation = locationManager.getLastLocation()

if (hasPermission && cachedLocation == null && !isWaitingForFirstLocation) {
isWaitingForFirstLocation = true
setProgressBarVisibility(true)
}

if (shouldPerformMapReadyActionsOnResume) {
shouldPerformMapReadyActionsOnResume = false
performMapReadyActions()
}

// Only refresh if permission state changed from false to true
if (hasPermission && !hadLocationPermissionOnPause) {
performMapReadyActions()
}
}

override fun onPause() {
super.onPause()
// Track permission state before pausing
hadLocationPermissionOnPause = locationPermissionsHelper?.checkLocationPermission(requireActivity()) == true

// unregistering the broadcastReceiver, as it was causing an exception and a potential crash
unregisterNetworkReceiver()
locationManager.unregisterLocationManager()
Expand Down Expand Up @@ -361,24 +381,36 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
isPermissionDenied = true
}

lastKnownLocation = getLastLocation()

if (lastKnownLocation == null) {
lastKnownLocation = defaultLatLng
}
// Use locationManager.getLastLocation() directly to get fresh location
// Don't use getLastLocation() as it may return cached defaultLatLng
lastKnownLocation = locationManager.getLastLocation()

// if we came from 'Show in Explore' in Nearby, load Nearby map center and zoom
if (isCameFromNearbyMap) {
val targetP = GeoPoint(prevLatitude, prevLongitude)
mapCenter = targetP
moveCameraToPosition(
GeoPoint(prevLatitude, prevLongitude),
targetP,
prevZoom.coerceIn(1.0, 22.0),
1L
)
} else {
moveCameraToPosition(
GeoPoint(lastKnownLocation!!.latitude, lastKnownLocation!!.longitude)
)
recenterMarkerToPosition(targetP)
} else if (lastKnownLocation != null) {
// We have a real location - center to it and show blue dot
val targetP = GeoPoint(lastKnownLocation!!.latitude, lastKnownLocation!!.longitude)
mapCenter = targetP
moveCameraToPosition(targetP)
recenterMarkerToPosition(targetP)
} else if (!isWaitingForFirstLocation) {
// No location and not waiting - use default
// This happens when permission not granted yet
lastKnownLocation = defaultLatLng
val targetP = GeoPoint(lastKnownLocation!!.latitude, lastKnownLocation!!.longitude)
mapCenter = targetP
moveCameraToPosition(targetP)
recenterMarkerToPosition(targetP)
}
// If waiting for first location, don't center - will center when location arrives
presenter!!.onMapReady(exploreMapController)
}

Expand Down Expand Up @@ -472,18 +504,33 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
}
}

override fun onLocationChangedSignificantly(latLng: LatLng) =
override fun onLocationChangedSignificantly(latLng: LatLng) {
handleLocationUpdate(latLng, LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
}

override fun onLocationChangedSlightly(latLng: LatLng) =
override fun onLocationChangedSlightly(latLng: LatLng) {
handleLocationUpdate(latLng, LocationChangeType.LOCATION_SLIGHTLY_CHANGED)
}

private fun handleLocationUpdate(
latLng: LatLng?,
locationChangeType: LocationChangeType
) {
lastKnownLocation = latLng
exploreMapController.currentLocation = lastKnownLocation

// If we were waiting for first location after permission grant, center map now
if (isWaitingForFirstLocation && latLng != null) {
isWaitingForFirstLocation = false
setProgressBarVisibility(false)

val targetP = GeoPoint(latLng.latitude, latLng.longitude)
mapCenter = targetP
binding!!.mapView.controller.setCenter(targetP)
recenterMarkerToPosition(targetP)
moveCameraToPosition(targetP)
}

presenter!!.updateMap(locationChangeType)
}

Expand Down Expand Up @@ -563,28 +610,27 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
private fun locationPermissionGranted() {
isPermissionDenied = false
applicationKvStore.putBoolean("doNotAskForLocationPermission", false)

// Add listener and register location manager
locationManager.addLocationListener(this)
locationManager.registerLocationManager()

lastKnownLocation = locationManager.getLastLocation()
val target = lastKnownLocation

if (lastKnownLocation != null) {
val targetP = GeoPoint(target!!.latitude, target.longitude)
val targetP = GeoPoint(lastKnownLocation!!.latitude, lastKnownLocation!!.longitude)
mapCenter = targetP
binding!!.mapView.controller.setCenter(targetP)
recenterMarkerToPosition(targetP)
moveCameraToPosition(targetP)
} else if (locationManager.isGPSProviderEnabled()
|| locationManager.isNetworkProviderEnabled()
) {
locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER)
locationManager.requestLocationUpdatesFromProvider(LocationManager.GPS_PROVIDER)
setProgressBarVisibility(true)
presenter!!.onMapReady(exploreMapController)
} else {
locationPermissionsHelper!!.showLocationOffDialog(
requireActivity(),
R.string.ask_to_turn_location_on_text
)
// No cached location - set flag to wait for first GPS fix
isWaitingForFirstLocation = true
setProgressBarVisibility(true)
// Load map but don't center yet - will center when location arrives
presenter!!.onMapReady(exploreMapController)
}
presenter!!.onMapReady(exploreMapController)
registerUnregisterLocationListener(false)
}

fun registerUnregisterLocationListener(removeLocationListener: Boolean) {
Expand Down Expand Up @@ -1121,12 +1167,31 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi

override fun onLocationPermissionGranted() {
if (locationPermissionsHelper!!.isLocationAccessToAppsTurnedOn()) {
locationManager.addLocationListener(this)
locationManager.registerLocationManager()
drawMyLocationMarker()

// Check if we have a cached location
val cachedLocation = locationManager.getLastLocation()

if (cachedLocation != null) {
// Center map immediately
val targetP = GeoPoint(cachedLocation.latitude, cachedLocation.longitude)
mapCenter = targetP
binding?.mapView?.controller?.setCenter(targetP)
recenterMarkerToPosition(targetP)
moveCameraToPosition(targetP)
populatePlaces(cachedLocation)
} else {
// No cached location - wait for first GPS fix
isWaitingForFirstLocation = true
setProgressBarVisibility(true)
// Still need to populate with default, but will recenter when location arrives
populatePlaces(getMapCenter())
}
} else {
locationPermissionsHelper!!.showLocationOffDialog(requireActivity(), R.string.location_off_dialog_text)
}
onLocationChanged(LocationChangeType.PERMISSION_JUST_GRANTED, null)
}

fun onLocationChanged(locationChangeType: LocationChangeType, location: Location?) {
Expand Down