Skip to content

Commit

Permalink
Merge pull request #255 from Glucosio/254-old-graph-selector
Browse files Browse the repository at this point in the history
Add possibility to revert to old graph (Fixes #254)
  • Loading branch information
emartynov authored Jul 8, 2016
2 parents 9f2058b + acc7a5c commit e1b406b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);



getFragmentManager().beginTransaction()
.replace(R.id.preferencesFrame, new MyPreferenceFragment()).commit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public GlucoseReading getGlucoseReadingById(long id) {
List<GlucoseReading> gReadings;
ArrayList<Integer> readings = new ArrayList<Integer>();
gReadings = GlucoseReading.getGlucoseReadings(whereString);
gReadings = GlucoseReading.getGlucoseReadingsWithZeros(whereString);
int i;
for (i=0; i < gReadings.size(); i++){
readings.add(gReadings.get(i).getGlucoseReading());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

import android.Manifest;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
Expand Down Expand Up @@ -130,7 +132,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
GlucosioApplication app = (GlucosioApplication) getActivity().getApplicationContext();
presenter = new OverviewPresenter(this, app.getDBHandler());
if (!presenter.isdbEmpty()) {
presenter.loadDatabase();
presenter.loadDatabase(isNewGraphEnabled());
}

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

if (!presenter.isdbEmpty()) {
Collections.reverse(presenter.getGlucoseDatetime());
Collections.reverse(presenter.getGlucoseType());
}

lastReadingTextView = (TextView) mFragmentView.findViewById(R.id.item_history_reading);
lastDateTextView = (TextView) mFragmentView.findViewById(R.id.fragment_overview_last_date);
trendTextView = (TextView) mFragmentView.findViewById(R.id.item_history_trend);
Expand Down Expand Up @@ -703,6 +700,11 @@ private void loadRandomTip() {
tipTextView.setText(presenter.getRandomTip(tipsManager));
}

private boolean isNewGraphEnabled(){
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
return !sharedPref.getBoolean("pref_graph_old", false);
}

@NonNull
public String convertDate(@NonNull final String date) {
FormatDateTime dateTime = new FormatDateTime(getActivity().getApplicationContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public boolean isdbEmpty() {
return dB.getGlucoseReadings().size() == 0;
}

public void loadDatabase() {
public void loadDatabase(boolean isNewGraphEnabled) {
this.glucoseReadings = dB.getGlucoseReadings();
this.glucoseGraphObjects = generateGlucoseGraphPoints();
this.glucoseGraphObjects = generateGlucoseGraphPoints(isNewGraphEnabled);
this.glucoseReadingsMonth = dB.getAverageGlucoseReadingsByMonth();
this.glucoseReadingsWeek = dB.getAverageGlucoseReadingsByWeek();
this.glucoseDatetimeWeek = dB.getGlucoseDatetimesByWeek();
Expand Down Expand Up @@ -240,35 +240,46 @@ public ArrayList<String> getCholesterolReadingsDateTime(){
return dB.getCholesterolDateTimeAsArray();
}

private List<IntGraphObject> generateGlucoseGraphPoints() {
DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15);
final List<GlucoseReading> glucoseReadings = dB.getLastMonthGlucoseReadings();

Collections.sort(glucoseReadings, new Comparator<GlucoseReading>() {
public int compare(GlucoseReading o1, GlucoseReading o2) {
return o1.getCreated().compareTo(o2.getCreated());
}
});

DateTime startDate = glucoseReadings.size() > 0 ?
minDateTime : DateTime.now();
// This will contain final values
private List<IntGraphObject> generateGlucoseGraphPoints(boolean isNewGraphEnabled) {
final ArrayList<IntGraphObject> finalGraphObjects = new ArrayList<>();
// Transfer values from database to ArrayList as GlucoseGraphObjects
for (int i=0; i<glucoseReadings.size(); i++){
final GlucoseReading reading = glucoseReadings.get(i);
final DateTime createdDate = new DateTime(reading.getCreated());
//add zero values between current value and last added value
addZeroReadings(finalGraphObjects, startDate, createdDate);
//add new value
finalGraphObjects.add(
new IntGraphObject(createdDate, reading.getReading())
);
//update start date
startDate = createdDate;
if (isNewGraphEnabled) {
DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15);
final List<GlucoseReading> glucoseReadings = dB.getLastMonthGlucoseReadings();

Collections.sort(glucoseReadings, new Comparator<GlucoseReading>() {
public int compare(GlucoseReading o1, GlucoseReading o2) {
return o1.getCreated().compareTo(o2.getCreated());
}
});

DateTime startDate = glucoseReadings.size() > 0 ?
minDateTime : DateTime.now();
// Transfer values from database to ArrayList as GlucoseGraphObjects
for (int i = 0; i < glucoseReadings.size(); i++) {
final GlucoseReading reading = glucoseReadings.get(i);
final DateTime createdDate = new DateTime(reading.getCreated());
//add zero values between current value and last added value
addZeroReadings(finalGraphObjects, startDate, createdDate);
//add new value
finalGraphObjects.add(
new IntGraphObject(createdDate, reading.getReading())
);
//update start date
startDate = createdDate;
}
//add last zeros till now
addZeroReadings(finalGraphObjects, startDate, DateTime.now());
} else {
Collections.sort(glucoseReadings, new Comparator<GlucoseReading>() {
public int compare(GlucoseReading o1, GlucoseReading o2) {
return o1.getCreated().compareTo(o2.getCreated());
}
});
for (int i = 0; i < glucoseReadings.size(); i++){
GlucoseReading glucoseReading = glucoseReadings.get(i);
finalGraphObjects.add(new IntGraphObject(new DateTime(glucoseReading.getCreated()), glucoseReading.getReading()));
}
}
//add last zeros till now
addZeroReadings(finalGraphObjects, startDate, DateTime.now());

return finalGraphObjects;
}
Expand All @@ -282,14 +293,6 @@ private void addZeroReadings(final ArrayList<IntGraphObject> graphObjects,
}
}

public ArrayList<String> getGlucoseType() {
return glucoseType;
}

public ArrayList<String> getGlucoseDatetime() {
return dB.getGlucoseDateTimeAsArray();
}

public ArrayList<String> getGraphGlucoseDateTime(){
ArrayList<String> glucoseDatetime = new ArrayList<>();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,5 @@
<string name="activity_backup_drive_button_open_drive">Manage on Drive</string>
<string name="mmol_mol">mmol/mol</string>
<string name="mmol_L">mmol/L</string>
<string name="preferences_graph_old">Revert to old graph</string>
</resources>
13 changes: 9 additions & 4 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
android:dialogTitle="@string/helloactivity_country"
android:key="pref_country"
android:title="@string/helloactivity_country" />
<ListPreference
android:dialogTitle="@string/helloactivity_language"
android:key="pref_language"
android:title="@string/helloactivity_language" />
<EditTextPreference
android:dialogTitle="@string/helloactivity_age"
android:inputType="number"
Expand Down Expand Up @@ -78,6 +74,11 @@
android:inputType="number"
android:key="pref_range_max"
android:title="@string/helloactivity_preferred_range_max" />
<SwitchPreference
android:id="@+id/preferences_graph_old"
android:defaultValue="false"
android:key="pref_graph_old"
android:title="@string/preferences_graph_old" />
<SwitchPreference
android:id="@+id/preferences_font_dyslexia"
android:defaultValue="false"
Expand All @@ -89,4 +90,8 @@
<Preference
android:key="about_settings"
android:title="@string/preferences_about_glucosio" />
<ListPreference
android:dialogTitle="@string/helloactivity_language"
android:key="pref_language"
android:title="@string/helloactivity_language" />
</PreferenceScreen>
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void ShouldAddZerosBetweenReadings_WhenAsked() throws Exception {
new GlucoseReading(21, "test", now.toDate(), ""))
);

presenter.loadDatabase();
presenter.loadDatabase(true);

final List<Integer> readings = presenter.getGlucoseReadings();
DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15);
Expand All @@ -66,7 +66,7 @@ public void ShouldSortReadingsChronologically_WhenAsked() throws Exception {
new GlucoseReading(11, "test", twoDaysAgo.toDate(), ""))
);

presenter.loadDatabase();
presenter.loadDatabase(true);

final List<Integer> readings = presenter.getGlucoseReadings();
assertThat(readings).containsSequence(11, 0, 33);
Expand Down

0 comments on commit e1b406b

Please sign in to comment.