Explore: Fix crashes caused by unattached map fragment

This commit is contained in:
Ifeoluwa Andrew Omole 2025-01-29 19:44:50 +01:00
parent da086454c0
commit 4be35d0784

View file

@ -701,6 +701,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
* @param nearbyBaseMarker The NearbyBaseMarker object representing the marker to be added. * @param nearbyBaseMarker The NearbyBaseMarker object representing the marker to be added.
*/ */
private void addMarkerToMap(BaseMarker nearbyBaseMarker) { private void addMarkerToMap(BaseMarker nearbyBaseMarker) {
if (isAttachedToActivity()) {
ArrayList<OverlayItem> items = new ArrayList<>(); ArrayList<OverlayItem> items = new ArrayList<>();
Bitmap icon = nearbyBaseMarker.getIcon(); Bitmap icon = nearbyBaseMarker.getIcon();
Drawable d = new BitmapDrawable(getResources(), icon); Drawable d = new BitmapDrawable(getResources(), icon);
@ -720,7 +721,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
removeMarker(clickedMarker); removeMarker(clickedMarker);
addMarkerToMap(clickedMarker); addMarkerToMap(clickedMarker);
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); bottomSheetDetailsBehavior.setState(
BottomSheetBehavior.STATE_COLLAPSED);
} }
clickedMarker = nearbyBaseMarker; clickedMarker = nearbyBaseMarker;
passInfoToSheet(place); passInfoToSheet(place);
@ -736,6 +738,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
overlay.setFocusItemsOnTap(true); overlay.setFocusItemsOnTap(true);
binding.mapView.getOverlays().add(overlay); // Add the overlay to the map binding.mapView.getOverlays().add(overlay); // Add the overlay to the map
} }
}
/** /**
* Removes a marker from the map based on the specified NearbyBaseMarker. * Removes a marker from the map based on the specified NearbyBaseMarker.
@ -768,6 +771,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
*/ */
@Override @Override
public void clearAllMarkers() { public void clearAllMarkers() {
if (isAttachedToActivity()) {
binding.mapView.getOverlayManager().clear(); binding.mapView.getOverlayManager().clear();
GeoPoint geoPoint = mapCenter; GeoPoint geoPoint = mapCenter;
if (geoPoint != null) { if (geoPoint != null) {
@ -793,7 +797,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
startMarker.setAnchor(org.osmdroid.views.overlay.Marker.ANCHOR_CENTER, startMarker.setAnchor(org.osmdroid.views.overlay.Marker.ANCHOR_CENTER,
org.osmdroid.views.overlay.Marker.ANCHOR_BOTTOM); org.osmdroid.views.overlay.Marker.ANCHOR_BOTTOM);
startMarker.setIcon( startMarker.setIcon(
ContextCompat.getDrawable(this.getContext(), R.drawable.current_location_marker)); ContextCompat.getDrawable(this.getContext(),
R.drawable.current_location_marker));
startMarker.setTitle("Your Location"); startMarker.setTitle("Your Location");
startMarker.setTextLabelFontSize(24); startMarker.setTextLabelFontSize(24);
binding.mapView.getOverlays().add(startMarker); binding.mapView.getOverlays().add(startMarker);
@ -815,7 +820,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
} else { } else {
Timber.e("CLICKED MARKER IS NULL"); Timber.e("CLICKED MARKER IS NULL");
} }
if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) { if (bottomSheetDetailsBehavior.getState()
== BottomSheetBehavior.STATE_EXPANDED) {
// Back should first hide the bottom sheet if it is expanded // Back should first hide the bottom sheet if it is expanded
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
} else if (isDetailsBottomSheetVisible()) { } else if (isDetailsBottomSheetVisible()) {
@ -831,6 +837,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
})); }));
binding.mapView.setMultiTouchControls(true); binding.mapView.setMultiTouchControls(true);
} }
}
/** /**
* Recenters the map view to the specified GeoPoint and updates the marker to indicate the new * Recenters the map view to the specified GeoPoint and updates the marker to indicate the new
@ -986,6 +993,14 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
}; };
} }
/**
* helper function to confirm that this fragment has been attached.
**/
public boolean isAttachedToActivity() {
boolean attached = isVisible() && getActivity() != null;
return attached;
}
@Override @Override
public void onLocationPermissionDenied(String toastMessage) { public void onLocationPermissionDenied(String toastMessage) {
} }