Skip to content

Commit 23bd617

Browse files
author
Paolo Rotolo
committed
Add possibility to revert to old graph (Fixes #254)
1 parent c39740a commit 23bd617

File tree

6 files changed

+61
-48
lines changed

6 files changed

+61
-48
lines changed

app/src/main/java/org/glucosio/android/activity/PreferencesActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ protected void onCreate(Bundle savedInstanceState) {
6767
super.onCreate(savedInstanceState);
6868
setContentView(R.layout.preferences);
6969

70+
71+
7072
getFragmentManager().beginTransaction()
7173
.replace(R.id.preferencesFrame, new MyPreferenceFragment()).commit();
7274

app/src/main/java/org/glucosio/android/db/DatabaseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public GlucoseReading getGlucoseReadingById(long id) {
310310
List<GlucoseReading> gReadings;
311311
ArrayList<Integer> readings = new ArrayList<Integer>();
312312
313-
gReadings = GlucoseReading.getGlucoseReadings(whereString);
313+
gReadings = GlucoseReading.getGlucoseReadingsWithZeros(whereString);
314314
int i;
315315
for (i=0; i < gReadings.size(); i++){
316316
readings.add(gReadings.get(i).getGlucoseReading());

app/src/main/java/org/glucosio/android/fragment/OverviewFragment.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
import android.Manifest;
2424
import android.app.Dialog;
25+
import android.content.SharedPreferences;
2526
import android.content.pm.PackageManager;
2627
import android.graphics.Color;
2728
import android.os.Build;
2829
import android.os.Bundle;
30+
import android.preference.PreferenceManager;
2931
import android.support.annotation.NonNull;
3032
import android.support.design.widget.Snackbar;
3133
import android.support.v4.app.ActivityCompat;
@@ -130,7 +132,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
130132
GlucosioApplication app = (GlucosioApplication) getActivity().getApplicationContext();
131133
presenter = new OverviewPresenter(this, app.getDBHandler());
132134
if (!presenter.isdbEmpty()) {
133-
presenter.loadDatabase();
135+
presenter.loadDatabase(isNewGraphEnabled());
134136
}
135137

136138
mFragmentView = inflater.inflate(R.layout.fragment_overview, container, false);
@@ -139,11 +141,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
139141
disableTouchTheft(chart);
140142
Legend legend = chart.getLegend();
141143

142-
if (!presenter.isdbEmpty()) {
143-
Collections.reverse(presenter.getGlucoseDatetime());
144-
Collections.reverse(presenter.getGlucoseType());
145-
}
146-
147144
lastReadingTextView = (TextView) mFragmentView.findViewById(R.id.item_history_reading);
148145
lastDateTextView = (TextView) mFragmentView.findViewById(R.id.fragment_overview_last_date);
149146
trendTextView = (TextView) mFragmentView.findViewById(R.id.item_history_trend);
@@ -703,6 +700,11 @@ private void loadRandomTip() {
703700
tipTextView.setText(presenter.getRandomTip(tipsManager));
704701
}
705702

703+
private boolean isNewGraphEnabled(){
704+
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
705+
return !sharedPref.getBoolean("pref_graph_old", false);
706+
}
707+
706708
@NonNull
707709
public String convertDate(@NonNull final String date) {
708710
FormatDateTime dateTime = new FormatDateTime(getActivity().getApplicationContext());

app/src/main/java/org/glucosio/android/presenter/OverviewPresenter.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public boolean isdbEmpty() {
6969
return dB.getGlucoseReadings().size() == 0;
7070
}
7171

72-
public void loadDatabase() {
72+
public void loadDatabase(boolean isNewGraphEnabled) {
7373
this.glucoseReadings = dB.getGlucoseReadings();
74-
this.glucoseGraphObjects = generateGlucoseGraphPoints();
74+
this.glucoseGraphObjects = generateGlucoseGraphPoints(isNewGraphEnabled);
7575
this.glucoseReadingsMonth = dB.getAverageGlucoseReadingsByMonth();
7676
this.glucoseReadingsWeek = dB.getAverageGlucoseReadingsByWeek();
7777
this.glucoseDatetimeWeek = dB.getGlucoseDatetimesByWeek();
@@ -240,35 +240,46 @@ public ArrayList<String> getCholesterolReadingsDateTime(){
240240
return dB.getCholesterolDateTimeAsArray();
241241
}
242242

243-
private List<IntGraphObject> generateGlucoseGraphPoints() {
244-
DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15);
245-
final List<GlucoseReading> glucoseReadings = dB.getLastMonthGlucoseReadings();
246-
247-
Collections.sort(glucoseReadings, new Comparator<GlucoseReading>() {
248-
public int compare(GlucoseReading o1, GlucoseReading o2) {
249-
return o1.getCreated().compareTo(o2.getCreated());
250-
}
251-
});
252-
253-
DateTime startDate = glucoseReadings.size() > 0 ?
254-
minDateTime : DateTime.now();
255-
// This will contain final values
243+
private List<IntGraphObject> generateGlucoseGraphPoints(boolean isNewGraphEnabled) {
256244
final ArrayList<IntGraphObject> finalGraphObjects = new ArrayList<>();
257-
// Transfer values from database to ArrayList as GlucoseGraphObjects
258-
for (int i=0; i<glucoseReadings.size(); i++){
259-
final GlucoseReading reading = glucoseReadings.get(i);
260-
final DateTime createdDate = new DateTime(reading.getCreated());
261-
//add zero values between current value and last added value
262-
addZeroReadings(finalGraphObjects, startDate, createdDate);
263-
//add new value
264-
finalGraphObjects.add(
265-
new IntGraphObject(createdDate, reading.getReading())
266-
);
267-
//update start date
268-
startDate = createdDate;
245+
if (isNewGraphEnabled) {
246+
DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15);
247+
final List<GlucoseReading> glucoseReadings = dB.getLastMonthGlucoseReadings();
248+
249+
Collections.sort(glucoseReadings, new Comparator<GlucoseReading>() {
250+
public int compare(GlucoseReading o1, GlucoseReading o2) {
251+
return o1.getCreated().compareTo(o2.getCreated());
252+
}
253+
});
254+
255+
DateTime startDate = glucoseReadings.size() > 0 ?
256+
minDateTime : DateTime.now();
257+
// Transfer values from database to ArrayList as GlucoseGraphObjects
258+
for (int i = 0; i < glucoseReadings.size(); i++) {
259+
final GlucoseReading reading = glucoseReadings.get(i);
260+
final DateTime createdDate = new DateTime(reading.getCreated());
261+
//add zero values between current value and last added value
262+
addZeroReadings(finalGraphObjects, startDate, createdDate);
263+
//add new value
264+
finalGraphObjects.add(
265+
new IntGraphObject(createdDate, reading.getReading())
266+
);
267+
//update start date
268+
startDate = createdDate;
269+
}
270+
//add last zeros till now
271+
addZeroReadings(finalGraphObjects, startDate, DateTime.now());
272+
} else {
273+
Collections.sort(glucoseReadings, new Comparator<GlucoseReading>() {
274+
public int compare(GlucoseReading o1, GlucoseReading o2) {
275+
return o1.getCreated().compareTo(o2.getCreated());
276+
}
277+
});
278+
for (int i = 0; i < glucoseReadings.size(); i++){
279+
GlucoseReading glucoseReading = glucoseReadings.get(i);
280+
finalGraphObjects.add(new IntGraphObject(new DateTime(glucoseReading.getCreated()), glucoseReading.getReading()));
281+
}
269282
}
270-
//add last zeros till now
271-
addZeroReadings(finalGraphObjects, startDate, DateTime.now());
272283

273284
return finalGraphObjects;
274285
}
@@ -282,14 +293,6 @@ private void addZeroReadings(final ArrayList<IntGraphObject> graphObjects,
282293
}
283294
}
284295

285-
public ArrayList<String> getGlucoseType() {
286-
return glucoseType;
287-
}
288-
289-
public ArrayList<String> getGlucoseDatetime() {
290-
return dB.getGlucoseDateTimeAsArray();
291-
}
292-
293296
public ArrayList<String> getGraphGlucoseDateTime(){
294297
ArrayList<String> glucoseDatetime = new ArrayList<>();
295298
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,4 +437,5 @@
437437
<string name="activity_backup_drive_button_open_drive">Manage on Drive</string>
438438
<string name="mmol_mol">mmol/mol</string>
439439
<string name="mmol_L">mmol/L</string>
440+
<string name="preferences_graph_old">Revert to old graph</string>
440441
</resources>

app/src/main/res/xml/preferences.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
android:dialogTitle="@string/helloactivity_country"
2424
android:key="pref_country"
2525
android:title="@string/helloactivity_country" />
26-
<ListPreference
27-
android:dialogTitle="@string/helloactivity_language"
28-
android:key="pref_language"
29-
android:title="@string/helloactivity_language" />
3026
<EditTextPreference
3127
android:dialogTitle="@string/helloactivity_age"
3228
android:inputType="number"
@@ -78,6 +74,11 @@
7874
android:inputType="number"
7975
android:key="pref_range_max"
8076
android:title="@string/helloactivity_preferred_range_max" />
77+
<SwitchPreference
78+
android:id="@+id/preferences_graph_old"
79+
android:defaultValue="false"
80+
android:key="pref_graph_old"
81+
android:title="@string/preferences_graph_old" />
8182
<SwitchPreference
8283
android:id="@+id/preferences_font_dyslexia"
8384
android:defaultValue="false"
@@ -89,4 +90,8 @@
8990
<Preference
9091
android:key="about_settings"
9192
android:title="@string/preferences_about_glucosio" />
93+
<ListPreference
94+
android:dialogTitle="@string/helloactivity_language"
95+
android:key="pref_language"
96+
android:title="@string/helloactivity_language" />
9297
</PreferenceScreen>

0 commit comments

Comments
 (0)