Saves distance, visitedNames list and displays saved info in statistics.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.a1.nextlocation;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
@@ -12,6 +13,7 @@ import androidx.fragment.app.DialogFragment;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import com.a1.nextlocation.data.Data;
|
||||
import com.a1.nextlocation.fragments.HelpPopup;
|
||||
import com.a1.nextlocation.fragments.HomeFragment;
|
||||
import com.a1.nextlocation.fragments.Refreshable;
|
||||
@@ -59,6 +61,8 @@ public class MainActivity extends AppCompatActivity implements Refreshable {
|
||||
CouponListManager.INSTANCE.load();
|
||||
RouteListManager.INSTANCE.setContext(this);
|
||||
RouteListManager.INSTANCE.load();
|
||||
Data.INSTANCE.setContext(this);
|
||||
Data.INSTANCE.load();
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).commit();
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package com.a1.nextlocation.data;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@@ -11,6 +18,16 @@ public enum Data {
|
||||
private int locationsVisited = 0;
|
||||
private long totalTime = 0;
|
||||
private double zoom = 0;
|
||||
private SharedPreferences.Editor editor;
|
||||
private Context context;
|
||||
|
||||
public void setContext(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void setEditor(SharedPreferences.Editor editor) {
|
||||
this.editor = editor;
|
||||
}
|
||||
|
||||
public double getZoom() {
|
||||
return zoom;
|
||||
@@ -20,10 +37,13 @@ public enum Data {
|
||||
this.zoom = zoom;
|
||||
}
|
||||
|
||||
private final ArrayList<String> visitedNames = new ArrayList<>();
|
||||
private ArrayList<String> visitedNames = new ArrayList<>();
|
||||
|
||||
public void addDistance(double d) {
|
||||
distanceTraveled += d;
|
||||
|
||||
editor.putString("distanceTraveled", String.valueOf(distanceTraveled));
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public long getTotalTime() {
|
||||
@@ -34,7 +54,6 @@ public enum Data {
|
||||
totalTime += time;
|
||||
}
|
||||
|
||||
|
||||
public double getDistanceTraveled() {
|
||||
return distanceTraveled;
|
||||
}
|
||||
@@ -43,6 +62,9 @@ public enum Data {
|
||||
if (!visitedNames.contains(location.getName())) {
|
||||
locationsVisited++;
|
||||
visitedNames.add(location.getName());
|
||||
saveVisitedNamesList();
|
||||
editor.putInt("locationsVisited", locationsVisited);
|
||||
editor.apply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,5 +72,25 @@ public enum Data {
|
||||
return locationsVisited;
|
||||
}
|
||||
|
||||
public void saveVisitedNamesList(){
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(visitedNames);
|
||||
editor.putString("visitedNames", json);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public ArrayList<String> loadAndGetVisitedNamesList(){
|
||||
String json = context.getSharedPreferences("Data", Context.MODE_PRIVATE).getString("visitedNames", "[]");
|
||||
Type type = new TypeToken<ArrayList<String>>() {}.getType();
|
||||
visitedNames = new Gson().fromJson(json, type);
|
||||
return visitedNames;
|
||||
}
|
||||
|
||||
public void load(){
|
||||
SharedPreferences prefs = context.getSharedPreferences("Data", Context.MODE_PRIVATE);
|
||||
this.editor = prefs.edit();
|
||||
Data.INSTANCE.addDistance(Double.parseDouble(prefs.getString("distanceTraveled", "0")));
|
||||
this.locationsVisited = loadAndGetVisitedNamesList().size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.a1.nextlocation.fragments;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.location.Location;
|
||||
@@ -336,8 +337,9 @@ public class HomeFragment extends Fragment implements LocationListener {
|
||||
if (currentLocation != null) {
|
||||
double distance = currentLocation.distanceTo(location); // in meters
|
||||
// can't walk 100 meters in a few seconds
|
||||
if (distance < 100)
|
||||
if (distance < 100) {
|
||||
Data.INSTANCE.addDistance(distance);
|
||||
}
|
||||
}
|
||||
currentLocation = location;
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ public class StatisticFragment extends Fragment {
|
||||
|
||||
private void initializeDistanceTextView(View view){
|
||||
distance = view.findViewById(R.id.statistics_km);
|
||||
double dist = Data.INSTANCE.getDistanceTraveled()/1000;
|
||||
double dist = Double.parseDouble(getContext().getSharedPreferences("Data", Context.MODE_PRIVATE).getString("distanceTraveled", "0")) /1000;
|
||||
if (getContext().getSharedPreferences("Settings", Context.MODE_PRIVATE).getBoolean("imperialSwitch", false))
|
||||
distance.setText("" + String.format("%.1f",dist * 0.621371) + " mi");
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user