Skip to content

Commit c8d08f3

Browse files
committed
Fix: Map auto-recenters on fresh install and after enabling location services
- Fixed requestLocationIfNeeded() to use askForLocationPermission() which properly triggers activityResultLauncher callback (was using ActivityCompat.requestPermissions without handling the result) - Fixed locationPermissionGranted() to call populatePlaces() after centering map to load markers at user location - Added explicit location state check in onResume() to detect when location services are enabled while app is in Settings - Added drawMyLocationMarker() call to show blue dot immediately after permission grant Fixes commons-app#6599
1 parent 391f54b commit c8d08f3

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.kt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,31 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
291291
}
292292
setSearchThisAreaButtonVisibility(false)
293293

294+
// Check if location services were enabled while app was in Settings
295+
val hasPermission = locationPermissionsHelper?.checkLocationPermission(requireActivity()) == true
296+
val isLocationNowEnabled = locationPermissionsHelper?.isLocationAccessToAppsTurnedOn() == true
297+
298+
if (hasPermission && isLocationNowEnabled && !locationServicesEnabledOnPause) {
299+
Timber.d("Location services enabled while app was paused - refreshing map")
300+
locationManager.registerLocationManager()
301+
drawMyLocationMarker()
302+
303+
val cachedLocation = locationManager.getLastLocation()
304+
if (cachedLocation != null) {
305+
val targetP = GeoPoint(cachedLocation.latitude, cachedLocation.longitude)
306+
mapCenter = targetP
307+
binding?.mapView?.controller?.setCenter(targetP)
308+
recenterMarkerToPosition(targetP)
309+
moveCameraToPosition(targetP)
310+
populatePlaces(cachedLocation)
311+
} else {
312+
isWaitingForFirstLocation = true
313+
setProgressBarVisibility(true)
314+
}
315+
}
316+
// Update tracked state for next pause/resume cycle
317+
locationServicesEnabledOnPause = isLocationNowEnabled
318+
294319
if (shouldPerformMapReadyActionsOnResume) {
295320
shouldPerformMapReadyActionsOnResume = false
296321
performMapReadyActions()
@@ -324,10 +349,8 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
324349
locationPermissionsHelper!!.showLocationOffDialog(requireActivity(), R.string.location_off_dialog_text)
325350
}
326351
} else {
327-
locationPermissionsHelper!!.requestForLocationAccess(
328-
R.string.location_permission_title,
329-
R.string.location_permission_rationale
330-
)
352+
// Use activityResultLauncher for proper callback handling
353+
askForLocationPermission()
331354
}
332355
}
333356

@@ -606,6 +629,7 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
606629
// Add listener and register location manager
607630
locationManager.addLocationListener(this)
608631
locationManager.registerLocationManager()
632+
drawMyLocationMarker()
609633

610634
lastKnownLocation = locationManager.getLastLocation()
611635

@@ -615,13 +639,13 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
615639
binding!!.mapView.controller.setCenter(targetP)
616640
recenterMarkerToPosition(targetP)
617641
moveCameraToPosition(targetP)
618-
presenter!!.onMapReady(exploreMapController)
642+
populatePlaces(lastKnownLocation)
619643
} else {
620644
// No cached location - set flag to wait for first GPS fix
621645
isWaitingForFirstLocation = true
622646
setProgressBarVisibility(true)
623-
// Load map but don't center yet - will center when location arrives
624-
presenter!!.onMapReady(exploreMapController)
647+
// Still need to populate with default location, will recenter when location arrives
648+
populatePlaces(getMapCenter())
625649
}
626650
}
627651

0 commit comments

Comments
 (0)