Add undo for swipe in history

This commit is contained in:
PavelRatushny 2017-09-13 15:37:42 +03:00
parent 56ebb8b33c
commit e21014a9dc
3 changed files with 38 additions and 3 deletions

View file

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

View file

@ -106,6 +106,7 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm
activeFragment.startLocationUpdate(); activeFragment.startLocationUpdate();
if (viewPager.getCurrentItem() != 0) { if (viewPager.getCurrentItem() != 0) {
activeFragment.updateAdapter(); activeFragment.updateAdapter();
historyFragment.hideSnackbar();
} }
viewPager.setCurrentItem(0); viewPager.setCurrentItem(0);
optionsButton.setVisibility(View.VISIBLE); optionsButton.setVisibility(View.VISIBLE);

View file

@ -9,12 +9,12 @@ import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect; 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.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.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -35,6 +35,7 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
private Paint backgroundPaint = new Paint(); private Paint backgroundPaint = new Paint();
private Paint iconPaint = new Paint(); private Paint iconPaint = new Paint();
private Paint textPaint = new Paint(); private Paint textPaint = new Paint();
private Snackbar snackbar;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -140,16 +141,29 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
} }
@Override @Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { public void onSwiped(RecyclerView.ViewHolder viewHolder, final int direction) {
int pos = viewHolder.getAdapterPosition(); final int pos = viewHolder.getAdapterPosition();
Object item = adapter.getItem(pos); Object item = adapter.getItem(pos);
if (item instanceof MapMarker) { if (item instanceof MapMarker) {
final MapMarker marker = (MapMarker) item;
if (direction == ItemTouchHelper.LEFT) { if (direction == ItemTouchHelper.LEFT) {
app.getMapMarkersHelper().restoreMarkerFromHistory((MapMarker) item, 0); app.getMapMarkersHelper().restoreMarkerFromHistory((MapMarker) item, 0);
} else { } else {
app.getMapMarkersHelper().removeMarkerFromHistory((MapMarker) item); app.getMapMarkersHelper().removeMarkerFromHistory((MapMarker) item);
} }
adapter.notifyItemRemoved(pos); adapter.notifyItemRemoved(pos);
snackbar = Snackbar.make(viewHolder.itemView, R.string.item_removed, Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_undo, new View.OnClickListener() {
@Override
public void onClick(View view) {
if (direction == ItemTouchHelper.LEFT) {
app.getMapMarkersHelper().moveMapMarkerToHistory(marker);
} else {
app.getMapMarkersHelper().addMarker(marker);
}
}
});
snackbar.show();
} }
} }
}; };
@ -183,6 +197,12 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel
return recyclerView; return recyclerView;
} }
void hideSnackbar() {
if (snackbar != null && snackbar.isShown()) {
snackbar.dismiss();
}
}
private HistoryMarkerMenuBottomSheetDialogFragment.HistoryMarkerMenuFragmentListener createHistoryMarkerMenuListener() { private HistoryMarkerMenuBottomSheetDialogFragment.HistoryMarkerMenuFragmentListener createHistoryMarkerMenuListener() {
return new HistoryMarkerMenuBottomSheetDialogFragment.HistoryMarkerMenuFragmentListener() { return new HistoryMarkerMenuBottomSheetDialogFragment.HistoryMarkerMenuFragmentListener() {
@Override @Override