Add swipe to history

This commit is contained in:
PavelRatushny 2017-09-11 13:04:40 +03:00
parent aa64fcd47a
commit b8c1d89025

View file

@ -1,26 +1,36 @@
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.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
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 net.osmand.data.PointDescription;
import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker; 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.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapmarkers.adapters.MapMarkerDateViewHolder;
import net.osmand.plus.mapmarkers.adapters.MapMarkersHistoryAdapter; import net.osmand.plus.mapmarkers.adapters.MapMarkersHistoryAdapter;
public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHelper.MapMarkerChangedListener { public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHelper.MapMarkerChangedListener {
MapMarkersHistoryAdapter adapter; private MapMarkersHistoryAdapter adapter;
OsmandApplication app; private OsmandApplication app;
private Paint backgroundPaint = new Paint();
private Paint iconPaint = new Paint();
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -31,8 +41,10 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
@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) {
app = getMyApplication(); final boolean night = !app.getSettings().isLightContent();
final MapActivity mapActivity = (MapActivity) getActivity(); 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); Fragment historyMarkerMenuFragment = mapActivity.getSupportFragmentManager().findFragmentByTag(HistoryMarkerMenuBottomSheetDialogFragment.TAG);
if (historyMarkerMenuFragment != null) { if (historyMarkerMenuFragment != null) {
@ -42,6 +54,58 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
final RecyclerView recyclerView = new RecyclerView(getContext()); final RecyclerView recyclerView = new RecyclerView(getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(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 = new MapMarkersHistoryAdapter(mapActivity.getMyApplication());
adapter.setAdapterListener(new MapMarkersHistoryAdapter.MapMarkersHistoryAdapterListener() { adapter.setAdapterListener(new MapMarkersHistoryAdapter.MapMarkersHistoryAdapterListener() {
@Override @Override
@ -105,7 +169,7 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
} }
public OsmandApplication getMyApplication() { public OsmandApplication getMyApplication() {
return (OsmandApplication)getActivity().getApplication(); return (OsmandApplication) getActivity().getApplication();
} }
@Override @Override