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

This commit is contained in:
Ifeoluwa Andrew Omole 2025-01-25 18:12:28 +01:00
parent 9d590fdee4
commit e96ab2bd22
3 changed files with 63 additions and 7 deletions

View file

@ -452,7 +452,6 @@ public class MainActivity extends BaseActivity
bundle.putDouble("prev_longitude", longitude);
loadFragment(ExploreFragment.newInstance(), false, bundle);
setSelectedItemId(NavTab.EXPLORE.code());
}
/**
@ -470,7 +469,6 @@ public class MainActivity extends BaseActivity
bundle.putDouble("prev_longitude", longitude);
loadFragment(NearbyParentFragment.newInstance(), false, bundle);
setSelectedItemId(NavTab.NEARBY.code());
}
@Override

View file

@ -198,6 +198,12 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.explore_fragment_menu, menu);
MenuItem others = menu.findItem(R.id.list_item_show_in_nearby);
if (binding.viewPager.getCurrentItem() == 2) {
others.setVisible(true);
}
// if on Map tab, show all menu options, else only show search
binding.viewPager.addOnPageChangeListener(new OnPageChangeListener() {
@Override
@ -207,8 +213,7 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
@Override
public void onPageSelected(int position) {
MenuItem other = menu.findItem(R.id.list_item_show_in_nearby);
other.setVisible((position == 2));
others.setVisible((position == 2));
}
@Override

View file

@ -233,6 +233,11 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private Place nearestPlace;
private volatile boolean stopQuery;
// Explore map data (for if we came from Explore)
private double prevZoom;
private double prevLatitude;
private double prevLongitude;
private final Handler searchHandler = new Handler();
private Runnable searchRunnable;
@ -338,6 +343,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
loadExploreMapData();
binding = FragmentNearbyParentBinding.inflate(inflater, container, false);
view = binding.getRoot();
@ -481,6 +488,14 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
binding.map.getOverlays().add(scaleBarOverlay);
binding.map.getZoomController().setVisibility(Visibility.NEVER);
binding.map.getController().setZoom(ZOOM_LEVEL);
// if we came from Explore map using 'Show in Nearby', load Explore map camera position
if (isCameFromExploreMap()) {
moveCameraToPosition(
new GeoPoint(prevLatitude, prevLongitude),
prevZoom,
2L
);
}
binding.map.getOverlays().add(mapEventsOverlay);
binding.map.addMapListener(new MapListener() {
@ -503,7 +518,9 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
}
initNearbyFilter();
addCheckBoxCallback();
moveCameraToPosition(lastMapFocus);
if (!isCameFromExploreMap()) {
moveCameraToPosition(lastMapFocus);
}
initRvNearbyList();
onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -560,6 +577,28 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
}
}
/**
* Fetch Explore map camera data from fragment arguments if any.
*/
public void loadExploreMapData() {
// 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 Explore map. if present, then the user
* navigated from Explore using 'Show in Nearby'.
*
* @return true if user navigated from Explore map
**/
public boolean isCameFromExploreMap() {
return prevZoom != 0.0 || prevLatitude != 0.0 || prevLongitude != 0.0;
}
/**
* Initialise background based on theme, this should be doe ideally via styles, that would need
* another refactor
@ -640,7 +679,9 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
mapCenter = targetP;
binding.map.getController().setCenter(targetP);
recenterMarkerToPosition(targetP);
moveCameraToPosition(targetP);
if (!isCameFromExploreMap()) {
moveCameraToPosition(targetP);
}
} else if (locationManager.isGPSProviderEnabled()
|| locationManager.isNetworkProviderEnabled()) {
locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER);
@ -684,7 +725,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
} else {
lastKnownLocation = MapUtils.getDefaultLatLng();
}
if (binding.map != null) {
if (binding.map != null && !isCameFromExploreMap()) {
moveCameraToPosition(
new GeoPoint(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()));
}
@ -2313,6 +2354,18 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
binding.map.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.map.getController().animateTo(geoPoint, zoom, speed);
}
@Override
public void onBottomSheetItemClick(@Nullable View view, int position) {
BottomSheetItem item = dataList.get(position);