diff --git a/app/src/main/java/org/glucosio/android/activity/PreferencesActivity.java b/app/src/main/java/org/glucosio/android/activity/PreferencesActivity.java index e66ab11b..8e2c70bb 100644 --- a/app/src/main/java/org/glucosio/android/activity/PreferencesActivity.java +++ b/app/src/main/java/org/glucosio/android/activity/PreferencesActivity.java @@ -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(); diff --git a/app/src/main/java/org/glucosio/android/db/DatabaseHandler.java b/app/src/main/java/org/glucosio/android/db/DatabaseHandler.java index 3dafd3a2..667efe9e 100644 --- a/app/src/main/java/org/glucosio/android/db/DatabaseHandler.java +++ b/app/src/main/java/org/glucosio/android/db/DatabaseHandler.java @@ -310,7 +310,7 @@ public GlucoseReading getGlucoseReadingById(long id) { List gReadings; ArrayList readings = new ArrayList(); - gReadings = GlucoseReading.getGlucoseReadings(whereString); + gReadings = GlucoseReading.getGlucoseReadingsWithZeros(whereString); int i; for (i=0; i < gReadings.size(); i++){ readings.add(gReadings.get(i).getGlucoseReading()); diff --git a/app/src/main/java/org/glucosio/android/fragment/OverviewFragment.java b/app/src/main/java/org/glucosio/android/fragment/OverviewFragment.java index dfd90ffb..4c13a9da 100644 --- a/app/src/main/java/org/glucosio/android/fragment/OverviewFragment.java +++ b/app/src/main/java/org/glucosio/android/fragment/OverviewFragment.java @@ -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; @@ -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); @@ -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); @@ -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()); diff --git a/app/src/main/java/org/glucosio/android/presenter/OverviewPresenter.java b/app/src/main/java/org/glucosio/android/presenter/OverviewPresenter.java index b8df8159..ed3a516e 100644 --- a/app/src/main/java/org/glucosio/android/presenter/OverviewPresenter.java +++ b/app/src/main/java/org/glucosio/android/presenter/OverviewPresenter.java @@ -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(); @@ -240,35 +240,46 @@ public ArrayList getCholesterolReadingsDateTime(){ return dB.getCholesterolDateTimeAsArray(); } - private List generateGlucoseGraphPoints() { - DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15); - final List glucoseReadings = dB.getLastMonthGlucoseReadings(); - - Collections.sort(glucoseReadings, new Comparator() { - 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 generateGlucoseGraphPoints(boolean isNewGraphEnabled) { final ArrayList finalGraphObjects = new ArrayList<>(); - // Transfer values from database to ArrayList as GlucoseGraphObjects - for (int i=0; i glucoseReadings = dB.getLastMonthGlucoseReadings(); + + Collections.sort(glucoseReadings, new Comparator() { + 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() { + 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; } @@ -282,14 +293,6 @@ private void addZeroReadings(final ArrayList graphObjects, } } - public ArrayList getGlucoseType() { - return glucoseType; - } - - public ArrayList getGlucoseDatetime() { - return dB.getGlucoseDateTimeAsArray(); - } - public ArrayList getGraphGlucoseDateTime(){ ArrayList glucoseDatetime = new ArrayList<>(); DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm"); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 72f15721..0112399b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -437,4 +437,5 @@ Manage on Drive mmol/mol mmol/L + Revert to old graph diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index a7c33b4d..a89a9830 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -23,10 +23,6 @@ android:dialogTitle="@string/helloactivity_country" android:key="pref_country" android:title="@string/helloactivity_country" /> - + + \ No newline at end of file diff --git a/app/src/test/java/org/glucosio/android/presenter/OverviewPresenterTest.java b/app/src/test/java/org/glucosio/android/presenter/OverviewPresenterTest.java index ab38d752..5e9410ab 100644 --- a/app/src/test/java/org/glucosio/android/presenter/OverviewPresenterTest.java +++ b/app/src/test/java/org/glucosio/android/presenter/OverviewPresenterTest.java @@ -48,7 +48,7 @@ public void ShouldAddZerosBetweenReadings_WhenAsked() throws Exception { new GlucoseReading(21, "test", now.toDate(), "")) ); - presenter.loadDatabase(); + presenter.loadDatabase(true); final List readings = presenter.getGlucoseReadings(); DateTime minDateTime = DateTime.now().minusMonths(1).minusDays(15); @@ -66,7 +66,7 @@ public void ShouldSortReadingsChronologically_WhenAsked() throws Exception { new GlucoseReading(11, "test", twoDaysAgo.toDate(), "")) ); - presenter.loadDatabase(); + presenter.loadDatabase(true); final List readings = presenter.getGlucoseReadings(); assertThat(readings).containsSequence(11, 0, 33);