Refactor groups

This commit is contained in:
PavelRatushny 2017-09-19 11:57:23 +03:00
parent e8792e0e39
commit bd8040420b
7 changed files with 247 additions and 118 deletions

View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:background="?attr/bg_color"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/show_hide_history_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:paddingLeft="56dp"
android:paddingStart="56dp"
android:textAllCaps="true"
android:gravity="center_vertical"
tools:text="Show passed"/>
</LinearLayout>

View file

@ -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
-->
<string name="show_passed">Show passed</string>
<string name="hide_passed">Hide passed</string>
<string name="remove_from_map_markers">Remove from Map Markers</string>
<string name="descendingly">Descendingly</string>
<string name="ascendingly">Ascendingly</string>

View file

@ -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<MapMarker> 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<MapMarker> getMarkersSortedByGroup() {
Map<String, MapMarkerGroup> groupsMap = new LinkedHashMap<>();
List<MapMarker> noGroup = new ArrayList<>();
for (MapMarker marker : mapMarkers) {
public List<MapMarkersGroup> getMapMarkersGroups() {
List<MapMarker> markers = new ArrayList<>();
markers.addAll(mapMarkers);
markers.addAll(mapMarkersHistory);
Map<String, MapMarkersGroup> 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<MapMarkerGroup> groups = new ArrayList<>(groupsMap.values());
List<MapMarkersGroup> groups = new ArrayList<>(groupsMap.values());
sortGroups(groups);
List<MapMarker> 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<MapMarkerGroup> groups) {
Collections.sort(groups, new Comparator<MapMarkerGroup>() {
private void sortGroups(List<MapMarkersGroup> groups) {
Collections.sort(groups, new Comparator<MapMarkersGroup>() {
@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<MapMarker> markers, final boolean visited, final OsmandSettings.MapMarkersOrderByMode orderByMode) {
final LatLon location = ctx.getSettings().getLastKnownMapLocation();
Collections.sort(history ? mapMarkersHistory : mapMarkers, new Comparator<MapMarker>() {
Collections.sort(markers, new Comparator<MapMarker>() {
@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<MapMarker> 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<MapMarker> getMapMarkers() {
return mapMarkers;
}
public void setMapMarkers(List<MapMarker> mapMarkers) {
this.mapMarkers = mapMarkers;
}
public long getCreationDate() {
return creationDate;
}
public void setCreationDate(long creationDate) {
this.creationDate = creationDate;
}
}
}

View file

@ -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<MapMarkersHelper.MapMarker> mapMarkers = new ArrayList<>();
private List<MapMarkersHelper.MapMarker> historyMarkers = new ArrayList<>();
private long creationDate;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<MapMarkersHelper.MapMarker> getMapMarkers() {
return mapMarkers;
}
public void setMapMarkers(List<MapMarkersHelper.MapMarker> mapMarkers) {
this.mapMarkers = mapMarkers;
}
public List<MapMarkersHelper.MapMarker> getHistoryMarkers() {
return historyMarkers;
}
public void setHistoryMarkers(List<MapMarkersHelper.MapMarker> historyMarkers) {
this.historyMarkers = historyMarkers;
}
public long getCreationDate() {
return creationDate;
}
public void setCreationDate(long creationDate) {
this.creationDate = creationDate;
}
}

View file

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

View file

@ -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<RecyclerView.V
private static final int HEADER_TYPE = 1;
private static final int MARKER_TYPE = 2;
private static final int SHOW_HIDE_HISTORY_TYPE = 3;
private static final int TODAY_HEADER = 56;
private static final int YESTERDAY_HEADER = 57;
@ -46,54 +48,66 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
public void createDisplayGroups() {
items.clear();
List<MapMarker> markersHistory = app.getMapMarkersHelper().getMarkersSortedByGroup();
List<MapMarkersGroup> 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<MapMarker> 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<RecyclerView.V
} else if (viewType == HEADER_TYPE) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_marker_item_header, viewGroup, false);
return new MapMarkerHeaderViewHolder(view);
} else if (viewType == SHOW_HIDE_HISTORY_TYPE) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_marker_item_show_hide_history, viewGroup, false);
return new MapMarkersShowHideHistoryViewHolder(view);
} else {
throw new IllegalArgumentException("Unsupported view type");
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
IconsCache iconsCache = app.getIconsCache();
if (holder instanceof MapMarkerItemViewHolder) {
final MapMarkerItemViewHolder itemViewHolder = (MapMarkerItemViewHolder) holder;
@ -165,7 +182,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
itemViewHolder.divider.setVisibility(View.VISIBLE);
}
} else if (holder instanceof MapMarkerHeaderViewHolder) {
final MapMarkerHeaderViewHolder dateViewHolder = (MapMarkerHeaderViewHolder) holder;
final MapMarkerHeaderViewHolder headerViewHolder = (MapMarkerHeaderViewHolder) holder;
final Object header = getItem(position);
String headerString;
if (header instanceof Integer) {
@ -188,7 +205,23 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
} else {
throw new IllegalArgumentException("Unsupported header");
}
dateViewHolder.date.setText(headerString);
headerViewHolder.date.setText(headerString);
} else if (holder instanceof MapMarkersShowHideHistoryViewHolder) {
final MapMarkersShowHideHistoryViewHolder showHideHistoryViewHolder = (MapMarkersShowHideHistoryViewHolder) holder;
final ShowHideHistoryButton showHideHistoryButton = (ShowHideHistoryButton) getItem(position);
final boolean showHistory = showHideHistoryButton.isShowHistory();
showHideHistoryViewHolder.title.setText(app.getString(showHistory ? R.string.hide_passed : R.string.show_passed));
// showHideHistoryViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// if (showHistory) {
// items.removeAll(showHideHistoryButton.getHistoryMarkers());
// showHideHistoryButton.setShowHistory(false);
// } else {
// items.addAll(holder.getAdapterPosition(), showHideHistoryButton.getHistoryMarkers());
// }
// }
// });
}
}
@ -199,6 +232,8 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
return MARKER_TYPE;
} else if (item instanceof String || item instanceof Integer) {
return HEADER_TYPE;
} else if (item instanceof ShowHideHistoryButton) {
return SHOW_HIDE_HISTORY_TYPE;
} else {
throw new IllegalArgumentException("Unsupported view type");
}
@ -219,4 +254,34 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
date.setMonth(month);
return dateFormat.format(date);
}
private static class ShowHideHistoryButton {
private boolean showHistory;
private String groupName;
private List<MapMarker> 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<MapMarker> getHistoryMarkers() {
return historyMarkers;
}
public void setHistoryMarkers(List<MapMarker> historyMarkers) {
this.historyMarkers = historyMarkers;
}
}
}

View file

@ -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);
}
}