Fix Location Permission Prompt on "Uploaded via Mobile" Tab (#6425)
Some checks are pending
Android CI / Run tests and generate APK (push) Waiting to run

* Added location prompt at Map screen

* Update menu visibility logic to reflect Map tab selection

* Fix location prompt by deferring permission request to Map tab visibility

* Fix: Restrict location permission prompt to Map tab in Explore section
This commit is contained in:
VoidRaven 2025-10-02 06:14:37 +05:30 committed by GitHub
parent d632c268ae
commit 4e51977fb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 78 additions and 23 deletions

View file

@ -64,6 +64,9 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageSelected(position: Int) {
binding!!.viewPager.canScroll = position != 2
if (position == 2) {
mapRootFragment?.requestLocationIfNeeded()
}
}
})
setTabs()
@ -171,14 +174,12 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
// if on Map tab, show all menu options, else only show search
binding!!.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageSelected(position: Int) {
others.setVisible((position == 2))
}
override fun onPageScrollStateChanged(state: Int) {
if (state == ViewPager.SCROLL_STATE_IDLE && binding!!.viewPager.currentItem == 2) {
onPageSelected(2)
binding!!.viewPager.canScroll = position != 2
others.setVisible(position == 2)
if (position == 2) {
mapRootFragment?.requestLocationIfNeeded()
}
}
})
@ -194,7 +195,6 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
*/
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle item selection
when (item.itemId) {
R.id.action_search -> {
startActivityWithFlags(requireActivity(), SearchActivity::class.java)
@ -224,6 +224,4 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
retainInstance = true
}
}
}
}

View file

@ -193,9 +193,20 @@ class ExploreMapRootFragment : CommonsDaggerSupportFragment, MediaDetailProvider
binding = null
}
fun requestLocationIfNeeded() {
mapFragment?.requestLocationIfNeeded()
}
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
if (isVisibleToUser) {
requestLocationIfNeeded()
}
}
companion object {
fun newInstance(): ExploreMapRootFragment = ExploreMapRootFragment().apply {
retainInstance = true
}
}
}
}

View file

@ -269,23 +269,18 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
override fun onZoom(event: ZoomEvent?): Boolean = false
})
if (!locationPermissionsHelper!!.checkLocationPermission(requireActivity())) {
askForLocationPermission()
}
// removed tha permission check here to prevent it from running on fragment creation
}
override fun onResume() {
super.onResume()
binding!!.mapView.onResume()
presenter!!.attachView(this)
registerNetworkReceiver()
if (isResumed) {
if (locationPermissionsHelper!!.checkLocationPermission(requireActivity())) {
performMapReadyActions()
} else {
startMapWithoutPermission()
}
locationManager.addLocationListener(this)
if (broadcastReceiver != null) {
requireActivity().registerReceiver(broadcastReceiver, intentFilter)
}
setSearchThisAreaButtonVisibility(false)
}
override fun onPause() {
@ -294,6 +289,38 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
unregisterNetworkReceiver()
}
fun requestLocationIfNeeded() {
if (!isVisible) return // skips if not visible to user
if (locationPermissionsHelper!!.checkLocationPermission(requireActivity())) {
if (locationPermissionsHelper!!.isLocationAccessToAppsTurnedOn()) {
locationManager.registerLocationManager()
drawMyLocationMarker()
} else {
locationPermissionsHelper!!.showLocationOffDialog(requireActivity(), R.string.location_off_dialog_text)
}
} else {
locationPermissionsHelper!!.requestForLocationAccess(
R.string.location_permission_title,
R.string.location_permission_rationale
)
}
}
private fun drawMyLocationMarker() {
val location = locationManager.getLastLocation()
if (location != null) {
val geoPoint = GeoPoint(location.latitude, location.longitude)
val startMarker = Marker(binding!!.mapView).apply {
setPosition(geoPoint)
setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM)
icon = ContextCompat.getDrawable(requireContext(), R.drawable.current_location_marker)
title = "Your Location"
textLabelFontSize = 24
}
binding!!.mapView.overlays.add(startMarker)
binding!!.mapView.invalidate()
}
}
/**
* Unregisters the networkReceiver
@ -1079,7 +1106,24 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
override fun onLocationPermissionDenied(toastMessage: String) = Unit
override fun onLocationPermissionGranted() = Unit
override fun onLocationPermissionGranted() {
if (locationPermissionsHelper!!.isLocationAccessToAppsTurnedOn()) {
locationManager.registerLocationManager()
drawMyLocationMarker()
} else {
locationPermissionsHelper!!.showLocationOffDialog(requireActivity(), R.string.location_off_dialog_text)
}
onLocationChanged(LocationChangeType.PERMISSION_JUST_GRANTED, null)
}
fun onLocationChanged(locationChangeType: LocationChangeType, location: Location?) {
if (locationChangeType == LocationChangeType.PERMISSION_JUST_GRANTED) {
val curLatLng = locationManager.getLastLocation() ?: getMapCenter()
populatePlaces(curLatLng)
} else {
presenter!!.updateMap(locationChangeType)
}
}
companion object {
fun newInstance(): ExploreMapFragment {

View file

@ -748,6 +748,8 @@ Upload your first media by tapping on the add button.</string>
<string name="wlm_campaign_description">Wiki Loves Monuments is an international photo contest for monuments organised by Wikimedia</string>
<string name="need_permission">Need Permission</string>
<string name="read_phone_state_permission_message">Nearby maps need to read PHONE STATE to function properly</string>
<string name="location_off_dialog_text">Please turn on location services to view nearby places.</string>
<string name="location_permission_rationale">Location access is needed to show nearby places on the map.</string>
<string name="contributions_of_user">Contributions of User: %s</string>
<string name="achievements_of_user">Achievements of User: %s</string>