added stopping route if last location has been reached

This commit is contained in:
Sem van der Hoeven
2021-01-06 14:40:30 +01:00
parent f806015963
commit a27ebfc0c3

View File

@@ -89,14 +89,7 @@ public class HomeFragment extends Fragment implements LocationListener {
// set up the route stop button // set up the route stop button
stopButton = view.findViewById(R.id.home_stop_route_button); stopButton = view.findViewById(R.id.home_stop_route_button);
stopButton.setOnClickListener(v -> { stopButton.setOnClickListener(v -> {
RouteHandler.INSTANCE.finishRoute(); stopRoute();
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;
}); });
if (RouteHandler.INSTANCE.isFollowingRoute()) { if (RouteHandler.INSTANCE.isFollowingRoute()) {
@@ -108,6 +101,20 @@ public class HomeFragment extends Fragment implements LocationListener {
return view; 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. * 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 //new thread because we don't want the main thread to hang, this method gets called a lot
Thread t = new Thread(() -> { Thread t = new Thread(() -> {
List<com.a1.nextlocation.data.Location> 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()) { 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 (com.a1.nextlocation.data.Location.getDistance(currentLocation.getLatitude(), currentLocation.getLongitude(), l.getLat(), l.getLong()) < 10) {
if (l.equals(last)) stopRoute();
StaticData.INSTANCE.visitLocation(l); StaticData.INSTANCE.visitLocation(l);
} }
} }