Skip to content

Commit 4e51977

Browse files
Fix Location Permission Prompt on "Uploaded via Mobile" Tab (#6425)
* 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
1 parent d632c26 commit 4e51977

File tree

4 files changed

+78
-23
lines changed

4 files changed

+78
-23
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
6464
override fun onPageScrollStateChanged(state: Int) = Unit
6565
override fun onPageSelected(position: Int) {
6666
binding!!.viewPager.canScroll = position != 2
67+
if (position == 2) {
68+
mapRootFragment?.requestLocationIfNeeded()
69+
}
6770
}
6871
})
6972
setTabs()
@@ -171,14 +174,12 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
171174
// if on Map tab, show all menu options, else only show search
172175
binding!!.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
173176
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
174-
177+
override fun onPageScrollStateChanged(state: Int) = Unit
175178
override fun onPageSelected(position: Int) {
176-
others.setVisible((position == 2))
177-
}
178-
179-
override fun onPageScrollStateChanged(state: Int) {
180-
if (state == ViewPager.SCROLL_STATE_IDLE && binding!!.viewPager.currentItem == 2) {
181-
onPageSelected(2)
179+
binding!!.viewPager.canScroll = position != 2
180+
others.setVisible(position == 2)
181+
if (position == 2) {
182+
mapRootFragment?.requestLocationIfNeeded()
182183
}
183184
}
184185
})
@@ -194,7 +195,6 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
194195
*/
195196
override fun onOptionsItemSelected(item: MenuItem): Boolean {
196197
// Handle item selection
197-
198198
when (item.itemId) {
199199
R.id.action_search -> {
200200
startActivityWithFlags(requireActivity(), SearchActivity::class.java)
@@ -224,6 +224,4 @@ class ExploreFragment : CommonsDaggerSupportFragment() {
224224
retainInstance = true
225225
}
226226
}
227-
}
228-
229-
227+
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,20 @@ class ExploreMapRootFragment : CommonsDaggerSupportFragment, MediaDetailProvider
193193
binding = null
194194
}
195195

196+
fun requestLocationIfNeeded() {
197+
mapFragment?.requestLocationIfNeeded()
198+
}
199+
200+
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
201+
super.setUserVisibleHint(isVisibleToUser)
202+
if (isVisibleToUser) {
203+
requestLocationIfNeeded()
204+
}
205+
}
206+
196207
companion object {
197208
fun newInstance(): ExploreMapRootFragment = ExploreMapRootFragment().apply {
198209
retainInstance = true
199210
}
200211
}
201-
}
212+
}

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

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,18 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
269269

270270
override fun onZoom(event: ZoomEvent?): Boolean = false
271271
})
272-
if (!locationPermissionsHelper!!.checkLocationPermission(requireActivity())) {
273-
askForLocationPermission()
274-
}
272+
// removed tha permission check here to prevent it from running on fragment creation
275273
}
276274

277275
override fun onResume() {
278276
super.onResume()
279277
binding!!.mapView.onResume()
280278
presenter!!.attachView(this)
281-
registerNetworkReceiver()
282-
if (isResumed) {
283-
if (locationPermissionsHelper!!.checkLocationPermission(requireActivity())) {
284-
performMapReadyActions()
285-
} else {
286-
startMapWithoutPermission()
287-
}
279+
locationManager.addLocationListener(this)
280+
if (broadcastReceiver != null) {
281+
requireActivity().registerReceiver(broadcastReceiver, intentFilter)
288282
}
283+
setSearchThisAreaButtonVisibility(false)
289284
}
290285

291286
override fun onPause() {
@@ -294,6 +289,38 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
294289
unregisterNetworkReceiver()
295290
}
296291

292+
fun requestLocationIfNeeded() {
293+
if (!isVisible) return // skips if not visible to user
294+
if (locationPermissionsHelper!!.checkLocationPermission(requireActivity())) {
295+
if (locationPermissionsHelper!!.isLocationAccessToAppsTurnedOn()) {
296+
locationManager.registerLocationManager()
297+
drawMyLocationMarker()
298+
} else {
299+
locationPermissionsHelper!!.showLocationOffDialog(requireActivity(), R.string.location_off_dialog_text)
300+
}
301+
} else {
302+
locationPermissionsHelper!!.requestForLocationAccess(
303+
R.string.location_permission_title,
304+
R.string.location_permission_rationale
305+
)
306+
}
307+
}
308+
309+
private fun drawMyLocationMarker() {
310+
val location = locationManager.getLastLocation()
311+
if (location != null) {
312+
val geoPoint = GeoPoint(location.latitude, location.longitude)
313+
val startMarker = Marker(binding!!.mapView).apply {
314+
setPosition(geoPoint)
315+
setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM)
316+
icon = ContextCompat.getDrawable(requireContext(), R.drawable.current_location_marker)
317+
title = "Your Location"
318+
textLabelFontSize = 24
319+
}
320+
binding!!.mapView.overlays.add(startMarker)
321+
binding!!.mapView.invalidate()
322+
}
323+
}
297324

298325
/**
299326
* Unregisters the networkReceiver
@@ -1079,7 +1106,24 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
10791106

10801107
override fun onLocationPermissionDenied(toastMessage: String) = Unit
10811108

1082-
override fun onLocationPermissionGranted() = Unit
1109+
override fun onLocationPermissionGranted() {
1110+
if (locationPermissionsHelper!!.isLocationAccessToAppsTurnedOn()) {
1111+
locationManager.registerLocationManager()
1112+
drawMyLocationMarker()
1113+
} else {
1114+
locationPermissionsHelper!!.showLocationOffDialog(requireActivity(), R.string.location_off_dialog_text)
1115+
}
1116+
onLocationChanged(LocationChangeType.PERMISSION_JUST_GRANTED, null)
1117+
}
1118+
1119+
fun onLocationChanged(locationChangeType: LocationChangeType, location: Location?) {
1120+
if (locationChangeType == LocationChangeType.PERMISSION_JUST_GRANTED) {
1121+
val curLatLng = locationManager.getLastLocation() ?: getMapCenter()
1122+
populatePlaces(curLatLng)
1123+
} else {
1124+
presenter!!.updateMap(locationChangeType)
1125+
}
1126+
}
10831127

10841128
companion object {
10851129
fun newInstance(): ExploreMapFragment {

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@ Upload your first media by tapping on the add button.</string>
748748
<string name="wlm_campaign_description">Wiki Loves Monuments is an international photo contest for monuments organised by Wikimedia</string>
749749
<string name="need_permission">Need Permission</string>
750750
<string name="read_phone_state_permission_message">Nearby maps need to read PHONE STATE to function properly</string>
751+
<string name="location_off_dialog_text">Please turn on location services to view nearby places.</string>
752+
<string name="location_permission_rationale">Location access is needed to show nearby places on the map.</string>
751753

752754
<string name="contributions_of_user">Contributions of User: %s</string>
753755
<string name="achievements_of_user">Achievements of User: %s</string>

0 commit comments

Comments
 (0)