added route singleton
This commit is contained in:
42
app/src/main/java/com/a1/nextlocation/data/RouteHandler.java
Normal file
42
app/src/main/java/com/a1/nextlocation/data/RouteHandler.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,24 +11,6 @@ public enum StaticData {
|
|||||||
INSTANCE;
|
INSTANCE;
|
||||||
private double distanceTraveled = 0;
|
private double distanceTraveled = 0;
|
||||||
private int locationsVisited = 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<>();
|
private ArrayList<String> visitedNames = new ArrayList<>();
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import android.widget.Toast;
|
|||||||
|
|
||||||
import com.a1.nextlocation.R;
|
import com.a1.nextlocation.R;
|
||||||
import com.a1.nextlocation.data.Route;
|
import com.a1.nextlocation.data.Route;
|
||||||
|
import com.a1.nextlocation.data.RouteHandler;
|
||||||
|
import com.a1.nextlocation.data.StaticData;
|
||||||
import com.a1.nextlocation.network.ApiHandler;
|
import com.a1.nextlocation.network.ApiHandler;
|
||||||
|
|
||||||
public class RouteDetailFragment extends Fragment {
|
public class RouteDetailFragment extends Fragment {
|
||||||
@@ -45,6 +47,7 @@ public class RouteDetailFragment extends Fragment {
|
|||||||
|
|
||||||
public void startRoute(View view) {
|
public void startRoute(View view) {
|
||||||
ApiHandler.INSTANCE.getDirections(route);
|
ApiHandler.INSTANCE.getDirections(route);
|
||||||
|
RouteHandler.INSTANCE.followRoute(route);
|
||||||
Toast.makeText(requireContext(),"Route started!",Toast.LENGTH_SHORT).show();
|
Toast.makeText(requireContext(),"Route started!",Toast.LENGTH_SHORT).show();
|
||||||
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).addToBackStack(null).commit();
|
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).addToBackStack(null).commit();
|
||||||
|
|
||||||
|
|||||||
@@ -127,6 +127,7 @@ public enum ApiHandler {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.d(TAG, "getDirections: caught exception: " + e.getLocalizedMessage());
|
Log.d(TAG, "getDirections: caught exception: " + e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
t.start();
|
t.start();
|
||||||
|
|||||||
Reference in New Issue
Block a user