From 2df51d08f1dd9026b25c59ca72c4ac0ea8fc3dd3 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Thu, 7 Sep 2017 17:50:55 +0300 Subject: [PATCH] Add functionality to "Move all to history" button --- .../src/net/osmand/plus/MapMarkersHelper.java | 3 ++- .../mapmarkers/MapMarkersActiveFragment.java | 6 ++++-- .../plus/mapmarkers/MapMarkersDbHelper.java | 7 +++++-- .../mapmarkers/MapMarkersDialogFragment.java | 21 ++++++++++++------- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index d1a0ff686e..4d967c08bf 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -281,7 +281,8 @@ public class MapMarkersHelper { cancelAddressRequests(); markersDbHelper.moveAllActiveMarkersToHistory(); mapMarkers.clear(); - mapMarkersHistory = markersDbHelper.getMarkersHistory(); + mapMarkersHistory.clear(); + mapMarkersHistory.addAll(markersDbHelper.getMarkersHistory()); refresh(); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java index 55a27727ae..8490060ebe 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java @@ -129,12 +129,14 @@ public class MapMarkersActiveFragment extends Fragment implements OsmAndCompassL } void hideSnackbar() { - adapter.hideSnackbar(); + if (adapter != null) { + adapter.hideSnackbar(); + } } private void updateLocationUi() { final MapActivity mapActivity = (MapActivity) getActivity(); - if (mapActivity != null) { + if (mapActivity != null && adapter != null) { mapActivity.getMyApplication().runInUIThread(new Runnable() { @Override public void run() { diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index d8d077e39e..4c1de6d643 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -314,8 +314,11 @@ public class MapMarkersDbHelper { SQLiteConnection db = openConnection(false); if (db != null) { try { - db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + MARKERS_COL_ACTIVE + " = ? " + - "WHERE " + MARKERS_COL_ACTIVE + " = ?", new Object[]{0, 1}); + long visitedDate = System.currentTimeMillis(); + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + + MARKERS_COL_ACTIVE + " = ?, " + + MARKERS_COL_VISITED + " = ? " + + "WHERE " + MARKERS_COL_ACTIVE + " = ?", new Object[]{0, visitedDate, 1}); } finally { db.close(); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java index 389866ff7d..87cb0ad019 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java @@ -28,6 +28,9 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm public static final String TAG = "MapMarkersDialogFragment"; + private MapMarkersActiveFragment activeFragment; + private MapMarkersHistoryFragment historyFragment; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -40,6 +43,9 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + activeFragment = new MapMarkersActiveFragment(); + historyFragment = new MapMarkersHistoryFragment(); + FragmentManager fragmentManager = getChildFragmentManager(); Fragment markerOptionsFragment = fragmentManager.findFragmentByTag(MarkerOptionsBottomSheetDialogFragment.TAG); if (markerOptionsFragment != null) { @@ -77,18 +83,18 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.action_active: - ((MapMarkersActiveFragment) adapter.getItem(0)).startLocationUpdate(); + activeFragment.startLocationUpdate(); if (viewPager.getCurrentItem() != 0) { - ((MapMarkersActiveFragment) adapter.getItem(0)).updateAdapter(); + activeFragment.updateAdapter(); } viewPager.setCurrentItem(0); optionsButton.setVisibility(View.VISIBLE); return true; case R.id.action_history: - ((MapMarkersActiveFragment) adapter.getItem(0)).stopLocationUpdate(); + activeFragment.stopLocationUpdate(); if (viewPager.getCurrentItem() != 1) { - ((MapMarkersHistoryFragment) adapter.getItem(1)).updateAdapter(); - ((MapMarkersActiveFragment) adapter.getItem(0)).hideSnackbar(); + historyFragment.updateAdapter(); + activeFragment.hideSnackbar(); } viewPager.setCurrentItem(1); optionsButton.setVisibility(View.GONE); @@ -132,7 +138,8 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm @Override public void moveAllToHistoryOnClick() { - Toast.makeText(getContext(), "Move all to history", Toast.LENGTH_SHORT).show(); + mapActivity.getMyApplication().getMapMarkersHelper().removeActiveMarkers(); + activeFragment.updateAdapter(); } }; } @@ -165,7 +172,7 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm MapMarkersViewPagerAdapter(FragmentManager fm) { super(fm); - fragments = Arrays.asList(new MapMarkersActiveFragment(), new MapMarkersHistoryFragment()); + fragments = Arrays.asList(activeFragment, historyFragment); } @Override