diff --git a/app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.kt b/app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.kt index ea96b50a3..a207c35f0 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.kt +++ b/app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.kt @@ -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() } } }) diff --git a/app/src/main/java/fr/free/nrw/commons/explore/ExploreMapRootFragment.kt b/app/src/main/java/fr/free/nrw/commons/explore/ExploreMapRootFragment.kt index af65834eb..e345e3e09 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/ExploreMapRootFragment.kt +++ b/app/src/main/java/fr/free/nrw/commons/explore/ExploreMapRootFragment.kt @@ -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 diff --git a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.kt b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.kt index e64f12db3..dea6f3df8 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.kt +++ b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.kt @@ -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 { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f58a0fd3c..6447f552a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -748,6 +748,8 @@ Upload your first media by tapping on the add button. Wiki Loves Monuments is an international photo contest for monuments organised by Wikimedia Need Permission Nearby maps need to read PHONE STATE to function properly + Please turn on location services to view nearby places. + Location access is needed to show nearby places on the map. Contributions of User: %s Achievements of User: %s