From 53ea394ef523aa28e5c278ee01bae1b9f318f3bc Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 14 Jan 2021 10:28:11 +0100 Subject: [PATCH] fixed locatiooooons --- .../java/com/a1/nextlocation/data/Data.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) 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 d9d8352..f81ea3c 100644 --- a/app/src/main/java/com/a1/nextlocation/data/Data.java +++ b/app/src/main/java/com/a1/nextlocation/data/Data.java @@ -4,17 +4,21 @@ import android.content.Context; import android.content.SharedPreferences; import android.util.Log; +import com.a1.nextlocation.recyclerview.LocationListManager; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Arrays; /** * singleton to keep track of different global data */ public enum Data { INSTANCE; + + private final String TAG = Data.class.getCanonicalName(); private double distanceTraveled = 0; private int locationsVisited = 0; private long totalTime = 0; @@ -103,17 +107,41 @@ public enum Data { String json = context.getSharedPreferences("Data", Context.MODE_PRIVATE).getString("visitedNames", "[]"); Type type = new TypeToken>() {}.getType(); visitedNames = new Gson().fromJson(json, type); + Log.i(TAG, "loadAndGetVisitedNamesList: visited names: " + Arrays.toString(visitedNames.toArray())); return visitedNames; } + public void load(){ SharedPreferences prefs = context.getSharedPreferences("Data", Context.MODE_PRIVATE); this.editor = prefs.edit(); this.distanceTraveled = (Double.parseDouble(prefs.getString("distanceTraveled", "0"))); + this.locationsVisited = loadAndGetVisitedNamesList().size(); + syncLocationObjectsWithList(); this.totalTime = prefs.getLong("timeWalked", 0); } + /** + * sync the visited boolean for location objects with the received list + */ + private void syncLocationObjectsWithList() { + for (Location l : LocationListManager.INSTANCE.getLocationList()) { + if (visitedNames.contains(l.getName())) l.setVisited(true); + } + } + + /** + * clears the visited locations + */ + public void clearVisitedLocations() { + visitedNames.clear(); + locationsVisited = 0; + for (Location l : LocationListManager.INSTANCE.getLocationList()) { + l.setVisited(false); + } + } + public boolean isVisited(Location location) { return this.visitedNames.contains(location.getName()); }