Fix modifying list

This commit is contained in:
PavelRatushny 2017-11-13 15:47:47 +02:00
parent 3ec1ccfde4
commit a44518009e

View file

@ -269,7 +269,7 @@ public class MapMarkersHelper {
mapMarkersHistory.clear(); mapMarkersHistory.clear();
List<MapMarker> activeMarkers = markersDbHelper.getActiveMarkers(); List<MapMarker> activeMarkers = markersDbHelper.getActiveMarkers();
mapMarkers.addAll(activeMarkers); addToMapMarkersList(activeMarkers);
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
List<MapMarker> markersHistory = markersDbHelper.getMarkersHistory(); List<MapMarker> markersHistory = markersDbHelper.getMarkersHistory();
@ -281,6 +281,32 @@ public class MapMarkersHelper {
} }
} }
private void removeFromMapMarkersList(MapMarker marker) {
List<MapMarker> copyList = new ArrayList<>();
copyList.remove(marker);
mapMarkers = copyList;
}
private void addToMapMarkersList(MapMarker marker) {
addToMapMarkersList(mapMarkers.size(), marker);
}
private void addToMapMarkersList(int position, MapMarker marker) {
List<MapMarker> copyList = new ArrayList<>(mapMarkers);
copyList.add(position, marker);
mapMarkers = copyList;
}
private void addToMapMarkersList(List<MapMarker> markers) {
addToMapMarkersList(mapMarkers.size(), markers);
}
private void addToMapMarkersList(int position, List<MapMarker> markers) {
List<MapMarker> copyList = new ArrayList<>();
copyList.addAll(position, markers);
mapMarkers = copyList;
}
public void reorderActiveMarkersIfNeeded() { public void reorderActiveMarkersIfNeeded() {
if (!mapMarkers.isEmpty()) { if (!mapMarkers.isEmpty()) {
if (mapMarkers.size() > 1) { if (mapMarkers.size() > 1) {
@ -458,7 +484,7 @@ public class MapMarkersHelper {
for (MapMarker marker : markers) { for (MapMarker marker : markers) {
if (!marker.history) { if (!marker.history) {
markersDbHelper.removeMarker(marker, false); markersDbHelper.removeMarker(marker, false);
mapMarkers.remove(marker); removeFromMapMarkersList(marker);
removeMarkerFromGroup(marker); removeMarkerFromGroup(marker);
needRefresh = true; needRefresh = true;
} }
@ -474,7 +500,7 @@ public class MapMarkersHelper {
if (marker != null) { if (marker != null) {
cancelPointAddressRequests(marker.point); cancelPointAddressRequests(marker.point);
markersDbHelper.moveMarkerToHistory(marker); markersDbHelper.moveMarkerToHistory(marker);
mapMarkers.remove(marker); removeFromMapMarkersList(marker);
marker.history = true; marker.history = true;
marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE; marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
mapMarkersHistory.add(marker); mapMarkersHistory.add(marker);
@ -487,7 +513,7 @@ public class MapMarkersHelper {
public void addMarkers(List<MapMarker> markers) { public void addMarkers(List<MapMarker> markers) {
if (markers != null) { if (markers != null) {
markersDbHelper.addMarkers(markers); markersDbHelper.addMarkers(markers);
mapMarkers.addAll(markers); addToMapMarkersList(markers);
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
addMarkersToGroups(markers, true); addMarkersToGroups(markers, true);
refresh(); refresh();
@ -501,7 +527,7 @@ public class MapMarkersHelper {
mapMarkersHistory.add(marker); mapMarkersHistory.add(marker);
sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
} else { } else {
mapMarkers.add(marker); addToMapMarkersList(marker);
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
} }
addMarkerToGroup(marker); addMarkerToGroup(marker);
@ -514,7 +540,7 @@ public class MapMarkersHelper {
markersDbHelper.restoreMapMarkerFromHistory(marker); markersDbHelper.restoreMapMarkerFromHistory(marker);
mapMarkersHistory.remove(marker); mapMarkersHistory.remove(marker);
marker.history = false; marker.history = false;
mapMarkers.add(position, marker); addToMapMarkersList(position, marker);
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
refresh(); refresh();
@ -527,7 +553,7 @@ public class MapMarkersHelper {
markersDbHelper.restoreMapMarkerFromHistory(marker); markersDbHelper.restoreMapMarkerFromHistory(marker);
mapMarkersHistory.remove(marker); mapMarkersHistory.remove(marker);
marker.history = false; marker.history = false;
mapMarkers.add(marker); addToMapMarkersList(marker);
} }
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
@ -543,7 +569,7 @@ public class MapMarkersHelper {
if (history) { if (history) {
mapMarkersHistory.remove(marker); mapMarkersHistory.remove(marker);
} else { } else {
mapMarkers.remove(marker); removeFromMapMarkersList(marker);
} }
removeMarkerFromGroup(marker); removeMarkerFromGroup(marker);
refresh(); refresh();
@ -619,7 +645,7 @@ public class MapMarkersHelper {
} }
mapMarkers.removeAll(markersToRemove); mapMarkers.removeAll(markersToRemove);
mapMarkers.addAll(0, markers); addToMapMarkersList(0, markers);
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
ctx.getSettings().MAP_MARKERS_ORDER_BY_MODE.set(OsmandSettings.MapMarkersOrderByMode.CUSTOM); ctx.getSettings().MAP_MARKERS_ORDER_BY_MODE.set(OsmandSettings.MapMarkersOrderByMode.CUSTOM);
} }
@ -725,9 +751,9 @@ public class MapMarkersHelper {
} }
} else { } else {
if (disabled) { if (disabled) {
mapMarkers.remove(marker); removeFromMapMarkersList(marker);
} else { } else {
mapMarkers.add(marker); addToMapMarkersList(marker);
} }
} }
} }
@ -797,7 +823,7 @@ public class MapMarkersHelper {
marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE; marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE;
markersDbHelper.addMarker(marker); markersDbHelper.addMarker(marker);
if (enabled) { if (enabled) {
mapMarkers.add(0, marker); addToMapMarkersList(0, marker);
} }
addedMarkers.add(marker); addedMarkers.add(marker);
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
@ -819,8 +845,8 @@ public class MapMarkersHelper {
public void moveMarkerToTop(MapMarker marker) { public void moveMarkerToTop(MapMarker marker) {
int i = mapMarkers.indexOf(marker); int i = mapMarkers.indexOf(marker);
if (i != -1 && mapMarkers.size() > 1) { if (i != -1 && mapMarkers.size() > 1) {
mapMarkers.remove(i); removeFromMapMarkersList(marker);
mapMarkers.add(0, marker); addToMapMarkersList(0, marker);
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
refresh(); refresh();
} }