From a27ebfc0c37ce1a6efbf899f8b9cdf6c9f70ff8c Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 6 Jan 2021 14:40:30 +0100 Subject: [PATCH] added stopping route if last location has been reached --- .../nextlocation/fragments/HomeFragment.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java index 34a573e..42ede66 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -89,14 +89,7 @@ public class HomeFragment extends Fragment implements LocationListener { // set up the route stop button stopButton = view.findViewById(R.id.home_stop_route_button); stopButton.setOnClickListener(v -> { - RouteHandler.INSTANCE.finishRoute(); - stopButton.setVisibility(View.GONE); - Toast.makeText(requireContext(), getResources().getString(R.string.route_stop_toast), Toast.LENGTH_SHORT).show(); - mapView.getOverlays().remove(roadOverlay); - mapView.getOverlays().remove(allLocationsOverlay); - addLocations(); - mapView.invalidate(); - roadOverlay = null; + stopRoute(); }); if (RouteHandler.INSTANCE.isFollowingRoute()) { @@ -108,6 +101,20 @@ public class HomeFragment extends Fragment implements LocationListener { return view; } + /** + * stops the current route + */ + private void stopRoute() { + RouteHandler.INSTANCE.finishRoute(); + stopButton.setVisibility(View.GONE); + Toast.makeText(requireContext(), getResources().getString(R.string.route_stop_toast), Toast.LENGTH_SHORT).show(); + mapView.getOverlays().remove(roadOverlay); + mapView.getOverlays().remove(allLocationsOverlay); + addLocations(); + mapView.invalidate(); + roadOverlay = null; + } + /** * callback method that gets called when there are new directions available in the form of a {@link DirectionsResult} object. * @@ -330,8 +337,11 @@ public class HomeFragment extends Fragment implements LocationListener { //new thread because we don't want the main thread to hang, this method gets called a lot Thread t = new Thread(() -> { + List locs = RouteHandler.INSTANCE.getCurrentRoute().getLocations(); + com.a1.nextlocation.data.Location last = locs.get(locs.size()-1); for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) { if (com.a1.nextlocation.data.Location.getDistance(currentLocation.getLatitude(), currentLocation.getLongitude(), l.getLat(), l.getLong()) < 10) { + if (l.equals(last)) stopRoute(); StaticData.INSTANCE.visitLocation(l); } }