Explore: Read fragment arguments for Nearby map data and update Explore map if present

This commit is contained in:
Ifeoluwa Andrew Omole 2025-01-24 20:05:26 +01:00
parent 3eda5dc169
commit a7da455ca4
3 changed files with 146 additions and 30 deletions

View file

@ -42,9 +42,13 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
@Named("default_preferences") @Named("default_preferences")
public JsonKvStore applicationKvStore; public JsonKvStore applicationKvStore;
public void setScroll(boolean canScroll){ // Nearby map state (for if we came from Nearby fragment)
if (binding != null) private double prevZoom;
{ private double prevLatitude;
private double prevLongitude;
public void setScroll(boolean canScroll) {
if (binding != null) {
binding.viewPager.setCanScroll(canScroll); binding.viewPager.setCanScroll(canScroll);
} }
} }
@ -60,6 +64,7 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) { @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
loadNearbyMapData();
binding = FragmentExploreBinding.inflate(inflater, container, false); binding = FragmentExploreBinding.inflate(inflater, container, false);
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager()); viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
@ -89,6 +94,11 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
}); });
setTabs(); setTabs();
setHasOptionsMenu(true); setHasOptionsMenu(true);
// if we came from 'Show in Explore' in Nearby, jump to Map tab
if (isCameFromNearbyMap()) {
binding.viewPager.setCurrentItem(2);
}
return binding.getRoot(); return binding.getRoot();
} }
@ -108,6 +118,13 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
Bundle mapArguments = new Bundle(); Bundle mapArguments = new Bundle();
mapArguments.putString("categoryName", EXPLORE_MAP); mapArguments.putString("categoryName", EXPLORE_MAP);
// if we came from 'Show in Explore' in Nearby, pass on zoom and center to Explore map root
if (isCameFromNearbyMap()) {
mapArguments.putDouble("prev_zoom", prevZoom);
mapArguments.putDouble("prev_latitude", prevLatitude);
mapArguments.putDouble("prev_longitude", prevLongitude);
}
featuredRootFragment = new ExploreListRootFragment(featuredArguments); featuredRootFragment = new ExploreListRootFragment(featuredArguments);
mobileRootFragment = new ExploreListRootFragment(mobileArguments); mobileRootFragment = new ExploreListRootFragment(mobileArguments);
mapRootFragment = new ExploreMapRootFragment(mapArguments); mapRootFragment = new ExploreMapRootFragment(mapArguments);
@ -120,13 +137,35 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
fragmentList.add(mapRootFragment); fragmentList.add(mapRootFragment);
titleList.add(getString(R.string.explore_tab_title_map).toUpperCase(Locale.ROOT)); titleList.add(getString(R.string.explore_tab_title_map).toUpperCase(Locale.ROOT));
((MainActivity)getActivity()).showTabs(); ((MainActivity) getActivity()).showTabs();
((BaseActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); ((BaseActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
viewPagerAdapter.setTabData(fragmentList, titleList); viewPagerAdapter.setTabData(fragmentList, titleList);
viewPagerAdapter.notifyDataSetChanged(); viewPagerAdapter.notifyDataSetChanged();
} }
/**
* Fetch Nearby map camera data from fragment arguments if any.
*/
public void loadNearbyMapData() {
// get fragment arguments
if (getArguments() != null) {
prevZoom = getArguments().getDouble("prev_zoom");
prevLatitude = getArguments().getDouble("prev_latitude");
prevLongitude = getArguments().getDouble("prev_longitude");
}
}
/**
* Checks if fragment arguments contain data from Nearby map, indicating that the user navigated
* from Nearby using 'Show in Explore'.
*
* @return true if user navigated from Nearby map
**/
public boolean isCameFromNearbyMap() {
return prevZoom != 0.0 || prevLatitude != 0.0 || prevLongitude != 0.0;
}
public boolean onBackPressed() { public boolean onBackPressed() {
if (binding.tabLayout.getSelectedTabPosition() == 0) { if (binding.tabLayout.getSelectedTabPosition() == 0) {
if (featuredRootFragment.backPressed()) { if (featuredRootFragment.backPressed()) {

View file

@ -39,10 +39,22 @@ public class ExploreMapRootFragment extends CommonsDaggerSupportFragment impleme
} }
public ExploreMapRootFragment(Bundle bundle) { public ExploreMapRootFragment(Bundle bundle) {
// get fragment arguments
String title = bundle.getString("categoryName"); String title = bundle.getString("categoryName");
double zoom = bundle.getDouble("prev_zoom");
double latitude = bundle.getDouble("prev_latitude");
double longitude = bundle.getDouble("prev_longitude");
mapFragment = new ExploreMapFragment(); mapFragment = new ExploreMapFragment();
Bundle featuredArguments = new Bundle(); Bundle featuredArguments = new Bundle();
featuredArguments.putString("categoryName", title); featuredArguments.putString("categoryName", title);
// if we came from 'Show in Explore' in Nearby, pass on zoom and center
if (zoom != 0.0 || latitude != 0.0 || longitude != 0.0) {
featuredArguments.putDouble("prev_zoom", zoom);
featuredArguments.putDouble("prev_latitude", latitude);
featuredArguments.putDouble("prev_longitude", longitude);
}
mapFragment.setArguments(featuredArguments); mapFragment.setArguments(featuredArguments);
} }
@ -198,7 +210,8 @@ public class ExploreMapRootFragment extends CommonsDaggerSupportFragment impleme
((MainActivity) getActivity()).showTabs(); ((MainActivity) getActivity()).showTabs();
return true; return true;
} if (mapFragment != null && mapFragment.isVisible()) { }
if (mapFragment != null && mapFragment.isVisible()) {
if (mapFragment.backButtonClicked()) { if (mapFragment.backButtonClicked()) {
// Explore map fragment handled the event no further action required. // Explore map fragment handled the event no further action required.
return true; return true;

View file

@ -62,6 +62,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.events.MapEventsReceiver; import org.osmdroid.events.MapEventsReceiver;
import org.osmdroid.events.MapListener; import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent; import org.osmdroid.events.ScrollEvent;
@ -115,6 +116,11 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
SystemThemeUtils systemThemeUtils; SystemThemeUtils systemThemeUtils;
LocationPermissionsHelper locationPermissionsHelper; LocationPermissionsHelper locationPermissionsHelper;
// Nearby map state (if we came from Nearby)
private double prevZoom;
private double prevLatitude;
private double prevLongitude;
private ExploreMapPresenter presenter; private ExploreMapPresenter presenter;
public FragmentExploreMapBinding binding; public FragmentExploreMapBinding binding;
@ -160,6 +166,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
ViewGroup container, ViewGroup container,
Bundle savedInstanceState Bundle savedInstanceState
) { ) {
loadNearbyMapData();
binding = FragmentExploreMapBinding.inflate(getLayoutInflater()); binding = FragmentExploreMapBinding.inflate(getLayoutInflater());
return binding.getRoot(); return binding.getRoot();
} }
@ -169,12 +176,14 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
setSearchThisAreaButtonVisibility(false); setSearchThisAreaButtonVisibility(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution), Html.FROM_HTML_MODE_LEGACY)); binding.tvAttribution.setText(
Html.fromHtml(getString(R.string.map_attribution), Html.FROM_HTML_MODE_LEGACY));
} else { } else {
binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution))); binding.tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution)));
} }
initNetworkBroadCastReceiver(); initNetworkBroadCastReceiver();
locationPermissionsHelper = new LocationPermissionsHelper(getActivity(),locationManager,this); locationPermissionsHelper = new LocationPermissionsHelper(getActivity(), locationManager,
this);
if (presenter == null) { if (presenter == null) {
presenter = new ExploreMapPresenter(bookmarkLocationDao); presenter = new ExploreMapPresenter(bookmarkLocationDao);
} }
@ -204,7 +213,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
scaleBarOverlay.setBackgroundPaint(barPaint); scaleBarOverlay.setBackgroundPaint(barPaint);
scaleBarOverlay.enableScaleBar(); scaleBarOverlay.enableScaleBar();
binding.mapView.getOverlays().add(scaleBarOverlay); binding.mapView.getOverlays().add(scaleBarOverlay);
binding.mapView.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER); binding.mapView.getZoomController()
.setVisibility(CustomZoomButtonsController.Visibility.NEVER);
binding.mapView.setMultiTouchControls(true); binding.mapView.setMultiTouchControls(true);
binding.mapView.getController().setZoom(ZOOM_LEVEL); binding.mapView.getController().setZoom(ZOOM_LEVEL);
performMapReadyActions(); performMapReadyActions();
@ -295,7 +305,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
unregisterNetworkReceiver(); unregisterNetworkReceiver();
} }
/** /**
* Unregisters the networkReceiver * Unregisters the networkReceiver
*/ */
@ -328,11 +338,43 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
isPermissionDenied = true; isPermissionDenied = true;
} }
lastKnownLocation = MapUtils.getDefaultLatLng(); lastKnownLocation = MapUtils.getDefaultLatLng();
moveCameraToPosition(
new GeoPoint(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude())); // if we came from 'Show in Explore' in Nearby, load Nearby map center and zoom
if (isCameFromNearbyMap()) {
moveCameraToPosition(
new GeoPoint(prevLatitude, prevLongitude),
prevZoom,
2L
);
} else {
moveCameraToPosition(
new GeoPoint(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()));
}
presenter.onMapReady(exploreMapController); presenter.onMapReady(exploreMapController);
} }
/**
* Fetch Nearby map camera data from fragment arguments if any.
*/
public void loadNearbyMapData() {
// get fragment arguments
if (getArguments() != null) {
prevZoom = getArguments().getDouble("prev_zoom");
prevLatitude = getArguments().getDouble("prev_latitude");
prevLongitude = getArguments().getDouble("prev_longitude");
}
}
/**
* Checks if fragment arguments contain data from Nearby map, indicating that the user navigated
* from Nearby using 'Show in Explore'.
*
* @return true if user navigated from Nearby map
**/
public boolean isCameFromNearbyMap() {
return prevZoom != 0.0 || prevLatitude != 0.0 || prevLongitude != 0.0;
}
private void initViews() { private void initViews() {
Timber.d("init views called"); Timber.d("init views called");
initBottomSheets(); initBottomSheets();
@ -340,13 +382,14 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
} }
/** /**
* a) Creates bottom sheet behaviours from bottom sheet, sets initial states and visibility * a) Creates bottom sheet behaviours from bottom sheet, sets initial states and visibility b)
* b) Gets the touch event on the map to perform following actions: * Gets the touch event on the map to perform following actions: if bottom sheet details are
* if bottom sheet details are expanded or collapsed hide the bottom sheet details. * expanded or collapsed hide the bottom sheet details.
*/ */
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
private void initBottomSheets() { private void initBottomSheets() {
bottomSheetDetailsBehavior = BottomSheetBehavior.from(binding.bottomSheetDetailsBinding.getRoot()); bottomSheetDetailsBehavior = BottomSheetBehavior.from(
binding.bottomSheetDetailsBinding.getRoot());
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
binding.bottomSheetDetailsBinding.getRoot().setVisibility(View.VISIBLE); binding.bottomSheetDetailsBinding.getRoot().setVisibility(View.VISIBLE);
} }
@ -404,7 +447,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
if (currentLatLng == null) { if (currentLatLng == null) {
return; return;
} }
if (currentLatLng.equals(getLastMapFocus())) { // Means we are checking around current location if (currentLatLng.equals(
getLastMapFocus())) { // Means we are checking around current location
nearbyPlacesInfoObservable = presenter.loadAttractionsFromLocation(currentLatLng, nearbyPlacesInfoObservable = presenter.loadAttractionsFromLocation(currentLatLng,
getLastMapFocus(), true); getLastMapFocus(), true);
} else { } else {
@ -416,11 +460,12 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(explorePlacesInfo -> { .subscribe(explorePlacesInfo -> {
mediaList = explorePlacesInfo.mediaList; mediaList = explorePlacesInfo.mediaList;
if(mediaList == null) { if (mediaList == null) {
showResponseMessage(getString(R.string.no_pictures_in_this_area)); showResponseMessage(getString(R.string.no_pictures_in_this_area));
} }
updateMapMarkers(explorePlacesInfo); updateMapMarkers(explorePlacesInfo);
lastMapFocus = new GeoPoint(currentLatLng.getLatitude(), currentLatLng.getLongitude()); lastMapFocus = new GeoPoint(currentLatLng.getLatitude(),
currentLatLng.getLongitude());
}, },
throwable -> { throwable -> {
Timber.d(throwable); Timber.d(throwable);
@ -474,9 +519,9 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER); locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdatesFromProvider(LocationManager.GPS_PROVIDER); locationManager.requestLocationUpdatesFromProvider(LocationManager.GPS_PROVIDER);
setProgressBarVisibility(true); setProgressBarVisibility(true);
} } else {
else { locationPermissionsHelper.showLocationOffDialog(getActivity(),
locationPermissionsHelper.showLocationOffDialog(getActivity(), R.string.ask_to_turn_location_on_text); R.string.ask_to_turn_location_on_text);
} }
presenter.onMapReady(exploreMapController); presenter.onMapReady(exploreMapController);
registerUnregisterLocationListener(false); registerUnregisterLocationListener(false);
@ -508,7 +553,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
recenterToUserLocation = true; recenterToUserLocation = true;
return; return;
} }
recenterMarkerToPosition(new GeoPoint(currentLatLng.getLatitude(), currentLatLng.getLongitude())); recenterMarkerToPosition(
new GeoPoint(currentLatLng.getLatitude(), currentLatLng.getLongitude()));
binding.mapView.getController() binding.mapView.getController()
.animateTo(new GeoPoint(currentLatLng.getLatitude(), currentLatLng.getLongitude())); .animateTo(new GeoPoint(currentLatLng.getLatitude(), currentLatLng.getLongitude()));
if (lastMapFocus != null) { if (lastMapFocus != null) {
@ -545,10 +591,12 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
* @param place Place of clicked nearby marker * @param place Place of clicked nearby marker
*/ */
private void passInfoToSheet(final Place place) { private void passInfoToSheet(final Place place) {
binding.bottomSheetDetailsBinding.directionsButton.setOnClickListener(view -> Utils.handleGeoCoordinates(getActivity(), binding.bottomSheetDetailsBinding.directionsButton.setOnClickListener(
place.getLocation())); view -> Utils.handleGeoCoordinates(getActivity(),
place.getLocation()));
binding.bottomSheetDetailsBinding.commonsButton.setVisibility(place.hasCommonsLink() ? View.VISIBLE : View.GONE); binding.bottomSheetDetailsBinding.commonsButton.setVisibility(
place.hasCommonsLink() ? View.VISIBLE : View.GONE);
binding.bottomSheetDetailsBinding.commonsButton.setOnClickListener( binding.bottomSheetDetailsBinding.commonsButton.setOnClickListener(
view -> Utils.handleWebUrl(getContext(), place.siteLinks.getCommonsLink())); view -> Utils.handleWebUrl(getContext(), place.siteLinks.getCommonsLink()));
@ -562,7 +610,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
} }
index++; index++;
} }
binding.bottomSheetDetailsBinding.title.setText(place.name.substring(5, place.name.lastIndexOf("."))); binding.bottomSheetDetailsBinding.title.setText(
place.name.substring(5, place.name.lastIndexOf(".")));
binding.bottomSheetDetailsBinding.category.setText(place.distance); binding.bottomSheetDetailsBinding.category.setText(place.distance);
// Remove label since it is double information // Remove label since it is double information
String descriptionText = place.getLongDescription() String descriptionText = place.getLongDescription()
@ -825,6 +874,18 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
binding.mapView.getController().animateTo(geoPoint); binding.mapView.getController().animateTo(geoPoint);
} }
/**
* Moves the camera of the map view to the specified GeoPoint at specified zoom level and speed
* using an animation.
*
* @param geoPoint The GeoPoint representing the new camera position for the map.
* @param zoom Zoom level of the map camera
* @param speed Speed of animation
*/
private void moveCameraToPosition(GeoPoint geoPoint, double zoom, long speed) {
binding.mapView.getController().animateTo(geoPoint, zoom, speed);
}
@Override @Override
public fr.free.nrw.commons.location.LatLng getLastMapFocus() { public fr.free.nrw.commons.location.LatLng getLastMapFocus() {
return lastMapFocus == null ? getMapCenter() : new fr.free.nrw.commons.location.LatLng( return lastMapFocus == null ? getMapCenter() : new fr.free.nrw.commons.location.LatLng(
@ -850,14 +911,15 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
-0.07483536015053005, 1f); -0.07483536015053005, 1f);
} }
} }
moveCameraToPosition(new GeoPoint(latLnge.getLatitude(),latLnge.getLongitude())); moveCameraToPosition(new GeoPoint(latLnge.getLatitude(), latLnge.getLongitude()));
return latLnge; return latLnge;
} }
@Override @Override
public fr.free.nrw.commons.location.LatLng getMapFocus() { public fr.free.nrw.commons.location.LatLng getMapFocus() {
fr.free.nrw.commons.location.LatLng mapFocusedLatLng = new fr.free.nrw.commons.location.LatLng( fr.free.nrw.commons.location.LatLng mapFocusedLatLng = new fr.free.nrw.commons.location.LatLng(
binding.mapView.getMapCenter().getLatitude(), binding.mapView.getMapCenter().getLongitude(), 100); binding.mapView.getMapCenter().getLatitude(),
binding.mapView.getMapCenter().getLongitude(), 100);
return mapFocusedLatLng; return mapFocusedLatLng;
} }
@ -911,8 +973,10 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
} }
@Override @Override
public void onLocationPermissionDenied(String toastMessage) {} public void onLocationPermissionDenied(String toastMessage) {
}
@Override @Override
public void onLocationPermissionGranted() {} public void onLocationPermissionGranted() {
}
} }