From e5bb69053bd572a1fb6bce31555d57ae37d22008 Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Thu, 21 Sep 2017 18:28:23 +0300 Subject: [PATCH 1/3] Add swipes with snackbar in list --- .../src/net/osmand/plus/MapMarkersHelper.java | 15 +++ .../mapmarkers/MapMarkersActiveFragment.java | 2 +- .../adapters/MapMarkersActiveAdapter.java | 47 ++++++++ .../MapMarkersItemTouchHelperCallback.java | 107 ++++++++++++++++-- 4 files changed, 162 insertions(+), 9 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 7a461137f5..4cf3cd30c1 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -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); diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java index 2736766568..1472affc17 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java @@ -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() { diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java index de5e198d43..b91bace978 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java @@ -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 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); diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java index 5c1684a0d3..3df53f1a0b 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java @@ -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); } } From 75cc6162b7b64921e3379358ab7215da57be543d Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Thu, 21 Sep 2017 18:47:33 +0300 Subject: [PATCH 2/3] Fix swipe in groups --- .../net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java index e280fd5f7d..6b6ed132bc 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java @@ -83,8 +83,9 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL @Override public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { boolean markerViewHolder = viewHolder instanceof MapMarkerItemViewHolder; - if (markerViewHolder) { - MapMarker marker = (MapMarker) adapter.getItem(viewHolder.getAdapterPosition()); + int pos = viewHolder.getAdapterPosition(); + if (markerViewHolder && pos != -1) { + MapMarker marker = (MapMarker) adapter.getItem(pos); if (marker.history) { return ItemTouchHelper.RIGHT; } else { From 25bdac43f54c6ed66b7b13e2b6cde94c04346ba7 Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Thu, 21 Sep 2017 19:04:42 +0300 Subject: [PATCH 3/3] Fix order by menu --- .../layout/fragment_map_markers_dialog.xml | 35 +++++------------- ...nt_marker_order_by_bottom_sheet_dialog.xml | 37 +++++++++++++++++-- .../mapmarkers/MapMarkersDialogFragment.java | 18 --------- .../OrderByBottomSheetDialogFragment.java | 30 +++++++++------ 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/OsmAnd/res/layout/fragment_map_markers_dialog.xml b/OsmAnd/res/layout/fragment_map_markers_dialog.xml index 6bc81c6b5e..725f5e8c94 100644 --- a/OsmAnd/res/layout/fragment_map_markers_dialog.xml +++ b/OsmAnd/res/layout/fragment_map_markers_dialog.xml @@ -20,39 +20,22 @@ app:contentInsetStart="54dp"> - - - - - - - + android:ellipsize="end" + android:maxLines="1" + android:gravity="center_vertical" + android:text="@string/map_markers" + android:textColor="@color/color_white" + osmand:typeface="@string/font_roboto_medium" + android:textSize="@dimen/dialog_header_text_size"/> + + + + + + + diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java index 7a893a25e2..25b3ea47c1 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java @@ -42,7 +42,6 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm private Snackbar snackbar; private LockableViewPager viewPager; - private TextView orderByModeTitle; private boolean lightTheme; @@ -100,7 +99,6 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm if (!lightTheme) { toolbar.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.actionbar_dark_color)); } - orderByModeTitle = toolbar.findViewById(R.id.order_by_mode_text); setOrderByMode(getMyApplication().getSettings().MAP_MARKERS_ORDER_BY_MODE.get()); toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.ic_arrow_back)); @@ -141,7 +139,6 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm historyFragment.hideSnackbar(); groupsFragment.hideSnackbar(); } - orderByModeTitle.setVisibility(View.VISIBLE); viewPager.setCurrentItem(0); optionsButton.setVisibility(View.VISIBLE); return true; @@ -152,7 +149,6 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm activeFragment.hideSnackbar(); historyFragment.hideSnackbar(); } - orderByModeTitle.setVisibility(View.GONE); viewPager.setCurrentItem(1); optionsButton.setVisibility(View.GONE); return true; @@ -163,7 +159,6 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm groupsFragment.hideSnackbar(); activeFragment.hideSnackbar(); } - orderByModeTitle.setVisibility(View.GONE); viewPager.setCurrentItem(2); optionsButton.setVisibility(View.GONE); return true; @@ -255,19 +250,6 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm } private void setOrderByMode(MapMarkersOrderByMode orderByMode) { - String modeStr = ""; - if (orderByMode.isDistanceDescending()) { - modeStr = getString(R.string.distance) + " (" + getString(R.string.descendingly) + ")"; - } else if (orderByMode.isDistanceAscending()) { - modeStr = getString(R.string.distance) + " (" + getString(R.string.ascendingly) + ")"; - } else if (orderByMode.isName()) { - modeStr = getString(R.string.shared_string_name); - } else if (orderByMode.isDateAddedDescending()) { - modeStr = getString(R.string.date_added) + " (" + getString(R.string.descendingly) + ")"; - } else { - modeStr = getString(R.string.date_added) + " (" + getString(R.string.ascendingly) + ")"; - } - orderByModeTitle.setText(modeStr); getMyApplication().getMapMarkersHelper().orderMarkers(orderByMode); activeFragment.updateAdapter(); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/OrderByBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/OrderByBottomSheetDialogFragment.java index cd044f5ff6..58b7a92e57 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/OrderByBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/OrderByBottomSheetDialogFragment.java @@ -58,12 +58,19 @@ public class OrderByBottomSheetDialogFragment extends BottomSheetDialogFragment ImageView nameIcon = (ImageView) mainView.findViewById(R.id.name_icon); nameIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_sort_by_name)); - ImageView dateAddedIcon = (ImageView) mainView.findViewById(R.id.date_added_icon); - dateAddedIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_sort_by_date)); + ImageView dateAddedAscIcon = (ImageView) mainView.findViewById(R.id.date_added_asc_icon); + dateAddedAscIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_sort_by_date)); + + ImageView dateAddedDescIcon = (ImageView) mainView.findViewById(R.id.date_added_desc_icon); + dateAddedDescIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_sort_by_date)); + + ((TextView) mainView.findViewById(R.id.date_added_asc_text)).setText(getString(R.string.date_added) + " (" + getString(R.string.ascendingly) + ")"); + ((TextView) mainView.findViewById(R.id.date_added_desc_text)).setText(getString(R.string.date_added) + " (" + getString(R.string.descendingly) + ")"); mainView.findViewById(R.id.distance_row).setOnClickListener(orderByModeOnClickListener); mainView.findViewById(R.id.name_row).setOnClickListener(orderByModeOnClickListener); - mainView.findViewById(R.id.date_added_row).setOnClickListener(orderByModeOnClickListener); + mainView.findViewById(R.id.date_added_asc_row).setOnClickListener(orderByModeOnClickListener); + mainView.findViewById(R.id.date_added_desc_row).setOnClickListener(orderByModeOnClickListener); mainView.findViewById(R.id.close_row).setOnClickListener(new View.OnClickListener() { @Override @@ -136,21 +143,20 @@ public class OrderByBottomSheetDialogFragment extends BottomSheetDialogFragment MapMarkersOrderByMode modeToSet; switch (view.getId()) { case R.id.distance_row: - if (currentOrderByMode == MapMarkersOrderByMode.DISTANCE_DESC) { - modeToSet = MapMarkersOrderByMode.DISTANCE_ASC; - } else { + if (currentOrderByMode == MapMarkersOrderByMode.DISTANCE_ASC) { modeToSet = MapMarkersOrderByMode.DISTANCE_DESC; + } else { + modeToSet = MapMarkersOrderByMode.DISTANCE_ASC; } break; case R.id.name_row: modeToSet = MapMarkersOrderByMode.NAME; break; - case R.id.date_added_row: - if (currentOrderByMode == MapMarkersOrderByMode.DATE_ADDED_DESC) { - modeToSet = MapMarkersOrderByMode.DATE_ADDED_ASC; - } else { - modeToSet = MapMarkersOrderByMode.DATE_ADDED_DESC; - } + case R.id.date_added_asc_row: + modeToSet = MapMarkersOrderByMode.DATE_ADDED_ASC; + break; + case R.id.date_added_desc_row: + modeToSet = MapMarkersOrderByMode.DATE_ADDED_DESC; break; default: modeToSet = currentOrderByMode;