added route singleton

This commit is contained in:
Sem van der Hoeven
2021-01-06 10:48:03 +01:00
parent abc58d3606
commit f6af7ae80b
4 changed files with 46 additions and 18 deletions

View File

@@ -0,0 +1,42 @@
package com.a1.nextlocation.data;
/**
* singleton to track the current route that is being followed
*/
public enum RouteHandler {
INSTANCE;
private boolean isFollowingRoute = false;
private Route currentRoute;
private int stepCount = 0;
public int getStepCount() {
return stepCount;
}
public void addStep() {
stepCount++;
}
public void finishRoute() {
stepCount = 0;
isFollowingRoute = false;
currentRoute = null;
}
public void followRoute(Route route) {
this.currentRoute = route;
setFollowingRoute(true);
}
public boolean isFollowingRoute(Route route) {
return isFollowingRoute && route.equals(currentRoute);
}
public void setFollowingRoute(boolean followingRoute) {
isFollowingRoute = followingRoute;
}
public boolean isFollowingRoute() {
return isFollowingRoute;
}
}

View File

@@ -11,24 +11,6 @@ public enum StaticData {
INSTANCE;
private double distanceTraveled = 0;
private int locationsVisited = 0;
private boolean isFollowingRoute = false;
private String routeName = "";
public void followRoute(Route route) {
routeName = route.getName();
setFollowingRoute(true);
}
public boolean isFollowingRoute(Route route) {
return isFollowingRoute && route.getName().equals(routeName);
}
public void setFollowingRoute(boolean followingRoute) {
isFollowingRoute = followingRoute;
}
public boolean isFollowingRoute() {
return isFollowingRoute;
}
private ArrayList<String> visitedNames = new ArrayList<>();

View File

@@ -14,6 +14,8 @@ import android.widget.Toast;
import com.a1.nextlocation.R;
import com.a1.nextlocation.data.Route;
import com.a1.nextlocation.data.RouteHandler;
import com.a1.nextlocation.data.StaticData;
import com.a1.nextlocation.network.ApiHandler;
public class RouteDetailFragment extends Fragment {
@@ -45,6 +47,7 @@ public class RouteDetailFragment extends Fragment {
public void startRoute(View view) {
ApiHandler.INSTANCE.getDirections(route);
RouteHandler.INSTANCE.followRoute(route);
Toast.makeText(requireContext(),"Route started!",Toast.LENGTH_SHORT).show();
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).addToBackStack(null).commit();

View File

@@ -127,6 +127,7 @@ public enum ApiHandler {
} catch (IOException e) {
Log.d(TAG, "getDirections: caught exception: " + e.getLocalizedMessage());
}
});
t.start();