Fix changing lists and fix adapters

This commit is contained in:
PavelRatushny 2017-11-14 13:32:10 +02:00
parent 7ce58add2d
commit 4efaccc61c
7 changed files with 102 additions and 128 deletions

View file

@ -5,7 +5,6 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
@ -24,7 +23,6 @@ import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
@ -265,8 +263,8 @@ public class MapMarkersHelper {
}
private void loadMarkers() {
mapMarkers.clear();
mapMarkersHistory.clear();
mapMarkers = new LinkedList<>();
mapMarkersHistory = new LinkedList<>();
List<MapMarker> activeMarkers = markersDbHelper.getActiveMarkers();
addToMapMarkersList(activeMarkers);
@ -274,15 +272,21 @@ public class MapMarkersHelper {
List<MapMarker> markersHistory = markersDbHelper.getMarkersHistory();
sortMarkers(markersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
mapMarkersHistory.addAll(markersHistory);
addToMapMarkersHistoryList(markersHistory);
if (!ctx.isApplicationInitializing()) {
lookupAddressAll();
}
}
private void removeFromMapMarkersList(List<MapMarker> markers) {
List<MapMarker> copyList = new LinkedList<>(mapMarkers);
copyList.removeAll(markers);
mapMarkers = copyList;
}
private void removeFromMapMarkersList(MapMarker marker) {
List<MapMarker> copyList = new ArrayList<>(mapMarkers);
List<MapMarker> copyList = new LinkedList<>(mapMarkers);
copyList.remove(marker);
mapMarkers = copyList;
}
@ -292,7 +296,7 @@ public class MapMarkersHelper {
}
private void addToMapMarkersList(int position, MapMarker marker) {
List<MapMarker> copyList = new ArrayList<>(mapMarkers);
List<MapMarker> copyList = new LinkedList<>(mapMarkers);
copyList.add(position, marker);
mapMarkers = copyList;
}
@ -302,11 +306,53 @@ public class MapMarkersHelper {
}
private void addToMapMarkersList(int position, List<MapMarker> markers) {
List<MapMarker> copyList = new ArrayList<>(mapMarkers);
List<MapMarker> copyList = new LinkedList<>(mapMarkers);
copyList.addAll(position, markers);
mapMarkers = copyList;
}
private void removeFromMapMarkersHistoryList(MapMarker marker) {
List<MapMarker> copyList = new LinkedList<>(mapMarkersHistory);
copyList.remove(marker);
mapMarkersHistory = copyList;
}
private void addToMapMarkersHistoryList(MapMarker marker) {
addToMapMarkersHistoryList(mapMarkersHistory.size(), marker);
}
private void addToMapMarkersHistoryList(int position, MapMarker marker) {
List<MapMarker> copyList = new LinkedList<>(mapMarkersHistory);
copyList.add(position, marker);
mapMarkersHistory = copyList;
}
private void addToMapMarkersHistoryList(int position, List<MapMarker> markers) {
List<MapMarker> copyList = new LinkedList<>(mapMarkersHistory);
copyList.addAll(position, markers);
mapMarkersHistory = copyList;
}
private void addToMapMarkersHistoryList(List<MapMarker> markers) {
addToMapMarkersHistoryList(mapMarkersHistory.size(), markers);
}
private void removeFromGroupsList(MapMarkersGroup group) {
List<MapMarkersGroup> copyList = new ArrayList<>(mapMarkersGroups);
copyList.remove(group);
mapMarkersGroups = copyList;
}
private void addToGroupsList(int position, MapMarkersGroup group) {
List<MapMarkersGroup> copyList = new ArrayList<>(mapMarkersGroups);
copyList.add(position, group);
mapMarkersGroups = copyList;
}
private void addToGroupsList(MapMarkersGroup group) {
addToGroupsList(mapMarkersGroups.size(), group);
}
public void reorderActiveMarkersIfNeeded() {
if (!mapMarkers.isEmpty()) {
if (mapMarkers.size() > 1) {
@ -507,7 +553,7 @@ public class MapMarkersHelper {
removeFromMapMarkersList(marker);
marker.history = true;
marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
mapMarkersHistory.add(marker);
addToMapMarkersHistoryList(marker);
reorderActiveMarkersIfNeeded();
sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
refresh();
@ -528,7 +574,7 @@ public class MapMarkersHelper {
if (marker != null) {
markersDbHelper.addMarker(marker);
if (marker.history) {
mapMarkersHistory.add(marker);
addToMapMarkersHistoryList(marker);
sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
} else {
addToMapMarkersList(marker);
@ -542,7 +588,7 @@ public class MapMarkersHelper {
public void restoreMarkerFromHistory(MapMarker marker, int position) {
if (marker != null) {
markersDbHelper.restoreMapMarkerFromHistory(marker);
mapMarkersHistory.remove(marker);
removeFromMapMarkersHistoryList(marker);
marker.history = false;
addToMapMarkersList(position, marker);
reorderActiveMarkersIfNeeded();
@ -555,7 +601,7 @@ public class MapMarkersHelper {
if (markers != null) {
for (MapMarker marker : markers) {
markersDbHelper.restoreMapMarkerFromHistory(marker);
mapMarkersHistory.remove(marker);
removeFromMapMarkersHistoryList(marker);
marker.history = false;
addToMapMarkersList(marker);
}
@ -571,7 +617,7 @@ public class MapMarkersHelper {
boolean history = marker.history;
markersDbHelper.removeMarker(marker, history);
if (history) {
mapMarkersHistory.remove(marker);
removeFromMapMarkersHistoryList(marker);
} else {
removeFromMapMarkersList(marker);
}
@ -648,7 +694,7 @@ public class MapMarkersHelper {
return;
}
mapMarkers.removeAll(markersToRemove);
removeFromMapMarkersList(markersToRemove);
addToMapMarkersList(0, markers);
reorderActiveMarkersIfNeeded();
ctx.getSettings().MAP_MARKERS_ORDER_BY_MODE.set(OsmandSettings.MapMarkersOrderByMode.CUSTOM);
@ -696,8 +742,8 @@ public class MapMarkersHelper {
marker.history = true;
marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
}
mapMarkersHistory.addAll(mapMarkers);
mapMarkers.clear();
addToMapMarkersHistoryList(mapMarkers);
mapMarkers = new LinkedList<>();
sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
updateGroups();
refresh();
@ -706,7 +752,7 @@ public class MapMarkersHelper {
public void removeMarkersHistory() {
cancelAddressRequests();
markersDbHelper.clearAllMarkersHistory();
mapMarkersHistory.clear();
mapMarkersHistory = new LinkedList<>();
refresh();
removeHistoryMarkersFromGroups();
}
@ -727,7 +773,7 @@ public class MapMarkersHelper {
}
MapMarkersGroup group = getMapMarkerGroupByKey(id);
if (group != null) {
mapMarkersGroups.remove(group);
removeFromGroupsList(group);
}
}
}
@ -749,9 +795,9 @@ public class MapMarkersHelper {
for (MapMarker marker : groupMarkers) {
if (marker.history) {
if (disabled) {
mapMarkersHistory.remove(marker);
removeFromMapMarkersHistoryList(marker);
} else {
mapMarkersHistory.add(marker);
addToMapMarkersHistoryList(marker);
}
} else {
if (disabled) {
@ -769,10 +815,12 @@ public class MapMarkersHelper {
public void removeActiveMarkersFromSyncGroup(String syncGroupId) {
if (syncGroupId != null) {
markersDbHelper.removeActiveMarkersFromSyncGroup(syncGroupId);
for (Iterator<MapMarker> iterator = mapMarkers.iterator(); iterator.hasNext(); ) {
String groupKey = iterator.next().groupKey;
List<MapMarker> copyList = new LinkedList<>(mapMarkers);
for (int i = 0; i < copyList.size(); i++) {
MapMarker marker = copyList.get(i);
String groupKey = marker.groupKey;
if (groupKey != null && groupKey.equals(syncGroupId)) {
iterator.remove();
removeFromMapMarkersList(marker);
}
}
reorderActiveMarkersIfNeeded();
@ -1020,7 +1068,7 @@ public class MapMarkersHelper {
public void updateGroup(MapMarkersGroup mapMarkersGroup) {
if (mapMarkersGroup.getMarkers().size() == 0) {
mapMarkersGroups.remove(mapMarkersGroup);
removeFromGroupsList(mapMarkersGroup);
return;
}
int historyMarkersCount = mapMarkersGroup.getHistoryMarkers().size();
@ -1086,7 +1134,7 @@ public class MapMarkersHelper {
group.setColor(MapMarker.getColorId(marker.colorIndex));
}
group.setCreationDate(marker.creationDate);
mapMarkersGroups.add(group);
addToGroupsList(group);
sortGroups();
return group;
}
@ -1159,7 +1207,7 @@ public class MapMarkersHelper {
}
mapMarkersGroups = new ArrayList<>(groupsMap.values());
if (noGroup != null) {
mapMarkersGroups.add(noGroup);
addToGroupsList(noGroup);
}
sortGroups();
@ -1175,7 +1223,8 @@ public class MapMarkersHelper {
MapMarkersGroup group = mapMarkersGroups.get(i);
if (group.getName() == null) {
sortMarkers(group.getMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
noGroup = mapMarkersGroups.remove(i);
removeFromGroupsList(group);
noGroup = group;
}
}
Collections.sort(mapMarkersGroups, new Comparator<MapMarkersGroup>() {
@ -1193,7 +1242,7 @@ public class MapMarkersHelper {
}
});
if (noGroup != null) {
mapMarkersGroups.add(0, noGroup);
addToGroupsList(0, noGroup);
}
}
}

View file

@ -159,6 +159,7 @@ public class MapMarkersActiveFragment extends Fragment implements OsmAndCompassL
void updateAdapter() {
if (adapter != null) {
adapter.changeMarkers();
adapter.notifyDataSetChanged();
}
}

View file

@ -185,10 +185,6 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL
int snackbarStringRes;
if (direction == ItemTouchHelper.RIGHT) {
mapActivity.getMyApplication().getMapMarkersHelper().moveMapMarkerToHistory((MapMarker) item);
MapMarkersHelper.MapMarkersGroup group = mapActivity.getMyApplication().getMapMarkersHelper().getMapMarkerGroupByName(marker.groupName);
if (group != null) {
mapActivity.getMyApplication().getMapMarkersHelper().updateGroup(group);
}
snackbarStringRes = R.string.marker_moved_to_history;
} else {
mapActivity.getMyApplication().getMapMarkersHelper().removeMarker((MapMarker) item);

View file

@ -158,7 +158,6 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
app.getMapMarkersHelper().removeMarker((MapMarker) item);
snackbarStringRes = R.string.item_removed;
}
adapter.notifyItemRemoved(pos);
snackbar = Snackbar.make(viewHolder.itemView, snackbarStringRes, Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_undo, new View.OnClickListener() {
@Override
@ -231,7 +230,6 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
Object item = adapter.getItem(pos);
if (item instanceof MapMarker) {
app.getMapMarkersHelper().restoreMarkerFromHistory((MapMarker) item, 0);
adapter.notifyItemRemoved(pos);
}
}
@ -240,7 +238,6 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
Object item = adapter.getItem(pos);
if (item instanceof MapMarker) {
app.getMapMarkersHelper().removeMarker((MapMarker) item);
adapter.notifyItemRemoved(pos);
}
}
};

View file

@ -42,6 +42,7 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
private boolean night;
public MapMarkersActiveAdapter(MapActivity mapActivity) {
setHasStableIds(true);
this.mapActivity = mapActivity;
markers = mapActivity.getMyApplication().getMapMarkersHelper().getMapMarkers();
night = !mapActivity.getMyApplication().getSettings().isLightContent();
@ -172,24 +173,16 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
final MapMarker marker = markers.get(position);
mapActivity.getMyApplication().getMapMarkersHelper().moveMapMarkerToHistory(marker);
notifyItemRemoved(position);
if (showDirectionEnabled && position < 2 && getItemCount() > 1) {
notifyItemChanged(1);
} else if (position == getItemCount()) {
notifyItemChanged(position - 1);
}
changeMarkers();
notifyDataSetChanged();
snackbar = Snackbar.make(holder.itemView, mapActivity.getString(R.string.marker_moved_to_history), Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_undo, new View.OnClickListener() {
@Override
public void onClick(View view) {
mapActivity.getMyApplication().getMapMarkersHelper().restoreMarkerFromHistory(marker, position);
notifyItemInserted(position);
if (showDirectionEnabled && position < 2 && getItemCount() > 2) {
notifyItemChanged(2);
} else if (position == getItemCount() - 1) {
notifyItemChanged(position - 1);
}
changeMarkers();
notifyDataSetChanged();
}
});
View snackBarView = snackbar.getView();
@ -218,6 +211,10 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
return markers;
}
public void changeMarkers() {
markers = mapActivity.getMyApplication().getMapMarkersHelper().getMapMarkers();
}
public void hideSnackbar() {
if (snackbar != null && snackbar.isShown()) {
snackbar.dismiss();
@ -245,23 +242,15 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
if (group != null) {
mapActivity.getMyApplication().getMapMarkersHelper().updateGroup(group);
}
notifyItemRemoved(pos);
if (showDirectionEnabled && pos < 2 && getItemCount() > 1) {
notifyItemChanged(1);
} else if (pos == getItemCount()) {
notifyItemChanged(pos - 1);
}
changeMarkers();
notifyDataSetChanged();
snackbar = Snackbar.make(holder.itemView, R.string.marker_moved_to_history, Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_undo, new View.OnClickListener() {
@Override
public void onClick(View view) {
mapActivity.getMyApplication().getMapMarkersHelper().restoreMarkerFromHistory(marker, pos);
notifyItemInserted(pos);
if (showDirectionEnabled && pos < 2 && getItemCount() > 2) {
notifyItemChanged(2);
} else if (pos == getItemCount() - 1) {
notifyItemChanged(pos - 1);
}
changeMarkers();
notifyDataSetChanged();
}
});
View snackBarView = snackbar.getView();
@ -270,6 +259,11 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
snackbar.show();
}
@Override
public long getItemId(int position) {
return getItem(position).hashCode();
}
@Override
public void onItemDismiss(RecyclerView.ViewHolder holder) {
listener.onDragOrSwipeEnded(holder);

View file

@ -84,7 +84,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
}
public void createDisplayGroups() {
items.clear();
items = new ArrayList<>();
app.getMapMarkersHelper().updateGroups();
List<MapMarkersGroup> groups = app.getMapMarkersHelper().getMapMarkersGroups();
for (int i = 0; i < groups.size(); i++) {
@ -291,8 +291,6 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
itemViewHolder.description.setVisibility(View.GONE);
}
final String markerGroupName = marker.groupName;
final MapMarkersGroup group = app.getMapMarkersHelper().getMapMarkerGroupByName(markerGroupName);
itemViewHolder.optionsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -302,42 +300,10 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
}
if (markerInHistory) {
app.getMapMarkersHelper().restoreMarkerFromHistory(marker, 0);
if (group != null) {
ShowHideHistoryButton showHideHistoryButton = group.getShowHideHistoryButton();
if (showHideHistoryButton != null) {
if (group.getHistoryMarkers().size() == 0) {
items.remove(showHideHistoryButton);
group.setShowHideHistoryButton(null);
}
}
}
} else {
app.getMapMarkersHelper().moveMapMarkerToHistory(marker);
if (group != null) {
ShowHideHistoryButton showHideHistoryButton = group.getShowHideHistoryButton();
if (showHideHistoryButton == null) {
items.remove(marker);
if (markerGroupName != null) {
showHideHistoryButton = new ShowHideHistoryButton();
showHideHistoryButton.setShowHistory(false);
showHideHistoryButton.setMarkerGroup(group);
int index = getLastDisplayItemIndexOfGroup(group);
if (index != -1) {
items.add(index + 1, showHideHistoryButton);
group.setShowHideHistoryButton(showHideHistoryButton);
}
} else {
boolean firstItemInDisplayGroup = position - 1 != -1 && getItem(position - 1) instanceof Integer;
boolean lastItemInDisplayGroup = position == getItemCount() || !(getItem(position) instanceof MapMarker);
if (firstItemInDisplayGroup && lastItemInDisplayGroup) {
items.remove(position - 1);
}
}
} else if (!showHideHistoryButton.isShowHistory()) {
items.remove(marker);
}
}
}
createDisplayGroups();
updateShowDirectionMarkers();
notifyDataSetChanged();
if (!markerInHistory) {
@ -415,7 +381,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
public void onCheckedChanged(CompoundButton compoundButton, boolean enabled) {
group.setDisabled(!enabled);
app.getMapMarkersHelper().updateGroupDisabled(group, !enabled);
populateAdapterWithGroupMarkers(group, headerViewHolder.getAdapterPosition() + 1);
createDisplayGroups();
updateShowDirectionMarkers();
notifyDataSetChanged();
if (!enabled) {
@ -454,15 +420,8 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
showHideHistoryViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<MapMarker> historyMarkers = showHideHistoryButton.getMapMarkerGroup().getHistoryMarkers();
int pos = holder.getAdapterPosition();
if (showHistory) {
showHideHistoryButton.setShowHistory(false);
items.removeAll(historyMarkers);
} else {
showHideHistoryButton.setShowHistory(true);
items.addAll(pos, historyMarkers);
}
showHideHistoryButton.setShowHistory(!showHistory);
createDisplayGroups();
notifyDataSetChanged();
}
});
@ -509,24 +468,6 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
return monthStr;
}
private int getLastDisplayItemIndexOfGroup(MapMarkersGroup group) {
List<MapMarker> markers = group.getActiveMarkers();
int index = -1;
for (MapMarker marker : markers) {
int markerIndex = items.indexOf(marker);
if (markerIndex > index) {
index = markerIndex;
}
}
if (index == -1) {
GroupHeader header = group.getGroupHeader();
if (header != null) {
index = items.indexOf(group.getGroupHeader());
}
}
return index;
}
public interface MapMarkersGroupsAdapterListener {
void onItemClick(View view);

View file

@ -43,10 +43,8 @@ public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<RecyclerView.
}
public void createHeaders() {
items.clear();
items = new ArrayList<>();
List<MapMarker> markersHistory = app.getMapMarkersHelper().getMapMarkersHistory();
int previousHeader = -1;
int monthsDisplayed = 0;
@ -151,14 +149,12 @@ public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<RecyclerView.
return;
}
app.getMapMarkersHelper().restoreMarkerFromHistory(marker, 0);
notifyItemRemoved(position);
snackbar = Snackbar.make(itemViewHolder.itemView, app.getString(R.string.marker_moved_to_active), Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_undo, new View.OnClickListener() {
@Override
public void onClick(View view) {
app.getMapMarkersHelper().moveMapMarkerToHistory(marker);
notifyDataSetChanged();
}
});
View snackBarView = snackbar.getView();