Add swipes with snackbar in groups

This commit is contained in:
PavelRatushny 2017-09-21 17:28:59 +03:00
parent a689841497
commit 41bf5e3abf
4 changed files with 239 additions and 47 deletions

View file

@ -9,6 +9,7 @@
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="move_to_history">Move to history</string>
<string name="group_will_be_removed_after_restart">Group will be removed after restart</string> <string name="group_will_be_removed_after_restart">Group will be removed after restart</string>
<string name="show_guide_line">Show guide line</string> <string name="show_guide_line">Show guide line</string>
<string name="show_arrows_on_the_map">Show arrows on the map</string> <string name="show_arrows_on_the_map">Show arrows on the map</string>

View file

@ -507,12 +507,17 @@ public class MapMarkersHelper {
} }
} }
public void removeMarkerFromHistory(MapMarker marker) { public void removeMarker(MapMarker marker) {
if (marker != null) { if (marker != null) {
markersDbHelper.removeMarker(marker, true); boolean history = marker.history;
mapMarkersHistory.remove(marker); markersDbHelper.removeMarker(marker, history);
refresh(); if (history) {
mapMarkersHistory.remove(marker);
} else {
mapMarkers.remove(marker);
}
removeMarkerFromGroup(marker); removeMarkerFromGroup(marker);
refresh();
} }
} }
@ -840,15 +845,21 @@ public class MapMarkersHelper {
} }
} }
markersGroup.setMarkers(activeMarkers); markersGroup.setMarkers(activeMarkers);
updateShowHideHistoryButtonInGroup(markersGroup); updateGroup(markersGroup);
} }
} }
private void updateShowHideHistoryButtonInGroup(MapMarkersGroup mapMarkersGroup) { public void updateGroup(MapMarkersGroup mapMarkersGroup) {
if (mapMarkersGroup.getMarkers().size() == 0) {
mapMarkersGroups.remove(mapMarkersGroup);
return;
}
int historyMarkersCount = mapMarkersGroup.getHistoryMarkers().size(); int historyMarkersCount = mapMarkersGroup.getHistoryMarkers().size();
ShowHideHistoryButton showHideHistoryButton = mapMarkersGroup.getShowHideHistoryButton(); ShowHideHistoryButton showHideHistoryButton = mapMarkersGroup.getShowHideHistoryButton();
if (showHideHistoryButton != null && historyMarkersCount == 0) { if (showHideHistoryButton != null) {
mapMarkersGroup.setShowHideHistoryButton(null); if (historyMarkersCount == 0) {
mapMarkersGroup.setShowHideHistoryButton(null);
}
} else if (historyMarkersCount > 0) { } else if (historyMarkersCount > 0) {
showHideHistoryButton = new ShowHideHistoryButton(); showHideHistoryButton = new ShowHideHistoryButton();
showHideHistoryButton.setShowHistory(false); showHideHistoryButton.setShowHistory(false);
@ -862,7 +873,7 @@ public class MapMarkersHelper {
MapMarkersGroup mapMarkersGroup = getMapMarkerGroupByName(marker.groupName); MapMarkersGroup mapMarkersGroup = getMapMarkerGroupByName(marker.groupName);
if (mapMarkersGroup != null) { if (mapMarkersGroup != null) {
mapMarkersGroup.getMarkers().add(marker); mapMarkersGroup.getMarkers().add(marker);
updateShowHideHistoryButtonInGroup(mapMarkersGroup); updateGroup(mapMarkersGroup);
if (mapMarkersGroup.getName() == null) { if (mapMarkersGroup.getName() == null) {
sortMarkers(mapMarkersGroup.getMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); sortMarkers(mapMarkersGroup.getMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
} }
@ -876,27 +887,32 @@ public class MapMarkersHelper {
private MapMarkersGroup createMapMarkerGroup(MapMarker marker) { private MapMarkersGroup createMapMarkerGroup(MapMarker marker) {
MapMarkersGroup group = new MapMarkersGroup(); MapMarkersGroup group = new MapMarkersGroup();
group.setName(marker.groupName); if (marker.groupName != null) {
group.setGroupKey(marker.groupKey); group.setName(marker.groupName);
MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey); group.setGroupKey(marker.groupKey);
if (syncGroup != null) { MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey);
group.setType(syncGroup.getType()); if (syncGroup != null) {
group.setType(syncGroup.getType());
}
group.setColor(MapMarker.getColorId(marker.colorIndex));
} }
group.setColor(MapMarker.getColorId(marker.colorIndex));
group.setCreationDate(marker.creationDate); group.setCreationDate(marker.creationDate);
mapMarkersGroups.add(group); mapMarkersGroups.add(group);
sortGroups();
return group; return group;
} }
private void createHeaderAndHistoryButtonInGroup(MapMarkersGroup group) { private void createHeaderAndHistoryButtonInGroup(MapMarkersGroup group) {
GroupHeader header = new GroupHeader(); if (group.getName() != null) {
int type = group.getType(); GroupHeader header = new GroupHeader();
if (type != -1) { int type = group.getType();
header.setIconRes(type == MapMarkersHelper.MarkersSyncGroup.FAVORITES_TYPE ? R.drawable.ic_action_fav_dark : R.drawable.ic_action_track_16); if (type != -1) {
header.setIconRes(type == MapMarkersHelper.MarkersSyncGroup.FAVORITES_TYPE ? R.drawable.ic_action_fav_dark : R.drawable.ic_action_track_16);
}
header.setGroup(group);
group.setGroupHeader(header);
updateGroup(group);
} }
header.setGroup(group);
group.setGroupHeader(header);
updateShowHideHistoryButtonInGroup(group);
} }
private void removeMarkerFromGroup(MapMarker marker) { private void removeMarkerFromGroup(MapMarker marker) {
@ -904,7 +920,7 @@ public class MapMarkersHelper {
MapMarkersGroup mapMarkersGroup = getMapMarkerGroupByName(marker.groupName); MapMarkersGroup mapMarkersGroup = getMapMarkerGroupByName(marker.groupName);
if (mapMarkersGroup != null) { if (mapMarkersGroup != null) {
mapMarkersGroup.getMarkers().remove(marker); mapMarkersGroup.getMarkers().remove(marker);
updateShowHideHistoryButtonInGroup(mapMarkersGroup); updateGroup(mapMarkersGroup);
} }
} }
} }
@ -931,7 +947,15 @@ public class MapMarkersHelper {
} else { } else {
MapMarkersGroup group = groupsMap.get(groupName); MapMarkersGroup group = groupsMap.get(groupName);
if (group == null) { if (group == null) {
group = createMapMarkerGroup(marker); group = new MapMarkersGroup();
group.setName(marker.groupName);
group.setGroupKey(marker.groupKey);
MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey);
if (syncGroup != null) {
group.setType(syncGroup.getType());
}
group.setColor(MapMarker.getColorId(marker.colorIndex));
group.setCreationDate(marker.creationDate);
groupsMap.put(groupName, group); groupsMap.put(groupName, group);
} else { } else {
long markerCreationDate = marker.creationDate; long markerCreationDate = marker.creationDate;
@ -943,33 +967,44 @@ public class MapMarkersHelper {
} }
} }
mapMarkersGroups = new ArrayList<>(groupsMap.values()); mapMarkersGroups = new ArrayList<>(groupsMap.values());
sortGroups(mapMarkersGroups); if (noGroup != null) {
mapMarkersGroups.add(noGroup);
}
sortGroups();
for (MapMarkersGroup group : mapMarkersGroups) { for (MapMarkersGroup group : mapMarkersGroups) {
createHeaderAndHistoryButtonInGroup(group); createHeaderAndHistoryButtonInGroup(group);
} }
if (noGroup != null) {
sortMarkers(noGroup.getMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
mapMarkersGroups.add(0, noGroup);
}
} }
private void sortGroups(List<MapMarkersGroup> groups) { private void sortGroups() {
Collections.sort(groups, new Comparator<MapMarkersGroup>() { if (mapMarkersGroups.size() > 0) {
@Override MapMarkersGroup noGroup = null;
public int compare(MapMarkersGroup group1, MapMarkersGroup group2) { for (int i = 0; i < mapMarkersGroups.size(); i++) {
long t1 = group1.getCreationDate(); MapMarkersGroup group = mapMarkersGroups.get(i);
long t2 = group2.getCreationDate(); if (group.getName() == null) {
if (t1 > t2) { sortMarkers(group.getMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
return -1; noGroup = mapMarkersGroups.remove(i);
} else if (t1 == t2) {
return 0;
} else {
return 1;
} }
} }
}); Collections.sort(mapMarkersGroups, 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;
}
}
});
if (noGroup != null) {
mapMarkersGroups.add(0, noGroup);
}
}
} }
public MapMarkersGroup getMapMarkerGroupByName(String name) { public MapMarkersGroup getMapMarkerGroupByName(String name) {

View file

@ -1,22 +1,37 @@
package net.osmand.plus.mapmarkers; package net.osmand.plus.mapmarkers;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView;
import net.osmand.Location; import net.osmand.Location;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener; import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener;
import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.plus.dashboard.DashLocationFragment;
import net.osmand.plus.mapmarkers.adapters.MapMarkerItemViewHolder;
import net.osmand.plus.mapmarkers.adapters.MapMarkersGroupsAdapter; import net.osmand.plus.mapmarkers.adapters.MapMarkersGroupsAdapter;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
@ -28,13 +43,151 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL
private Float heading; private Float heading;
private Location location; private Location location;
private boolean locationUpdateStarted; private boolean locationUpdateStarted;
private Paint backgroundPaint = new Paint();
private Paint iconPaint = new Paint();
private Paint textPaint = new Paint();
private Snackbar snackbar;
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final RecyclerView recyclerView = new RecyclerView(getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
final MapActivity mapActivity = (MapActivity) getActivity(); final MapActivity mapActivity = (MapActivity) getActivity();
final boolean night = !mapActivity.getMyApplication().getSettings().isLightContent();
final RecyclerView recyclerView = new RecyclerView(getContext());
backgroundPaint.setColor(ContextCompat.getColor(getActivity(), night ? R.color.dashboard_divider_dark : R.color.dashboard_divider_light));
backgroundPaint.setStyle(Paint.Style.FILL_AND_STROKE);
backgroundPaint.setAntiAlias(true);
iconPaint.setAntiAlias(true);
iconPaint.setFilterBitmap(true);
iconPaint.setDither(true);
textPaint.setTextSize(getResources().getDimension(R.dimen.default_desc_text_size));
textPaint.setFakeBoldText(true);
textPaint.setAntiAlias(true);
final String delStr = getString(R.string.shared_string_delete).toUpperCase();
final String moveToHistoryStr = getString(R.string.move_to_history).toUpperCase();
final Rect bounds = new Rect();
textPaint.getTextBounds(moveToHistoryStr, 0, moveToHistoryStr.length(), bounds);
final int moveToHistoryStrWidth = bounds.width();
final int textHeight = bounds.height();
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
private float marginSides = getResources().getDimension(R.dimen.list_content_padding);
private Bitmap deleteBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_delete_dark);
private Bitmap resetBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_reset_to_default_dark);
private boolean iconHidden;
@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
boolean markerViewHolder = viewHolder instanceof MapMarkerItemViewHolder;
if (markerViewHolder) {
MapMarker marker = (MapMarker) adapter.getItem(viewHolder.getAdapterPosition());
if (marker.history) {
return ItemTouchHelper.RIGHT;
} else {
return super.getSwipeDirs(recyclerView, viewHolder);
}
} else {
return 0;
}
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && viewHolder instanceof MapMarkerItemViewHolder) {
if (!iconHidden && isCurrentlyActive) {
((MapMarkerItemViewHolder) viewHolder).optionsBtn.setVisibility(View.GONE);
iconHidden = true;
}
View itemView = viewHolder.itemView;
int colorIcon;
int colorText;
if (Math.abs(dX) > itemView.getWidth() / 2) {
colorIcon = R.color.map_widget_blue;
colorText = R.color.map_widget_blue;
} else {
colorIcon = night ? 0 : R.color.icon_color;
colorText = R.color.dashboard_subheader_text_light;
}
if (colorIcon != 0) {
iconPaint.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(getActivity(), colorIcon), PorterDuff.Mode.SRC_IN));
}
textPaint.setColor(ContextCompat.getColor(getActivity(), colorText));
float textMarginTop = ((float) itemView.getHeight() - (float) textHeight) / 2;
if (dX > 0) {
c.drawRect(itemView.getLeft(), itemView.getTop(), dX, itemView.getBottom(), backgroundPaint);
float iconMarginTop = ((float) itemView.getHeight() - (float) deleteBitmap.getHeight()) / 2;
c.drawBitmap(deleteBitmap, itemView.getLeft() + marginSides, itemView.getTop() + iconMarginTop, iconPaint);
c.drawText(delStr, itemView.getLeft() + 2 * marginSides + deleteBitmap.getWidth(),
itemView.getTop() + textMarginTop + textHeight, textPaint);
} else {
c.drawRect(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom(), backgroundPaint);
float iconMarginTop = ((float) itemView.getHeight() - (float) resetBitmap.getHeight()) / 2;
c.drawBitmap(resetBitmap, itemView.getRight() - resetBitmap.getWidth() - marginSides, itemView.getTop() + iconMarginTop, iconPaint);
c.drawText(moveToHistoryStr, itemView.getRight() - resetBitmap.getWidth() - 2 * marginSides - moveToHistoryStrWidth,
itemView.getTop() + textMarginTop + textHeight, textPaint);
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof MapMarkerItemViewHolder) {
((MapMarkerItemViewHolder) viewHolder).optionsBtn.setVisibility(View.VISIBLE);
iconHidden = false;
}
super.clearView(recyclerView, viewHolder);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, final int direction) {
final int pos = viewHolder.getAdapterPosition();
Object item = adapter.getItem(pos);
if (item instanceof MapMarker) {
final MapMarker marker = (MapMarker) item;
int snackbarStringRes;
if (direction == ItemTouchHelper.LEFT) {
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);
snackbarStringRes = R.string.item_removed;
}
updateAdapter();
snackbar = Snackbar.make(viewHolder.itemView, snackbarStringRes, Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_undo, new View.OnClickListener() {
@Override
public void onClick(View view) {
if (direction == ItemTouchHelper.LEFT) {
mapActivity.getMyApplication().getMapMarkersHelper().restoreMarkerFromHistory(marker, 0);
} else {
mapActivity.getMyApplication().getMapMarkersHelper().addMarker(marker);
}
updateAdapter();
}
});
View snackBarView = snackbar.getView();
TextView tv = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_action);
tv.setTextColor(ContextCompat.getColor(mapActivity, R.color.color_dialog_buttons_dark));
snackbar.show();
}
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
adapter = new MapMarkersGroupsAdapter(mapActivity); adapter = new MapMarkersGroupsAdapter(mapActivity);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
@ -87,6 +240,9 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL
if (adapter != null) { if (adapter != null) {
adapter.hideSnackbar(); adapter.hideSnackbar();
} }
if (snackbar != null && snackbar.isShown()) {
snackbar.dismiss();
}
} }
@Override @Override

View file

@ -152,7 +152,7 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
app.getMapMarkersHelper().restoreMarkerFromHistory((MapMarker) item, 0); app.getMapMarkersHelper().restoreMarkerFromHistory((MapMarker) item, 0);
snackbarStringRes = R.string.marker_moved_to_active; snackbarStringRes = R.string.marker_moved_to_active;
} else { } else {
app.getMapMarkersHelper().removeMarkerFromHistory((MapMarker) item); app.getMapMarkersHelper().removeMarker((MapMarker) item);
snackbarStringRes = R.string.item_removed; snackbarStringRes = R.string.item_removed;
} }
adapter.notifyItemRemoved(pos); adapter.notifyItemRemoved(pos);
@ -225,7 +225,7 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
public void onDeleteMarker(int pos) { public void onDeleteMarker(int pos) {
Object item = adapter.getItem(pos); Object item = adapter.getItem(pos);
if (item instanceof MapMarker) { if (item instanceof MapMarker) {
app.getMapMarkersHelper().removeMarkerFromHistory((MapMarker) item); app.getMapMarkersHelper().removeMarker((MapMarker) item);
adapter.notifyItemRemoved(pos); adapter.notifyItemRemoved(pos);
} }
} }