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.
*/
private void addMarkerToMap(BaseMarker nearbyBaseMarker) {
if (isAttachedToActivity()) {
ArrayList<OverlayItem> items = new ArrayList<>();
Bitmap icon = nearbyBaseMarker.getIcon();
Drawable d = new BitmapDrawable(getResources(), icon);
@ -720,7 +721,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
removeMarker(clickedMarker);
addMarkerToMap(clickedMarker);
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
bottomSheetDetailsBehavior.setState(
BottomSheetBehavior.STATE_COLLAPSED);
}
clickedMarker = nearbyBaseMarker;
passInfoToSheet(place);
@ -736,6 +738,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
overlay.setFocusItemsOnTap(true);
binding.mapView.getOverlays().add(overlay); // Add the overlay to the map
}
}
/**
* Removes a marker from the map based on the specified NearbyBaseMarker.
@ -768,6 +771,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
*/
@Override
public void clearAllMarkers() {
if (isAttachedToActivity()) {
binding.mapView.getOverlayManager().clear();
GeoPoint geoPoint = mapCenter;
if (geoPoint != null) {
@ -793,7 +797,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
startMarker.setAnchor(org.osmdroid.views.overlay.Marker.ANCHOR_CENTER,
org.osmdroid.views.overlay.Marker.ANCHOR_BOTTOM);
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.setTextLabelFontSize(24);
binding.mapView.getOverlays().add(startMarker);
@ -815,7 +820,8 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
} else {
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
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
} else if (isDetailsBottomSheetVisible()) {
@ -831,6 +837,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
}));
binding.mapView.setMultiTouchControls(true);
}
}
/**
* 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
public void onLocationPermissionDenied(String toastMessage) {
}