From 91670fb6f393c3d863133f48aaaa75084c3f1376 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 6 Jan 2021 17:16:19 +0100 Subject: [PATCH] rename staticdata to data --- .../java/com/a1/nextlocation/data/Data.java | 105 ++++++------------ .../a1/nextlocation/data/RouteHandler.java | 4 +- .../com/a1/nextlocation/data/StaticData.java | 55 --------- .../nextlocation/fragments/HomeFragment.java | 14 +-- .../fragments/StatisticFragment.java | 8 +- .../java/com/a1/nextlocation/DataTest.java | 49 ++++++++ .../com/a1/nextlocation/StaticDataTest.java | 49 -------- 7 files changed, 98 insertions(+), 186 deletions(-) delete mode 100644 app/src/main/java/com/a1/nextlocation/data/StaticData.java create mode 100644 app/src/test/java/com/a1/nextlocation/DataTest.java delete mode 100644 app/src/test/java/com/a1/nextlocation/StaticDataTest.java diff --git a/app/src/main/java/com/a1/nextlocation/data/Data.java b/app/src/main/java/com/a1/nextlocation/data/Data.java index 17affd4..aac43a3 100644 --- a/app/src/main/java/com/a1/nextlocation/data/Data.java +++ b/app/src/main/java/com/a1/nextlocation/data/Data.java @@ -1,88 +1,55 @@ package com.a1.nextlocation.data; -import androidx.annotation.NonNull; - import java.util.ArrayList; -import java.util.List; -public class Data { +/** + * singleton to keep track of different global data + */ +public enum Data { + INSTANCE; + private double distanceTraveled = 0; + private int locationsVisited = 0; + private long totalTime = 0; + private double zoom = 0; - private float distanceTraveled; - - private int locationsVisited; - - private int totalTime; - - private List couponList; - - private Location nextLocation; - - private Location lastLocation; - - private Route currentRoute; - - - public Data() { - this.distanceTraveled = 0; - this.locationsVisited = 0; - this.totalTime = 0; - couponList = new ArrayList<>(); + public double getZoom() { + return zoom; } - - public float getDistanceTraveled() { + public void setZoom(double zoom) { + this.zoom = zoom; + } + + private ArrayList visitedNames = new ArrayList<>(); + + public void addDistance(double d) { + distanceTraveled += d; + } + + public long getTotalTime() { + return totalTime; + } + + public void addTimeWalked(long time) { + totalTime += time; + } + + + public double getDistanceTraveled() { return distanceTraveled; } - public void setDistanceTraveled(float distanceTraveled) { - this.distanceTraveled = distanceTraveled; + public void visitLocation(Location location) { + if (!visitedNames.contains(location.getName())) { + locationsVisited++; + visitedNames.add(location.getName()); + } } public int getLocationsVisited() { return locationsVisited; } - public void setLocationsVisited(int locationsVisited) { - this.locationsVisited = locationsVisited; - } - public int getTotalTime() { - return totalTime; - } - public void setTotalTime(int totalTime) { - this.totalTime = totalTime; - } - - public List getCouponList() { - return couponList; - } - - public void setCouponList(List couponList) { - this.couponList = couponList; - } - - public Location getNextLocation() { - return nextLocation; - } - - public void setNextLocation(Location nextLocation) { - this.nextLocation = nextLocation; - } - - public Location getLastLocation() { - return lastLocation; - } - - public void setLastLocation(Location lastLocation) { - this.lastLocation = lastLocation; - } - - public Route getCurrentRoute() { - return currentRoute; - } - - public void setCurrentRoute(Route currentRoute) { - this.currentRoute = currentRoute; - } } diff --git a/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java b/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java index 448c7d6..a662c2a 100644 --- a/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java +++ b/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java @@ -45,13 +45,13 @@ public enum RouteHandler { isFollowingRoute = false; currentRoute = null; currentRouteLine = null; - StaticData.INSTANCE.addTimeWalked(System.currentTimeMillis()-startedTime); + Data.INSTANCE.addTimeWalked(System.currentTimeMillis()-startedTime); startedTime = 0; } public void followRoute(Route route) { if (isFollowingRoute) { - StaticData.INSTANCE.addTimeWalked(System.currentTimeMillis()-startedTime); + Data.INSTANCE.addTimeWalked(System.currentTimeMillis()-startedTime); } this.currentRoute = route; setFollowingRoute(true); diff --git a/app/src/main/java/com/a1/nextlocation/data/StaticData.java b/app/src/main/java/com/a1/nextlocation/data/StaticData.java deleted file mode 100644 index 9a9eabe..0000000 --- a/app/src/main/java/com/a1/nextlocation/data/StaticData.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.a1.nextlocation.data; - -import java.util.ArrayList; - -/** - * singleton to keep track of different global data - */ -public enum StaticData { - INSTANCE; - private double distanceTraveled = 0; - private int locationsVisited = 0; - private long timeWalked = 0; - private double zoom = 0; - - public double getZoom() { - return zoom; - } - - public void setZoom(double zoom) { - this.zoom = zoom; - } - - private ArrayList visitedNames = new ArrayList<>(); - - public void addDistance(double d) { - distanceTraveled += d; - } - - public long getTimeWalked() { - return timeWalked; - } - - public void addTimeWalked(long time) { - timeWalked += time; - } - - - public double getDistanceTraveled() { - return distanceTraveled; - } - - public void visitLocation(Location location) { - if (!visitedNames.contains(location.getName())) { - locationsVisited++; - visitedNames.add(location.getName()); - } - } - - public int getLocationsVisited() { - return locationsVisited; - } - - - -} 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 3080567..b870d9c 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -27,7 +27,7 @@ import androidx.fragment.app.FragmentActivity; import com.a1.nextlocation.R; import com.a1.nextlocation.data.RouteHandler; -import com.a1.nextlocation.data.StaticData; +import com.a1.nextlocation.data.Data; import com.a1.nextlocation.json.DirectionsResult; import com.a1.nextlocation.network.ApiHandler; import com.a1.nextlocation.network.DirectionsListener; @@ -181,10 +181,10 @@ public class HomeFragment extends Fragment implements LocationListener { // add the zoom controller IMapController mapController = mapView.getController(); - if (StaticData.INSTANCE.getZoom() == 0) { - StaticData.INSTANCE.setZoom(15.0); + if (Data.INSTANCE.getZoom() == 0) { + Data.INSTANCE.setZoom(15.0); } - mapController.setZoom(StaticData.INSTANCE.getZoom()); + mapController.setZoom(Data.INSTANCE.getZoom()); // add location manager and set the start point LocationManager locationManager = (LocationManager) requireActivity().getSystemService(Context.LOCATION_SERVICE); @@ -341,7 +341,7 @@ public class HomeFragment extends Fragment implements LocationListener { double distance = currentLocation.distanceTo(location); // in meters // can't walk 100 meters in a few seconds if (distance < 100) - StaticData.INSTANCE.addDistance(distance); + Data.INSTANCE.addDistance(distance); } currentLocation = location; @@ -357,12 +357,12 @@ public class HomeFragment extends Fragment implements LocationListener { for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) { // mark the location visited if we are less than 20 meters away if (com.a1.nextlocation.data.Location.getDistance(currentLocation.getLatitude(), currentLocation.getLongitude(), l.getLat(), l.getLong()) < 20) { - StaticData.INSTANCE.visitLocation(l); + Data.INSTANCE.visitLocation(l); if (l.equals(last)) stopRoute(); } } - StaticData.INSTANCE.setZoom(mapView.getZoomLevelDouble()); + Data.INSTANCE.setZoom(mapView.getZoomLevelDouble()); }); t.start(); diff --git a/app/src/main/java/com/a1/nextlocation/fragments/StatisticFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/StatisticFragment.java index af009d6..f1215ab 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/StatisticFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/StatisticFragment.java @@ -14,7 +14,7 @@ import android.widget.TextView; import com.a1.nextlocation.R; import com.a1.nextlocation.data.Coupon; -import com.a1.nextlocation.data.StaticData; +import com.a1.nextlocation.data.Data; import com.a1.nextlocation.recyclerview.CouponAdapter; import com.a1.nextlocation.recyclerview.CouponListManager; @@ -39,11 +39,11 @@ public class StatisticFragment extends Fragment { TextView distance = view.findViewById(R.id.statistics_km); TextView locs = view.findViewById(R.id.statistics_locations_visited); TextView timeText = view.findViewById(R.id.statistics_time_value); - double dist = StaticData.INSTANCE.getDistanceTraveled()/1000; + double dist = Data.INSTANCE.getDistanceTraveled()/1000; distance.setText("" + String.format("%.1f",dist) + " km"); - locs.setText("" + StaticData.INSTANCE.getLocationsVisited()); + locs.setText("" + Data.INSTANCE.getLocationsVisited()); - long seconds = StaticData.INSTANCE.getTimeWalked() / 1000; + long seconds = Data.INSTANCE.getTotalTime() / 1000; long p1 = seconds % 60; long p2 = seconds / 60; long p3 = p2 % 60; diff --git a/app/src/test/java/com/a1/nextlocation/DataTest.java b/app/src/test/java/com/a1/nextlocation/DataTest.java new file mode 100644 index 0000000..dd7d137 --- /dev/null +++ b/app/src/test/java/com/a1/nextlocation/DataTest.java @@ -0,0 +1,49 @@ +package com.a1.nextlocation; + +import com.a1.nextlocation.data.Location; +import com.a1.nextlocation.data.Data; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotSame; + +public class DataTest { + private Data Data; + + @Before + public void init(){ + Data = Data.INSTANCE; + } + + @Test + public void testDistance(){ + Data.addDistance(2356.234); + double expected = 2356.234; + assertEquals(expected, Data.getDistanceTraveled(), 0.01); + Data.addDistance(234342.1); + assertNotEquals(expected, Data.getDistanceTraveled()); + } + + @Test + public void testTimeWalked(){ + Data.addTimeWalked(3456); + long expected = 3456; + assertEquals(expected, Data.getTimeWalked()); + Data.addTimeWalked(3445); + assertNotEquals(expected, Data.getTimeWalked()); + } + + @Test + public void testVisitedLocation(){ + Location testLocation = new Location("test", "test", "test", "test"); + Data.visitLocation(testLocation); + int expected = 1; + assertEquals(expected, Data.getLocationsVisited()); + Data.visitLocation(new Location("TESTFORFALSE", "TESTFORFALSE", "TESTFORFALSE", "TESTFORFALSE")); + assertNotEquals(expected, Data.getLocationsVisited()); + } + +} diff --git a/app/src/test/java/com/a1/nextlocation/StaticDataTest.java b/app/src/test/java/com/a1/nextlocation/StaticDataTest.java deleted file mode 100644 index d9ea744..0000000 --- a/app/src/test/java/com/a1/nextlocation/StaticDataTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.a1.nextlocation; - -import com.a1.nextlocation.data.Location; -import com.a1.nextlocation.data.StaticData; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotSame; - -public class StaticDataTest { - private StaticData staticData; - - @Before - public void init(){ - staticData = StaticData.INSTANCE; - } - - @Test - public void testDistance(){ - staticData.addDistance(2356.234); - double expected = 2356.234; - assertEquals(expected, staticData.getDistanceTraveled(), 0.01); - staticData.addDistance(234342.1); - assertNotEquals(expected, staticData.getDistanceTraveled()); - } - - @Test - public void testTimeWalked(){ - staticData.addTimeWalked(3456); - long expected = 3456; - assertEquals(expected, staticData.getTimeWalked()); - staticData.addTimeWalked(3445); - assertNotEquals(expected, staticData.getTimeWalked()); - } - - @Test - public void testVisitedLocation(){ - Location testLocation = new Location("test", "test", "test", "test"); - staticData.visitLocation(testLocation); - int expected = 1; - assertEquals(expected, staticData.getLocationsVisited()); - staticData.visitLocation(new Location("TESTFORFALSE", "TESTFORFALSE", "TESTFORFALSE", "TESTFORFALSE")); - assertNotEquals(expected, staticData.getLocationsVisited()); - } - -}