Add swipes with snackbar in list

This commit is contained in:
PavelRatushny 2017-09-21 18:28:23 +03:00
parent 88c882d47d
commit e5bb69053b
4 changed files with 162 additions and 9 deletions

View file

@ -481,6 +481,21 @@ public class MapMarkersHelper {
}
}
public void addMarker(MapMarker marker, int position) {
if (marker != null) {
markersDbHelper.addMarker(marker);
if (marker.history) {
mapMarkersHistory.add(position, marker);
sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC);
} else {
mapMarkers.add(position, marker);
checkAndFixActiveMarkersOrderIfNeeded();
}
addMarkerToGroup(marker);
refresh();
}
}
public void restoreMarkerFromHistory(MapMarker marker, int position) {
if (marker != null) {
markersDbHelper.restoreMapMarkerFromHistory(marker);

View file

@ -41,7 +41,7 @@ public class MapMarkersActiveFragment extends Fragment implements OsmAndCompassL
final MapActivity mapActivity = (MapActivity) getActivity();
adapter = new MapMarkersActiveAdapter(mapActivity);
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MapMarkersItemTouchHelperCallback(adapter));
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MapMarkersItemTouchHelperCallback(mapActivity, adapter));
touchHelper.attachToRecyclerView(recyclerView);
adapter.setAdapterListener(new MapMarkersActiveAdapterListener() {

View file

@ -4,6 +4,7 @@ import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
@ -13,6 +14,7 @@ import android.widget.TextView;
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.OsmandSettings;
import net.osmand.plus.R;
@ -220,6 +222,51 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
return true;
}
@Override
public void onItemSwiped(RecyclerView.ViewHolder holder, final int direction) {
final int pos = holder.getAdapterPosition();
final MapMarker marker = getItem(pos);
int snackbarStringRes;
if (direction == ItemTouchHelper.LEFT) {
mapActivity.getMyApplication().getMapMarkersHelper().moveMapMarkerToHistory(marker);
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(marker);
snackbarStringRes = R.string.item_removed;
}
notifyItemRemoved(pos);
if (showDirectionEnabled && pos < 2 && getItemCount() > 1) {
notifyItemChanged(1);
} else if (pos == getItemCount()) {
notifyItemChanged(pos - 1);
}
snackbar = Snackbar.make(holder.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, pos);
} else {
mapActivity.getMyApplication().getMapMarkersHelper().addMarker(marker, pos);
}
notifyItemInserted(pos);
if (showDirectionEnabled && pos < 2 && getItemCount() > 2) {
notifyItemChanged(2);
} else if (pos == getItemCount() - 1) {
notifyItemChanged(pos - 1);
}
}
});
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();
}
@Override
public void onItemDismiss(RecyclerView.ViewHolder holder) {
listener.onDragEnded(holder);

View file

@ -1,14 +1,66 @@
package net.osmand.plus.mapmarkers.adapters;
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.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final ItemTouchHelperAdapter adapter;
private MapActivity mapActivity;
public MapMarkersItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
private Paint backgroundPaint = new Paint();
private Paint iconPaint = new Paint();
private Paint textPaint = new Paint();
private float marginSides;
private Bitmap deleteBitmap;
private Bitmap resetBitmap;
private boolean iconHidden;
private boolean night;
private String delStr;
private String moveToHistoryStr;
private int moveToHistoryStrWidth;
private int textHeight;
public MapMarkersItemTouchHelperCallback(MapActivity mapActivity, ItemTouchHelperAdapter adapter) {
this.mapActivity = mapActivity;
this.adapter = adapter;
marginSides = mapActivity.getResources().getDimension(R.dimen.list_content_padding);
deleteBitmap = BitmapFactory.decodeResource(mapActivity.getResources(), R.drawable.ic_action_delete_dark);
resetBitmap = BitmapFactory.decodeResource(mapActivity.getResources(), R.drawable.ic_action_reset_to_default_dark);
night = !mapActivity.getMyApplication().getSettings().isLightContent();
backgroundPaint.setColor(ContextCompat.getColor(mapActivity, 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(mapActivity.getResources().getDimension(R.dimen.default_desc_text_size));
textPaint.setFakeBoldText(true);
textPaint.setAntiAlias(true);
delStr = mapActivity.getString(R.string.shared_string_delete).toUpperCase();
moveToHistoryStr = mapActivity.getString(R.string.move_to_history).toUpperCase();
Rect bounds = new Rect();
textPaint.getTextBounds(moveToHistoryStr, 0, moveToHistoryStr.length(), bounds);
moveToHistoryStrWidth = bounds.width();
textHeight = bounds.height();
}
@Override
@ -16,15 +68,11 @@ public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback
return false;
}
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
return makeMovementFlags(dragFlags, 0);
final int moveFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
return makeMovementFlags(dragFlags, moveFlags);
}
@Override
@ -33,13 +81,54 @@ public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
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(mapActivity, colorIcon), PorterDuff.Mode.SRC_IN));
}
textPaint.setColor(ContextCompat.getColor(mapActivity, 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 onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
adapter.onItemSwiped(viewHolder, i);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
((MapMarkerItemViewHolder) viewHolder).optionsBtn.setVisibility(View.VISIBLE);
iconHidden = false;
adapter.onItemDismiss(viewHolder);
}
@ -47,6 +136,8 @@ public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback
boolean onItemMove(int from, int to);
void onItemSwiped(RecyclerView.ViewHolder holder, int direction);
void onItemDismiss(RecyclerView.ViewHolder holder);
}
}