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

This commit is contained in:
Alexander Sytnyk 2017-09-19 19:01:22 +03:00
commit 4b3318d197
6 changed files with 307 additions and 200 deletions

View file

@ -14,13 +14,25 @@
android:orientation="horizontal"
android:background="?attr/bg_color">
<ImageView
android:id="@+id/icon"
android:scaleType="centerInside"
android:layout_width="56dp"
android:layout_height="48dp"
tools:src="@drawable/ic_action_fav_dark"
tools:visibility="visible"
android:visibility="gone"/>
<View
android:id="@+id/icon_space"
android:layout_width="16dp"
android:layout_height="match_parent"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/date_title"
android:id="@+id/title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:gravity="center_vertical"
tools:text="Today"/>

View file

@ -17,7 +17,6 @@ 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;
@ -176,6 +175,10 @@ public class MapMarkersHelper {
}
}
public MarkersSyncGroup getGroup(String id) {
return markersDbHelper.getGroup(id);
}
public static class MarkersSyncGroup {
public static final int FAVORITES_TYPE = 0;
@ -284,73 +287,7 @@ public class MapMarkersHelper {
}
}
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) {
if (noGroup == null) {
noGroup = new MapMarkersGroup();
noGroup.setCreationDate(marker.creationDate);
}
if (marker.history) {
noGroup.getHistoryMarkers().add(marker);
} else {
noGroup.getMapMarkers().add(marker);
}
} else {
MapMarkersGroup group = groupsMap.get(groupName);
if (group == null) {
group = new MapMarkersGroup();
group.setName(groupName);
group.setCreationDate(marker.creationDate);
groupsMap.put(groupName, group);
} else {
long markerCreationDate = marker.creationDate;
if (markerCreationDate < group.getCreationDate()) {
group.setCreationDate(markerCreationDate);
}
}
if (marker.history) {
group.getHistoryMarkers().add(marker);
} else {
group.getMapMarkers().add(marker);
}
}
}
List<MapMarkersGroup> groups = new ArrayList<>(groupsMap.values());
sortGroups(groups);
if (noGroup != null) {
sortMarkers(noGroup.getMapMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
groups.add(0, noGroup);
}
return groups;
}
private void sortGroups(List<MapMarkersGroup> groups) {
Collections.sort(groups, new Comparator<MapMarkersGroup>() {
@Override
public int compare(MapMarkersGroup group1, MapMarkersGroup group2) {
long t1 = group1.getCreationDate();
long t2 = group2.getCreationDate();
if (t1 > t2) {
return -1;
} else if (t1 == t2) {
return 0;
} else {
return 1;
}
}
});
}
private void sortMarkers(List<MapMarker> markers, final boolean visited, final OsmandSettings.MapMarkersOrderByMode orderByMode) {
public void sortMarkers(List<MapMarker> markers, final boolean visited, final OsmandSettings.MapMarkersOrderByMode orderByMode) {
final LatLon location = ctx.getSettings().getLastKnownMapLocation();
Collections.sort(markers, new Comparator<MapMarker>() {
@Override

View file

@ -1,75 +0,0 @@
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;
private ShowHideHistoryButton showHideHistoryButton;
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;
}
public ShowHideHistoryButton getShowHideHistoryButton() {
return showHideHistoryButton;
}
public void setShowHideHistoryButton(ShowHideHistoryButton showHideHistoryButton) {
this.showHideHistoryButton = showHideHistoryButton;
}
public static class ShowHideHistoryButton {
private boolean showHistory;
private MapMarkersGroup group;
public boolean isShowHistory() {
return showHistory;
}
public void setShowHistory(boolean showHistory) {
this.showHistory = showHistory;
}
public MapMarkersGroup getGroup() {
return group;
}
public void setGroup(MapMarkersGroup group) {
this.group = group;
}
}
}

View file

@ -3,18 +3,23 @@ package net.osmand.plus.mapmarkers.adapters;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.plus.R;
public class MapMarkerHeaderViewHolder extends RecyclerView.ViewHolder {
final TextView date;
final ImageView icon;
final View iconSpace;
final TextView title;
final ImageButton optionsBtn;
public MapMarkerHeaderViewHolder(View itemView) {
super(itemView);
date = itemView.findViewById(R.id.date_title);
optionsBtn = itemView.findViewById(R.id.date_options_button);
icon = (ImageView) itemView.findViewById(R.id.icon);
iconSpace = itemView.findViewById(R.id.icon_space);
title = (TextView) itemView.findViewById(R.id.title);
optionsBtn = (ImageButton) itemView.findViewById(R.id.date_options_button);
}
}

View file

@ -7,18 +7,24 @@ import android.view.ViewGroup;
import net.osmand.data.LatLon;
import net.osmand.plus.IconsCache;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.dashboard.DashLocationFragment;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@ -31,6 +37,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
private static final int LAST_SEVEN_DAYS_HEADER = 58;
private static final int THIS_YEAR_HEADER = 59;
private MapActivity mapActivity;
private OsmandApplication app;
private List<Object> items = new ArrayList<>();
private List<MapMarkersGroup> groups;
@ -41,16 +48,15 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
private boolean useCenter;
public MapMarkersGroupsAdapter(MapActivity mapActivity) {
this.app = mapActivity.getMyApplication();
this.mapActivity = mapActivity;
app = mapActivity.getMyApplication();
night = !mapActivity.getMyApplication().getSettings().isLightContent();
createDisplayGroups();
}
public void createDisplayGroups() {
items.clear();
groups = app.getMapMarkersHelper().getMapMarkersGroups();
groups = getMapMarkersGroups();
for (int i = 0; i < groups.size(); i++) {
MapMarkersGroup group = groups.get(i);
String markerGroupName = group.getName();
@ -64,7 +70,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
int currentMonth = currentDateCalendar.get(Calendar.MONTH);
int currentYear = currentDateCalendar.get(Calendar.YEAR);
Calendar markerCalendar = Calendar.getInstance();
List<MapMarker> groupMarkers = group.getMapMarkers();
List<MapMarker> groupMarkers = group.getActiveMarkers();
for (int j = 0; j < groupMarkers.size(); j++) {
MapMarker marker = groupMarkers.get(j);
markerCalendar.setTimeInMillis(marker.creationDate);
@ -99,10 +105,16 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
if (markerGroupName.equals("")) {
markerGroupName = app.getString(R.string.shared_string_favorites);
}
items.add(markerGroupName);
items.addAll(group.getMapMarkers());
GroupHeader header = new GroupHeader();
header.setGroupName(markerGroupName);
header.setIconRes(group.getType() == MapMarkersHelper.MarkersSyncGroup.FAVORITES_TYPE ? R.drawable.ic_action_fav_dark : R.drawable.ic_action_track_16);
header.setActiveMarkersCount(group.getActiveMarkers().size());
header.setHistoryMarkersCount(group.getHistoryMarkers().size());
group.setGroupHeader(header);
items.add(header);
items.addAll(group.getActiveMarkers());
if (group.getHistoryMarkers().size() > 0) {
MapMarkersGroup.ShowHideHistoryButton showHideHistoryButton = new MapMarkersGroup.ShowHideHistoryButton();
ShowHideHistoryButton showHideHistoryButton = new ShowHideHistoryButton();
showHideHistoryButton.setShowHistory(false);
showHideHistoryButton.setGroup(group);
group.setShowHideHistoryButton(showHideHistoryButton);
@ -112,6 +124,73 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
}
}
public List<MapMarkersGroup> getMapMarkersGroups() {
List<MapMarker> markers = new ArrayList<>();
markers.addAll(app.getMapMarkersHelper().getMapMarkers());
markers.addAll(app.getMapMarkersHelper().getMapMarkersHistory());
Map<String, MapMarkersGroup> groupsMap = new LinkedHashMap<>();
MapMarkersGroup noGroup = null;
for (MapMarker marker : markers) {
String groupName = marker.groupName;
if (groupName == null) {
if (noGroup == null) {
noGroup = new MapMarkersGroup();
noGroup.setCreationDate(marker.creationDate);
}
if (marker.history) {
noGroup.getHistoryMarkers().add(marker);
} else {
noGroup.getActiveMarkers().add(marker);
}
} else {
MapMarkersGroup group = groupsMap.get(groupName);
if (group == null) {
group = new MapMarkersGroup();
group.setName(groupName);
group.setType(app.getMapMarkersHelper().getGroup(marker.groupKey).getType());
group.setCreationDate(marker.creationDate);
groupsMap.put(groupName, group);
} else {
long markerCreationDate = marker.creationDate;
if (markerCreationDate < group.getCreationDate()) {
group.setCreationDate(markerCreationDate);
}
}
if (marker.history) {
group.getHistoryMarkers().add(marker);
} else {
group.getActiveMarkers().add(marker);
}
}
}
List<MapMarkersGroup> groups = new ArrayList<>(groupsMap.values());
sortGroups(groups);
if (noGroup != null) {
app.getMapMarkersHelper().sortMarkers(noGroup.getActiveMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
groups.add(0, noGroup);
}
return groups;
}
private void sortGroups(List<MapMarkersGroup> groups) {
Collections.sort(groups, new Comparator<MapMarkersGroup>() {
@Override
public int compare(MapMarkersGroup group1, MapMarkersGroup group2) {
long t1 = group1.getCreationDate();
long t2 = group2.getCreationDate();
if (t1 > t2) {
return -1;
} else if (t1 == t2) {
return 0;
} else {
return 1;
}
}
});
}
public void setLocation(LatLon location) {
this.location = location;
}
@ -150,15 +229,22 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
if (holder instanceof MapMarkerItemViewHolder) {
final MapMarkerItemViewHolder itemViewHolder = (MapMarkerItemViewHolder) holder;
final MapMarker marker = (MapMarker) getItem(position);
final boolean markerInHistory = marker.history;
int color = MapMarker.getColorId(marker.colorIndex);
itemViewHolder.icon.setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_flag_dark, color));
itemViewHolder.title.setText(marker.getName(app));
if (marker.history) {
itemViewHolder.point.setVisibility(View.VISIBLE);
itemViewHolder.description.setVisibility(View.VISIBLE);
itemViewHolder.description.setText(app.getString(R.string.passed, new SimpleDateFormat("MMM dd", Locale.getDefault()).format(new Date(marker.visitedDate))));
} else {
itemViewHolder.point.setVisibility(View.GONE);
itemViewHolder.description.setVisibility(View.GONE);
}
final boolean markerInHistory = marker.history;
itemViewHolder.optionsBtn.setImageDrawable(iconsCache.getThemedIcon(markerInHistory ? R.drawable.ic_action_reset_to_default_dark : R.drawable.ic_action_marker_passed));
itemViewHolder.optionsBtn.setOnClickListener(new View.OnClickListener() {
@Override
@ -167,13 +253,14 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
if (position < 0) {
return;
}
String markerGroupName = marker.groupName;
boolean markerHasGroup = markerGroupName != null;
MapMarkersGroup group = markerHasGroup ? getMapMarkerGroupByName(markerGroupName) : null;
if (markerInHistory) {
app.getMapMarkersHelper().restoreMarkerFromHistory(marker, 0);
if (marker.groupName != null) {
MapMarkersGroup group = getMapMarkerGroupByName(marker.groupName);
if (group != null) {
restoreMarkerFromHistoryInGroup(group, marker);
MapMarkersGroup.ShowHideHistoryButton showHideHistoryButton = group.getShowHideHistoryButton();
ShowHideHistoryButton showHideHistoryButton = group.getShowHideHistoryButton();
if (showHideHistoryButton != null) {
if (group.getHistoryMarkers().size() == 0) {
items.remove(showHideHistoryButton);
@ -181,17 +268,14 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
}
}
}
}
} else {
app.getMapMarkersHelper().moveMapMarkerToHistory(marker);
if (marker.groupName != null) {
MapMarkersGroup group = getMapMarkerGroupByName(marker.groupName);
if (group != null) {
moveMarkerToHistoryInGroup(group, marker);
MapMarkersGroup.ShowHideHistoryButton showHideHistoryButton = group.getShowHideHistoryButton();
ShowHideHistoryButton showHideHistoryButton = group.getShowHideHistoryButton();
if (showHideHistoryButton == null) {
items.remove(marker);
showHideHistoryButton = new MapMarkersGroup.ShowHideHistoryButton();
showHideHistoryButton = new ShowHideHistoryButton();
showHideHistoryButton.setShowHistory(false);
showHideHistoryButton.setGroup(group);
int index = getLastDisplayItemIndexOfGroup(group);
@ -204,15 +288,18 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
}
}
}
if (group != null) {
GroupHeader header = group.getGroupHeader();
if (header != null) {
header.setActiveMarkersCount(group.getActiveMarkers().size());
header.setHistoryMarkersCount(group.getHistoryMarkers().size());
}
}
notifyDataSetChanged();
}
});
itemViewHolder.iconReorder.setVisibility(View.GONE);
itemViewHolder.flagIconLeftSpace.setVisibility(View.VISIBLE);
itemViewHolder.iconDirection.setVisibility(View.GONE);
itemViewHolder.leftPointSpace.setVisibility(View.GONE);
itemViewHolder.rightPointSpace.setVisibility(View.GONE);
if (position == getItemCount() - 1) {
itemViewHolder.bottomShadow.setVisibility(View.VISIBLE);
itemViewHolder.divider.setVisibility(View.GONE);
@ -220,11 +307,19 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
itemViewHolder.bottomShadow.setVisibility(View.GONE);
itemViewHolder.divider.setVisibility(View.VISIBLE);
}
LatLon markerLatLon = new LatLon(marker.getLatitude(), marker.getLongitude());
DashLocationFragment.updateLocationView(useCenter, location,
heading, itemViewHolder.iconDirection, R.drawable.ic_direction_arrow, 0,
itemViewHolder.distance, markerLatLon,
screenOrientation, app, mapActivity, true);
} else if (holder instanceof MapMarkerHeaderViewHolder) {
final MapMarkerHeaderViewHolder headerViewHolder = (MapMarkerHeaderViewHolder) holder;
final Object header = getItem(position);
String headerString;
if (header instanceof Integer) {
headerViewHolder.icon.setVisibility(View.GONE);
headerViewHolder.iconSpace.setVisibility(View.VISIBLE);
Integer dateHeader = (Integer) header;
if (dateHeader == TODAY_HEADER) {
headerString = app.getString(R.string.today);
@ -239,15 +334,22 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
} else {
headerString = String.valueOf(dateHeader);
}
} else if (header instanceof String) {
headerString = (String) header;
} else if (header instanceof GroupHeader) {
GroupHeader groupHeader = (GroupHeader) header;
headerString = groupHeader.getGroupName().concat(" - ")
.concat(Integer.toString(groupHeader.getActiveMarkersCount()))
.concat("/")
.concat(Integer.toString(groupHeader.getActiveMarkersCount() + groupHeader.getHistoryMarkersCount()));
headerViewHolder.icon.setVisibility(View.VISIBLE);
headerViewHolder.iconSpace.setVisibility(View.GONE);
headerViewHolder.icon.setImageDrawable(iconsCache.getThemedIcon(groupHeader.getIconRes()));
} else {
throw new IllegalArgumentException("Unsupported header");
}
headerViewHolder.date.setText(headerString);
headerViewHolder.title.setText(headerString);
} else if (holder instanceof MapMarkersShowHideHistoryViewHolder) {
final MapMarkersShowHideHistoryViewHolder showHideHistoryViewHolder = (MapMarkersShowHideHistoryViewHolder) holder;
final MapMarkersGroup.ShowHideHistoryButton showHideHistoryButton = (MapMarkersGroup.ShowHideHistoryButton) getItem(position);
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() {
@ -273,9 +375,9 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
Object item = items.get(position);
if (item instanceof MapMarker) {
return MARKER_TYPE;
} else if (item instanceof String || item instanceof Integer) {
} else if (item instanceof GroupHeader || item instanceof Integer) {
return HEADER_TYPE;
} else if (item instanceof MapMarkersGroup.ShowHideHistoryButton) {
} else if (item instanceof ShowHideHistoryButton) {
return SHOW_HIDE_HISTORY_TYPE;
} else {
throw new IllegalArgumentException("Unsupported view type");
@ -308,17 +410,17 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
}
private void moveMarkerToHistoryInGroup(MapMarkersGroup group, MapMarker marker) {
group.getMapMarkers().remove(marker);
group.getActiveMarkers().remove(marker);
group.getHistoryMarkers().add(marker);
}
private void restoreMarkerFromHistoryInGroup(MapMarkersGroup group, MapMarker marker) {
group.getHistoryMarkers().remove(marker);
group.getMapMarkers().add(marker);
group.getActiveMarkers().add(marker);
}
private int getLastDisplayItemIndexOfGroup(MapMarkersGroup group) {
List<MapMarker> markers = group.getMapMarkers();
List<MapMarker> markers = group.getActiveMarkers();
int index = -1;
for (MapMarker marker : markers) {
int markerIndex = items.indexOf(marker);
@ -328,4 +430,130 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
}
return index;
}
private static class MapMarkersGroup {
private String name;
private GroupHeader header;
private int type;
private List<MapMarker> activeMarkers = new ArrayList<>();
private List<MapMarker> historyMarkers = new ArrayList<>();
private long creationDate;
private ShowHideHistoryButton showHideHistoryButton;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public GroupHeader getGroupHeader() {
return header;
}
public void setGroupHeader(GroupHeader header) {
this.header = header;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public List<MapMarker> getActiveMarkers() {
return activeMarkers;
}
public void setActiveMarkers(List<MapMarker> activeMarkers) {
this.activeMarkers = activeMarkers;
}
public List<MapMarker> getHistoryMarkers() {
return historyMarkers;
}
public void setHistoryMarkers(List<MapMarker> historyMarkers) {
this.historyMarkers = historyMarkers;
}
public long getCreationDate() {
return creationDate;
}
public void setCreationDate(long creationDate) {
this.creationDate = creationDate;
}
public ShowHideHistoryButton getShowHideHistoryButton() {
return showHideHistoryButton;
}
public void setShowHideHistoryButton(ShowHideHistoryButton showHideHistoryButton) {
this.showHideHistoryButton = showHideHistoryButton;
}
}
private static class ShowHideHistoryButton {
private boolean showHistory;
private MapMarkersGroup group;
public boolean isShowHistory() {
return showHistory;
}
public void setShowHistory(boolean showHistory) {
this.showHistory = showHistory;
}
public MapMarkersGroup getGroup() {
return group;
}
public void setGroup(MapMarkersGroup group) {
this.group = group;
}
}
private static class GroupHeader {
private String groupName;
private int activeMarkersCount;
private int historyMarkersCount;
private int iconRes;
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public int getActiveMarkersCount() {
return activeMarkersCount;
}
public void setActiveMarkersCount(int activeMarkersCount) {
this.activeMarkersCount = activeMarkersCount;
}
public int getHistoryMarkersCount() {
return historyMarkersCount;
}
public void setHistoryMarkersCount(int historyMarkersCount) {
this.historyMarkersCount = historyMarkersCount;
}
public int getIconRes() {
return iconRes;
}
public void setIconRes(int iconRes) {
this.iconRes = iconRes;
}
}
}

View file

@ -160,7 +160,7 @@ public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<RecyclerView.
} else {
dateString = String.valueOf(dateHeader);
}
dateViewHolder.date.setText(dateString);
dateViewHolder.title.setText(dateString);
}
}