From b8c1d890255ac6212f908375282e54e9ce099840 Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Mon, 11 Sep 2017 13:04:40 +0300 Subject: [PATCH] Add swipe to history --- .../mapmarkers/MapMarkersHistoryFragment.java | 76 +++++++++++++++++-- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersHistoryFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersHistoryFragment.java index 3ad7f41f8f..a169fbe04c 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersHistoryFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersHistoryFragment.java @@ -1,26 +1,36 @@ 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.os.Bundle; import android.support.annotation.Nullable; -import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; +import android.support.v4.content.ContextCompat; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import net.osmand.data.PointDescription; import net.osmand.plus.MapMarkersHelper; 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.adapters.MapMarkerDateViewHolder; import net.osmand.plus.mapmarkers.adapters.MapMarkersHistoryAdapter; public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHelper.MapMarkerChangedListener { - MapMarkersHistoryAdapter adapter; - OsmandApplication app; + private MapMarkersHistoryAdapter adapter; + private OsmandApplication app; + private Paint backgroundPaint = new Paint(); + private Paint iconPaint = new Paint(); @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -31,8 +41,10 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - app = getMyApplication(); + final boolean night = !app.getSettings().isLightContent(); final MapActivity mapActivity = (MapActivity) getActivity(); + backgroundPaint.setColor(ContextCompat.getColor(getActivity(), night ? R.color.dashboard_divider_dark : R.color.dashboard_divider_light)); + iconPaint.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(getActivity(), night ? 0 : R.color.icon_color), PorterDuff.Mode.SRC_IN)); Fragment historyMarkerMenuFragment = mapActivity.getSupportFragmentManager().findFragmentByTag(HistoryMarkerMenuBottomSheetDialogFragment.TAG); if (historyMarkerMenuFragment != null) { @@ -42,6 +54,58 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel final RecyclerView recyclerView = new RecyclerView(getContext()); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { + private float iconMarginSides = 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); + + @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) { + View itemView = viewHolder.itemView; + 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() + iconMarginSides,itemView.getTop() + iconMarginTop, iconPaint); + } 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() - iconMarginSides, itemView.getTop() + iconMarginTop, iconPaint); + } + } + super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); + } + + @Override + public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + if (viewHolder instanceof MapMarkerDateViewHolder) { + return 0; + } + return super.getSwipeDirs(recyclerView, viewHolder); + } + + @Override + public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { + int pos = viewHolder.getAdapterPosition(); + Object item = adapter.getItem(pos); + if (item instanceof MapMarker) { + if (direction == ItemTouchHelper.LEFT) { + app.getMapMarkersHelper().restoreMarkerFromHistory((MapMarker) item, 0); + } else { + app.getMapMarkersHelper().removeMarkerFromHistory((MapMarker) item); + } + adapter.notifyItemRemoved(pos); + } + } + }; + ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback); + itemTouchHelper.attachToRecyclerView(recyclerView); + adapter = new MapMarkersHistoryAdapter(mapActivity.getMyApplication()); adapter.setAdapterListener(new MapMarkersHistoryAdapter.MapMarkersHistoryAdapterListener() { @Override @@ -105,7 +169,7 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel } public OsmandApplication getMyApplication() { - return (OsmandApplication)getActivity().getApplication(); + return (OsmandApplication) getActivity().getApplication(); } @Override