From bd8040420b5007a58f600f6743b7b022a0f7e1c3 Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Tue, 19 Sep 2017 11:57:23 +0300 Subject: [PATCH] Refactor groups --- .../map_marker_item_show_hide_history.xml | 21 +++ OsmAnd/res/values/strings.xml | 2 + .../src/net/osmand/plus/MapMarkersHelper.java | 123 ++++++-------- .../plus/mapmarkers/MapMarkersGroup.java | 45 +++++ .../mapmarkers/MapMarkersGroupsFragment.java | 1 + .../adapters/MapMarkersGroupsAdapter.java | 157 +++++++++++++----- .../MapMarkersShowHideHistoryViewHolder.java | 16 ++ 7 files changed, 247 insertions(+), 118 deletions(-) create mode 100644 OsmAnd/res/layout/map_marker_item_show_hide_history.xml create mode 100644 OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroup.java create mode 100644 OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersShowHideHistoryViewHolder.java diff --git a/OsmAnd/res/layout/map_marker_item_show_hide_history.xml b/OsmAnd/res/layout/map_marker_item_show_hide_history.xml new file mode 100644 index 0000000000..1d1f332ccf --- /dev/null +++ b/OsmAnd/res/layout/map_marker_item_show_hide_history.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 034ca5acff..ad814a51a3 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,8 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + Show passed + Hide passed Remove from Map Markers Descendingly Ascendingly diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 1830656109..d4d3d42bc4 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -17,6 +17,7 @@ import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.helpers.ColorDialogs; import net.osmand.plus.mapmarkers.MapMarkersDbHelper; +import net.osmand.plus.mapmarkers.MapMarkersGroup; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -251,7 +252,7 @@ public class MapMarkersHelper { checkAndFixActiveMarkersOrderIfNeeded(); List markersHistory = markersDbHelper.getMarkersHistory(); - sortHistoryMarkers(); + sortMarkers(markersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); mapMarkersHistory.addAll(markersHistory); if (!ctx.isApplicationInitializing()) { @@ -279,17 +280,31 @@ public class MapMarkersHelper { } } - public List getMarkersSortedByGroup() { - Map groupsMap = new LinkedHashMap<>(); - List noGroup = new ArrayList<>(); - for (MapMarker marker : mapMarkers) { + public List getMapMarkersGroups() { + List markers = new ArrayList<>(); + markers.addAll(mapMarkers); + markers.addAll(mapMarkersHistory); + + Map groupsMap = new LinkedHashMap<>(); + MapMarkersGroup noGroup = null; + for (MapMarker marker : markers) { String groupName = marker.groupName; if (groupName == null) { - noGroup.add(marker); + if (noGroup == null) { + noGroup = new MapMarkersGroup(); + noGroup.setCreationDate(marker.creationDate); + } + if (marker.history) { + noGroup.getHistoryMarkers().add(marker); + } else { + noGroup.getMapMarkers().add(marker); + } } else { - MapMarkerGroup group = groupsMap.get(groupName); + MapMarkersGroup group = groupsMap.get(groupName); if (group == null) { - group = new MapMarkerGroup(groupName, marker.creationDate); + group = new MapMarkersGroup(); + group.setName(groupName); + group.setCreationDate(marker.creationDate); groupsMap.put(groupName, group); } else { long markerCreationDate = marker.creationDate; @@ -297,26 +312,29 @@ public class MapMarkersHelper { group.setCreationDate(markerCreationDate); } } - group.getMapMarkers().add(marker); + if (marker.history) { + group.getHistoryMarkers().add(marker); + } else { + group.getMapMarkers().add(marker); + } } } - List groups = new ArrayList<>(groupsMap.values()); + List groups = new ArrayList<>(groupsMap.values()); sortGroups(groups); - List markers = new ArrayList<>(); - markers.addAll(noGroup); - for (MapMarkerGroup group : groups) { - markers.addAll(group.getMapMarkers()); + if (noGroup != null) { + sortMarkers(noGroup.getMapMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + groups.add(0, noGroup); } - return markers; + return groups; } - private void sortGroups(List groups) { - Collections.sort(groups, new Comparator() { + private void sortGroups(List groups) { + Collections.sort(groups, new Comparator() { @Override - public int compare(MapMarkerGroup group1, MapMarkerGroup group2) { - long t1 = group1.creationDate; - long t2 = group2.creationDate; + public int compare(MapMarkersGroup group1, MapMarkersGroup group2) { + long t1 = group1.getCreationDate(); + long t2 = group2.getCreationDate(); if (t1 > t2) { return -1; } else if (t1 == t2) { @@ -328,24 +346,20 @@ public class MapMarkersHelper { }); } - private void sortHistoryMarkers() { - sortMarkers(true, null); - } - - private void sortMarkers(final boolean history, final OsmandSettings.MapMarkersOrderByMode orderByMode) { + private void sortMarkers(List markers, final boolean visited, final OsmandSettings.MapMarkersOrderByMode orderByMode) { final LatLon location = ctx.getSettings().getLastKnownMapLocation(); - Collections.sort(history ? mapMarkersHistory : mapMarkers, new Comparator() { + Collections.sort(markers, new Comparator() { @Override public int compare(MapMarker mapMarker1, MapMarker mapMarker2) { - if (history || orderByMode.isDateAddedDescending() || orderByMode.isDateAddedAscending()) { - long t1 = history ? mapMarker1.visitedDate : mapMarker1.creationDate; - long t2 = history ? mapMarker2.visitedDate : mapMarker2.creationDate; + if (orderByMode.isDateAddedDescending() || orderByMode.isDateAddedAscending()) { + long t1 = visited ? mapMarker1.visitedDate : mapMarker1.creationDate; + long t2 = visited ? mapMarker2.visitedDate : mapMarker2.creationDate; if (t1 > t2) { - return history || orderByMode.isDateAddedDescending() ? -1 : 1; + return orderByMode.isDateAddedDescending() ? -1 : 1; } else if (t1 == t2) { return 0; } else { - return history || orderByMode.isDateAddedDescending() ? 1 : -1; + return orderByMode.isDateAddedDescending() ? 1 : -1; } } else if (orderByMode.isDistanceDescending() || orderByMode.isDistanceAscending()) { int d1 = (int) MapUtils.getDistance(location, mapMarker1.getLatitude(), mapMarker1.getLongitude()); @@ -367,7 +381,7 @@ public class MapMarkersHelper { } public void orderMarkers(OsmandSettings.MapMarkersOrderByMode orderByMode) { - sortMarkers(false, orderByMode); + sortMarkers(getMapMarkers(), false, orderByMode); checkAndFixActiveMarkersOrderIfNeeded(); } @@ -501,7 +515,7 @@ public class MapMarkersHelper { marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE; mapMarkersHistory.add(marker); checkAndFixActiveMarkersOrderIfNeeded(); - sortHistoryMarkers(); + sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); refresh(); } } @@ -511,7 +525,7 @@ public class MapMarkersHelper { markersDbHelper.addMarker(marker); if (marker.history) { mapMarkersHistory.add(marker); - sortHistoryMarkers(); + sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); } else { mapMarkers.add(marker); checkAndFixActiveMarkersOrderIfNeeded(); @@ -527,7 +541,7 @@ public class MapMarkersHelper { marker.history = false; mapMarkers.add(position, marker); checkAndFixActiveMarkersOrderIfNeeded(); - sortHistoryMarkers(); + sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); refresh(); } } @@ -541,7 +555,7 @@ public class MapMarkersHelper { mapMarkers.add(marker); } checkAndFixActiveMarkersOrderIfNeeded(); - sortHistoryMarkers(); + sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); refresh(); } } @@ -623,7 +637,7 @@ public class MapMarkersHelper { } mapMarkersHistory.addAll(mapMarkers); mapMarkers.clear(); - sortHistoryMarkers(); + sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); refresh(); } @@ -855,39 +869,4 @@ public class MapMarkersHelper { } GPXUtilities.writeGpxFile(fout, file, ctx); } - - public static class MapMarkerGroup { - private String name; - private List mapMarkers = new ArrayList<>(); - private long creationDate; - - public MapMarkerGroup(String name, long creationDate) { - this.name = name; - this.creationDate = creationDate; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public List getMapMarkers() { - return mapMarkers; - } - - public void setMapMarkers(List mapMarkers) { - this.mapMarkers = mapMarkers; - } - - public long getCreationDate() { - return creationDate; - } - - public void setCreationDate(long creationDate) { - this.creationDate = creationDate; - } - } } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroup.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroup.java new file mode 100644 index 0000000000..3ba9536f91 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroup.java @@ -0,0 +1,45 @@ +package net.osmand.plus.mapmarkers; + +import net.osmand.plus.MapMarkersHelper; + +import java.util.ArrayList; +import java.util.List; + +public class MapMarkersGroup { + private String name; + private List mapMarkers = new ArrayList<>(); + private List historyMarkers = new ArrayList<>(); + private long creationDate; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getMapMarkers() { + return mapMarkers; + } + + public void setMapMarkers(List mapMarkers) { + this.mapMarkers = mapMarkers; + } + + public List getHistoryMarkers() { + return historyMarkers; + } + + public void setHistoryMarkers(List historyMarkers) { + this.historyMarkers = historyMarkers; + } + + public long getCreationDate() { + return creationDate; + } + + public void setCreationDate(long creationDate) { + this.creationDate = creationDate; + } +} diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java index 6ef6cbd9fa..f689f14cc8 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java @@ -43,6 +43,7 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL void updateAdapter() { if (adapter != null) { + adapter.createDisplayGroups(); adapter.notifyDataSetChanged(); } } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersGroupsAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersGroupsAdapter.java index 3d698fbae8..408223e920 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersGroupsAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersGroupsAdapter.java @@ -11,6 +11,7 @@ import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.mapmarkers.MapMarkersGroup; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -23,6 +24,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter markersHistory = app.getMapMarkersHelper().getMarkersSortedByGroup(); + List groups = app.getMapMarkersHelper().getMapMarkersGroups(); - int previousDateHeader = -1; - String previousNameHeader = ""; - int monthsDisplayed = 0; + for (int i = 0; i < groups.size(); i++) { + MapMarkersGroup group = groups.get(i); + String markerGroupName = group.getName(); + if (markerGroupName == null) { + int previousDateHeader = -1; + int monthsDisplayed = 0; - Calendar currentDateCalendar = Calendar.getInstance(); - currentDateCalendar.setTimeInMillis(System.currentTimeMillis()); - int currentDay = currentDateCalendar.get(Calendar.DAY_OF_YEAR); - int currentMonth = currentDateCalendar.get(Calendar.MONTH); - int currentYear = currentDateCalendar.get(Calendar.YEAR); - Calendar markerCalendar = Calendar.getInstance(); - for (int i = 0; i < markersHistory.size(); i++) { - MapMarker marker = markersHistory.get(i); - markerCalendar.setTimeInMillis(marker.creationDate); - int markerDay = markerCalendar.get(Calendar.DAY_OF_YEAR); - int markerMonth = markerCalendar.get(Calendar.MONTH); - int markerYear = markerCalendar.get(Calendar.YEAR); - String markerGroupName = marker.groupName; - if (markerGroupName != null && markerGroupName.equals("")) { - markerGroupName = app.getString(R.string.shared_string_favorites); - } - if (markerGroupName == null && markerYear == currentYear) { - if (markerDay == currentDay && previousDateHeader != TODAY_HEADER) { - items.add(TODAY_HEADER); - previousDateHeader = TODAY_HEADER; - } else if (markerDay == currentDay - 1 && previousDateHeader != YESTERDAY_HEADER) { - items.add(YESTERDAY_HEADER); - previousDateHeader = YESTERDAY_HEADER; - } else if (currentDay - markerDay >= 2 && currentDay - markerDay <= 8 && previousDateHeader != LAST_SEVEN_DAYS_HEADER) { - items.add(LAST_SEVEN_DAYS_HEADER); - previousDateHeader = LAST_SEVEN_DAYS_HEADER; - } else if (currentDay - markerDay > 8 && monthsDisplayed < 3 && previousDateHeader != markerMonth) { - items.add(markerMonth); - previousDateHeader = markerMonth; - monthsDisplayed += 1; - } else if (currentMonth - markerMonth >= 4 && previousDateHeader != THIS_YEAR_HEADER) { - items.add(THIS_YEAR_HEADER); - previousDateHeader = THIS_YEAR_HEADER; + Calendar currentDateCalendar = Calendar.getInstance(); + currentDateCalendar.setTimeInMillis(System.currentTimeMillis()); + int currentDay = currentDateCalendar.get(Calendar.DAY_OF_YEAR); + int currentMonth = currentDateCalendar.get(Calendar.MONTH); + int currentYear = currentDateCalendar.get(Calendar.YEAR); + Calendar markerCalendar = Calendar.getInstance(); + List groupMarkers = group.getMapMarkers(); + for (int j = 0; j < groupMarkers.size(); j++) { + MapMarker marker = groupMarkers.get(j); + markerCalendar.setTimeInMillis(marker.creationDate); + int markerDay = markerCalendar.get(Calendar.DAY_OF_YEAR); + int markerMonth = markerCalendar.get(Calendar.MONTH); + int markerYear = markerCalendar.get(Calendar.YEAR); + if (markerYear == currentYear) { + if (markerDay == currentDay && previousDateHeader != TODAY_HEADER) { + items.add(TODAY_HEADER); + previousDateHeader = TODAY_HEADER; + } else if (markerDay == currentDay - 1 && previousDateHeader != YESTERDAY_HEADER) { + items.add(YESTERDAY_HEADER); + previousDateHeader = YESTERDAY_HEADER; + } else if (currentDay - markerDay >= 2 && currentDay - markerDay <= 8 && previousDateHeader != LAST_SEVEN_DAYS_HEADER) { + items.add(LAST_SEVEN_DAYS_HEADER); + previousDateHeader = LAST_SEVEN_DAYS_HEADER; + } else if (currentDay - markerDay > 8 && monthsDisplayed < 3 && previousDateHeader != markerMonth) { + items.add(markerMonth); + previousDateHeader = markerMonth; + monthsDisplayed += 1; + } else if (currentMonth - markerMonth >= 4 && previousDateHeader != THIS_YEAR_HEADER) { + items.add(THIS_YEAR_HEADER); + previousDateHeader = THIS_YEAR_HEADER; + } + } else if (previousDateHeader != markerYear) { + items.add(markerYear); + previousDateHeader = markerYear; + } + items.add(marker); + } + } else { + if (markerGroupName.equals("")) { + markerGroupName = app.getString(R.string.shared_string_favorites); } - } else if (markerGroupName == null && previousDateHeader != markerYear) { - items.add(markerYear); - previousDateHeader = markerYear; - } else if (markerGroupName != null && !previousNameHeader.equals(markerGroupName)) { items.add(markerGroupName); - previousNameHeader = markerGroupName; + items.addAll(group.getMapMarkers()); + if (group.getHistoryMarkers().size() > 0) { + ShowHideHistoryButton showHideHistoryButton = new ShowHideHistoryButton(); + showHideHistoryButton.setShowHistory(false); + showHideHistoryButton.setGroupName(markerGroupName); + showHideHistoryButton.setHistoryMarkers(group.getHistoryMarkers()); + items.add(showHideHistoryButton); + } } - items.add(marker); } } @@ -121,13 +135,16 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter historyMarkers; + + public boolean isShowHistory() { + return showHistory; + } + + public void setShowHistory(boolean showHistory) { + this.showHistory = showHistory; + } + + public String getGroupName() { + return groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + public List getHistoryMarkers() { + return historyMarkers; + } + + public void setHistoryMarkers(List historyMarkers) { + this.historyMarkers = historyMarkers; + } + } } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersShowHideHistoryViewHolder.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersShowHideHistoryViewHolder.java new file mode 100644 index 0000000000..9cc5be907d --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersShowHideHistoryViewHolder.java @@ -0,0 +1,16 @@ +package net.osmand.plus.mapmarkers.adapters; + +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.widget.TextView; + +import net.osmand.plus.R; + +public class MapMarkersShowHideHistoryViewHolder extends RecyclerView.ViewHolder { + final TextView title; + + public MapMarkersShowHideHistoryViewHolder(View itemView) { + super(itemView); + title = (TextView) itemView.findViewById(R.id.show_hide_history_title); + } +}