Added location prompt at Map screen

This commit is contained in:
jagadeesh-18-bot 2025-09-09 00:23:55 +05:30
parent fcc63b9f09
commit 0785a2db90
4 changed files with 75 additions and 16 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,11 @@ 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
if (position == 2) {
mapRootFragment?.requestLocationIfNeeded()
}
}
})

View file

@ -193,6 +193,17 @@ 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

@ -278,14 +278,11 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
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 +291,38 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
unregisterNetworkReceiver()
}
fun requestLocationIfNeeded() {
if (!isVisible) return // Skip 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
@ -807,7 +836,7 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
}
/**
* Retrieves the specific Media object from the mediaList field.
* Retrieves the specific Mediaa object from the mediaList field.
* @param url The specific Media's image URL.
* @return The Media object that matches the URL or null if it could not be found.
*/
@ -1079,7 +1108,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>