Merge remote-tracking branch 'origin/sasha_pasha_branch' into sasha_pasha_branch

This commit is contained in:
Alexander Sytnyk 2017-09-19 13:40:28 +03:00
commit b2b9204d4e
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). 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 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="remove_from_map_markers">Remove from Map Markers</string>
<string name="descendingly">Descendingly</string> <string name="descendingly">Descendingly</string>
<string name="ascendingly">Ascendingly</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.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.helpers.ColorDialogs; import net.osmand.plus.helpers.ColorDialogs;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
@ -255,7 +256,7 @@ public class MapMarkersHelper {
checkAndFixActiveMarkersOrderIfNeeded(); checkAndFixActiveMarkersOrderIfNeeded();
List<MapMarker> markersHistory = markersDbHelper.getMarkersHistory(); List<MapMarker> markersHistory = markersDbHelper.getMarkersHistory();
sortHistoryMarkers(); sortMarkers(markersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
mapMarkersHistory.addAll(markersHistory); mapMarkersHistory.addAll(markersHistory);
if (!ctx.isApplicationInitializing()) { if (!ctx.isApplicationInitializing()) {
@ -283,17 +284,31 @@ public class MapMarkersHelper {
} }
} }
public List<MapMarker> getMarkersSortedByGroup() { public List<MapMarkersGroup> getMapMarkersGroups() {
Map<String, MapMarkerGroup> groupsMap = new LinkedHashMap<>(); List<MapMarker> markers = new ArrayList<>();
List<MapMarker> noGroup = new ArrayList<>(); markers.addAll(mapMarkers);
for (MapMarker marker : mapMarkers) { markers.addAll(mapMarkersHistory);
Map<String, MapMarkersGroup> groupsMap = new LinkedHashMap<>();
MapMarkersGroup noGroup = null;
for (MapMarker marker : markers) {
String groupName = marker.groupName; String groupName = marker.groupName;
if (groupName == null) { if (groupName == null) {
noGroup.add(marker); if (noGroup == null) {
noGroup = new MapMarkersGroup();
noGroup.setCreationDate(marker.creationDate);
}
if (marker.history) {
noGroup.getHistoryMarkers().add(marker);
} else { } else {
MapMarkerGroup group = groupsMap.get(groupName); noGroup.getMapMarkers().add(marker);
}
} else {
MapMarkersGroup group = groupsMap.get(groupName);
if (group == null) { if (group == null) {
group = new MapMarkerGroup(groupName, marker.creationDate); group = new MapMarkersGroup();
group.setName(groupName);
group.setCreationDate(marker.creationDate);
groupsMap.put(groupName, group); groupsMap.put(groupName, group);
} else { } else {
long markerCreationDate = marker.creationDate; long markerCreationDate = marker.creationDate;
@ -301,26 +316,29 @@ public class MapMarkersHelper {
group.setCreationDate(markerCreationDate); group.setCreationDate(markerCreationDate);
} }
} }
if (marker.history) {
group.getHistoryMarkers().add(marker);
} else {
group.getMapMarkers().add(marker); group.getMapMarkers().add(marker);
} }
} }
List<MapMarkerGroup> groups = new ArrayList<>(groupsMap.values()); }
List<MapMarkersGroup> groups = new ArrayList<>(groupsMap.values());
sortGroups(groups); sortGroups(groups);
List<MapMarker> markers = new ArrayList<>(); if (noGroup != null) {
markers.addAll(noGroup); sortMarkers(noGroup.getMapMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
for (MapMarkerGroup group : groups) { groups.add(0, noGroup);
markers.addAll(group.getMapMarkers());
} }
return markers; return groups;
} }
private void sortGroups(List<MapMarkerGroup> groups) { private void sortGroups(List<MapMarkersGroup> groups) {
Collections.sort(groups, new Comparator<MapMarkerGroup>() { Collections.sort(groups, new Comparator<MapMarkersGroup>() {
@Override @Override
public int compare(MapMarkerGroup group1, MapMarkerGroup group2) { public int compare(MapMarkersGroup group1, MapMarkersGroup group2) {
long t1 = group1.creationDate; long t1 = group1.getCreationDate();
long t2 = group2.creationDate; long t2 = group2.getCreationDate();
if (t1 > t2) { if (t1 > t2) {
return -1; return -1;
} else if (t1 == t2) { } else if (t1 == t2) {
@ -332,24 +350,20 @@ public class MapMarkersHelper {
}); });
} }
private void sortHistoryMarkers() { private void sortMarkers(List<MapMarker> markers, final boolean visited, final OsmandSettings.MapMarkersOrderByMode orderByMode) {
sortMarkers(true, null);
}
private void sortMarkers(final boolean history, final OsmandSettings.MapMarkersOrderByMode orderByMode) {
final LatLon location = ctx.getSettings().getLastKnownMapLocation(); final LatLon location = ctx.getSettings().getLastKnownMapLocation();
Collections.sort(history ? mapMarkersHistory : mapMarkers, new Comparator<MapMarker>() { Collections.sort(markers, new Comparator<MapMarker>() {
@Override @Override
public int compare(MapMarker mapMarker1, MapMarker mapMarker2) { public int compare(MapMarker mapMarker1, MapMarker mapMarker2) {
if (history || orderByMode.isDateAddedDescending() || orderByMode.isDateAddedAscending()) { if (orderByMode.isDateAddedDescending() || orderByMode.isDateAddedAscending()) {
long t1 = history ? mapMarker1.visitedDate : mapMarker1.creationDate; long t1 = visited ? mapMarker1.visitedDate : mapMarker1.creationDate;
long t2 = history ? mapMarker2.visitedDate : mapMarker2.creationDate; long t2 = visited ? mapMarker2.visitedDate : mapMarker2.creationDate;
if (t1 > t2) { if (t1 > t2) {
return history || orderByMode.isDateAddedDescending() ? -1 : 1; return orderByMode.isDateAddedDescending() ? -1 : 1;
} else if (t1 == t2) { } else if (t1 == t2) {
return 0; return 0;
} else { } else {
return history || orderByMode.isDateAddedDescending() ? 1 : -1; return orderByMode.isDateAddedDescending() ? 1 : -1;
} }
} else if (orderByMode.isDistanceDescending() || orderByMode.isDistanceAscending()) { } else if (orderByMode.isDistanceDescending() || orderByMode.isDistanceAscending()) {
int d1 = (int) MapUtils.getDistance(location, mapMarker1.getLatitude(), mapMarker1.getLongitude()); int d1 = (int) MapUtils.getDistance(location, mapMarker1.getLatitude(), mapMarker1.getLongitude());
@ -371,7 +385,7 @@ public class MapMarkersHelper {
} }
public void orderMarkers(OsmandSettings.MapMarkersOrderByMode orderByMode) { public void orderMarkers(OsmandSettings.MapMarkersOrderByMode orderByMode) {
sortMarkers(false, orderByMode); sortMarkers(getMapMarkers(), false, orderByMode);
checkAndFixActiveMarkersOrderIfNeeded(); checkAndFixActiveMarkersOrderIfNeeded();
} }
@ -509,7 +523,7 @@ public class MapMarkersHelper {
marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE; marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
mapMarkersHistory.add(marker); mapMarkersHistory.add(marker);
checkAndFixActiveMarkersOrderIfNeeded(); checkAndFixActiveMarkersOrderIfNeeded();
sortHistoryMarkers(); sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
refresh(); refresh();
} }
} }
@ -519,7 +533,7 @@ public class MapMarkersHelper {
markersDbHelper.addMarker(marker); markersDbHelper.addMarker(marker);
if (marker.history) { if (marker.history) {
mapMarkersHistory.add(marker); mapMarkersHistory.add(marker);
sortHistoryMarkers(); sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
} else { } else {
mapMarkers.add(marker); mapMarkers.add(marker);
checkAndFixActiveMarkersOrderIfNeeded(); checkAndFixActiveMarkersOrderIfNeeded();
@ -535,7 +549,7 @@ public class MapMarkersHelper {
marker.history = false; marker.history = false;
mapMarkers.add(position, marker); mapMarkers.add(position, marker);
checkAndFixActiveMarkersOrderIfNeeded(); checkAndFixActiveMarkersOrderIfNeeded();
sortHistoryMarkers(); sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
refresh(); refresh();
} }
} }
@ -549,7 +563,7 @@ public class MapMarkersHelper {
mapMarkers.add(marker); mapMarkers.add(marker);
} }
checkAndFixActiveMarkersOrderIfNeeded(); checkAndFixActiveMarkersOrderIfNeeded();
sortHistoryMarkers(); sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
refresh(); refresh();
} }
} }
@ -631,7 +645,7 @@ public class MapMarkersHelper {
} }
mapMarkersHistory.addAll(mapMarkers); mapMarkersHistory.addAll(mapMarkers);
mapMarkers.clear(); mapMarkers.clear();
sortHistoryMarkers(); sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
refresh(); refresh();
} }
@ -863,39 +877,4 @@ public class MapMarkersHelper {
} }
GPXUtilities.writeGpxFile(fout, file, ctx); 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() { void updateAdapter() {
if (adapter != null) { if (adapter != null) {
adapter.createDisplayGroups();
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
} }
} }

View file

@ -11,6 +11,7 @@ import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; 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 HEADER_TYPE = 1;
private static final int MARKER_TYPE = 2; 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 TODAY_HEADER = 56;
private static final int YESTERDAY_HEADER = 57; private static final int YESTERDAY_HEADER = 57;
@ -46,10 +48,13 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
public void createDisplayGroups() { public void createDisplayGroups() {
items.clear(); items.clear();
List<MapMarker> markersHistory = app.getMapMarkersHelper().getMarkersSortedByGroup(); List<MapMarkersGroup> groups = app.getMapMarkersHelper().getMapMarkersGroups();
for (int i = 0; i < groups.size(); i++) {
MapMarkersGroup group = groups.get(i);
String markerGroupName = group.getName();
if (markerGroupName == null) {
int previousDateHeader = -1; int previousDateHeader = -1;
String previousNameHeader = "";
int monthsDisplayed = 0; int monthsDisplayed = 0;
Calendar currentDateCalendar = Calendar.getInstance(); Calendar currentDateCalendar = Calendar.getInstance();
@ -58,17 +63,14 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
int currentMonth = currentDateCalendar.get(Calendar.MONTH); int currentMonth = currentDateCalendar.get(Calendar.MONTH);
int currentYear = currentDateCalendar.get(Calendar.YEAR); int currentYear = currentDateCalendar.get(Calendar.YEAR);
Calendar markerCalendar = Calendar.getInstance(); Calendar markerCalendar = Calendar.getInstance();
for (int i = 0; i < markersHistory.size(); i++) { List<MapMarker> groupMarkers = group.getMapMarkers();
MapMarker marker = markersHistory.get(i); for (int j = 0; j < groupMarkers.size(); j++) {
MapMarker marker = groupMarkers.get(j);
markerCalendar.setTimeInMillis(marker.creationDate); markerCalendar.setTimeInMillis(marker.creationDate);
int markerDay = markerCalendar.get(Calendar.DAY_OF_YEAR); int markerDay = markerCalendar.get(Calendar.DAY_OF_YEAR);
int markerMonth = markerCalendar.get(Calendar.MONTH); int markerMonth = markerCalendar.get(Calendar.MONTH);
int markerYear = markerCalendar.get(Calendar.YEAR); int markerYear = markerCalendar.get(Calendar.YEAR);
String markerGroupName = marker.groupName; if (markerYear == currentYear) {
if (markerGroupName != null && markerGroupName.equals("")) {
markerGroupName = app.getString(R.string.shared_string_favorites);
}
if (markerGroupName == null && markerYear == currentYear) {
if (markerDay == currentDay && previousDateHeader != TODAY_HEADER) { if (markerDay == currentDay && previousDateHeader != TODAY_HEADER) {
items.add(TODAY_HEADER); items.add(TODAY_HEADER);
previousDateHeader = TODAY_HEADER; previousDateHeader = TODAY_HEADER;
@ -86,15 +88,27 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
items.add(THIS_YEAR_HEADER); items.add(THIS_YEAR_HEADER);
previousDateHeader = THIS_YEAR_HEADER; previousDateHeader = THIS_YEAR_HEADER;
} }
} else if (markerGroupName == null && previousDateHeader != markerYear) { } else if (previousDateHeader != markerYear) {
items.add(markerYear); items.add(markerYear);
previousDateHeader = markerYear; previousDateHeader = markerYear;
} else if (markerGroupName != null && !previousNameHeader.equals(markerGroupName)) {
items.add(markerGroupName);
previousNameHeader = markerGroupName;
} }
items.add(marker); items.add(marker);
} }
} else {
if (markerGroupName.equals("")) {
markerGroupName = app.getString(R.string.shared_string_favorites);
}
items.add(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);
}
}
}
} }
public void setLocation(LatLon location) { public void setLocation(LatLon location) {
@ -121,13 +135,16 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
} else if (viewType == HEADER_TYPE) { } else if (viewType == HEADER_TYPE) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_marker_item_header, viewGroup, false); View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_marker_item_header, viewGroup, false);
return new MapMarkerHeaderViewHolder(view); 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 { } else {
throw new IllegalArgumentException("Unsupported view type"); throw new IllegalArgumentException("Unsupported view type");
} }
} }
@Override @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
IconsCache iconsCache = app.getIconsCache(); IconsCache iconsCache = app.getIconsCache();
if (holder instanceof MapMarkerItemViewHolder) { if (holder instanceof MapMarkerItemViewHolder) {
final MapMarkerItemViewHolder itemViewHolder = (MapMarkerItemViewHolder) holder; final MapMarkerItemViewHolder itemViewHolder = (MapMarkerItemViewHolder) holder;
@ -165,7 +182,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
itemViewHolder.divider.setVisibility(View.VISIBLE); itemViewHolder.divider.setVisibility(View.VISIBLE);
} }
} else if (holder instanceof MapMarkerHeaderViewHolder) { } else if (holder instanceof MapMarkerHeaderViewHolder) {
final MapMarkerHeaderViewHolder dateViewHolder = (MapMarkerHeaderViewHolder) holder; final MapMarkerHeaderViewHolder headerViewHolder = (MapMarkerHeaderViewHolder) holder;
final Object header = getItem(position); final Object header = getItem(position);
String headerString; String headerString;
if (header instanceof Integer) { if (header instanceof Integer) {
@ -188,7 +205,23 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
} else { } else {
throw new IllegalArgumentException("Unsupported header"); 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; return MARKER_TYPE;
} else if (item instanceof String || item instanceof Integer) { } else if (item instanceof String || item instanceof Integer) {
return HEADER_TYPE; return HEADER_TYPE;
} else if (item instanceof ShowHideHistoryButton) {
return SHOW_HIDE_HISTORY_TYPE;
} else { } else {
throw new IllegalArgumentException("Unsupported view type"); throw new IllegalArgumentException("Unsupported view type");
} }
@ -219,4 +254,34 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
date.setMonth(month); date.setMonth(month);
return dateFormat.format(date); 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);
}
}