From 88ad6cf0e0eaf2225e913063060790a5ca2a83a4 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Mon, 10 Sep 2018 23:38:49 +0200 Subject: [PATCH] Improve balance handling --- .../charts/AbstractWeekChartFragment.java | 39 ++++++++++++++----- .../charts/WeekSleepChartFragment.java | 24 ++++++++---- .../charts/WeekStepsChartFragment.java | 17 +++++--- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java index 1fc3dce75..c6b07dee4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java @@ -33,6 +33,7 @@ import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; +import com.github.mikephil.charting.data.ChartData; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import com.github.mikephil.charting.data.PieEntry; @@ -60,8 +61,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { protected final int TOTAL_DAYS = 7; private Locale mLocale; - protected int mTargetValue = 0; - protected long mBalance = 0; + private int mTargetValue = 0; private PieChart mTodayPieChart; private BarChart mWeekChart; @@ -75,7 +75,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { day.setTime(chartsHost.getEndDate()); //NB: we could have omitted the day, but this way we can move things to the past easily DayData dayData = refreshDayPie(db, day, device); - DefaultChartsData weekBeforeData = refreshWeekBeforeData(db, mWeekChart, day, device); + WeekChartsData weekBeforeData = refreshWeekBeforeData(db, mWeekChart, day, device); return new MyChartsData(dayData, weekBeforeData); } @@ -91,24 +91,28 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { mWeekChart.setData(null); // workaround for https://github.com/PhilJay/MPAndroidChart/issues/2317 mWeekChart.setData(mcd.getWeekBeforeData().getData()); mWeekChart.getXAxis().setValueFormatter(mcd.getWeekBeforeData().getXValueFormatter()); + + mBalanceView.setText(mcd.getWeekBeforeData().getBalanceMessage()); } @Override protected void renderCharts() { mWeekChart.invalidate(); mTodayPieChart.invalidate(); - mBalanceView.setText(getBalance()); +// mBalanceView.setText(getBalanceMessage(balance)); } - private DefaultChartsData refreshWeekBeforeData(DBHandler db, BarChart barChart, Calendar day, GBDevice device) { + private WeekChartsData refreshWeekBeforeData(DBHandler db, BarChart barChart, Calendar day, GBDevice device) { day = (Calendar) day.clone(); // do not modify the caller's argument day.add(Calendar.DATE, -TOTAL_DAYS); List entries = new ArrayList<>(); ArrayList labels = new ArrayList(); + int balance = 0; for (int counter = 0; counter < TOTAL_DAYS; counter++) { ActivityAmounts amounts = getActivityAmountsForDay(db, day, device); + balance += calculateBalance(amounts); entries.add(new BarEntry(counter, getTotalsForActivityAmounts(amounts))); labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale)); day.add(Calendar.DATE, 1); @@ -126,7 +130,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { barChart.getAxisLeft().removeAllLimitLines(); barChart.getAxisLeft().addLimitLine(target); - return new DefaultChartsData(barData, new PreformattedXIndexLabelFormatter(labels)); + return new WeekChartsData(barData, new PreformattedXIndexLabelFormatter(labels), getBalanceMessage(balance, mTargetValue)); } private DayData refreshDayPie(DBHandler db, Calendar day, GBDevice device) { @@ -271,10 +275,10 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { } private static class MyChartsData extends ChartsData { - private final DefaultChartsData weekBeforeData; + private final WeekChartsData weekBeforeData; private final DayData dayData; - MyChartsData(DayData dayData, DefaultChartsData weekBeforeData) { + MyChartsData(DayData dayData, WeekChartsData weekBeforeData) { this.dayData = dayData; this.weekBeforeData = weekBeforeData; } @@ -283,7 +287,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { return dayData; } - DefaultChartsData getWeekBeforeData() { + WeekChartsData getWeekBeforeData() { return weekBeforeData; } } @@ -331,5 +335,20 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment { abstract String getPieDescription(int targetValue); - abstract String getBalance(); + protected abstract int calculateBalance(ActivityAmounts amounts); + + protected abstract String getBalanceMessage(int balance, int targetValue); + + private class WeekChartsData> extends DefaultChartsData { + private final String balanceMessage; + + public WeekChartsData(T data, PreformattedXIndexLabelFormatter xIndexLabelFormatter, String balanceMessage) { + super(data, xIndexLabelFormatter); + this.balanceMessage = balanceMessage; + } + + public String getBalanceMessage() { + return balanceMessage; + } + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekSleepChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekSleepChartFragment.java index b30952dc4..1d82cd5c8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekSleepChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekSleepChartFragment.java @@ -58,12 +58,23 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment { return -12; } + @Override - String getBalance() { - final long balance = this.mBalance; - this.mBalance = 0; + protected int calculateBalance(ActivityAmounts activityAmounts) { + long balance = 0; + + for (ActivityAmount amount : activityAmounts.getAmounts()) { + if (amount.getActivityKind() == ActivityKind.TYPE_DEEP_SLEEP || amount.getActivityKind() == ActivityKind.TYPE_LIGHT_SLEEP) { + balance += amount.getTotalSeconds(); + } + } + return (int) (balance / 60); + } + + @Override + protected String getBalanceMessage(int balance, int targetValue) { if (balance > 0) { - final long totalBalance = balance - (mTargetValue * TOTAL_DAYS); + final long totalBalance = balance - (targetValue * TOTAL_DAYS); if (totalBalance > 0) return getString(R.string.overslept, getHM((int) totalBalance)); else @@ -85,7 +96,6 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment { } int totalMinutesDeepSleep = (int) (totalSecondsDeepSleep / 60); int totalMinutesLightSleep = (int) (totalSecondsLightSleep / 60); - mBalance = mBalance + totalMinutesDeepSleep + totalMinutesLightSleep; return new float[]{totalMinutesDeepSleep, totalMinutesLightSleep}; } @@ -155,8 +165,6 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment { } private String getHM(int value) { - int hours = value / 60; - int minutes = value % 60; - return String.format("%d:%02d", hours, minutes); + return DateTimeUtils.formatDurationHoursMinutes(value, TimeUnit.MINUTES); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java index 905e31f50..df298ca24 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java @@ -53,12 +53,19 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment { int totalSteps = 0; for (ActivityAmount amount : activityAmounts.getAmounts()) { totalSteps += amount.getTotalSteps(); - amount.getTotalSteps(); - mBalance = mBalance + totalSteps; } return new float[]{totalSteps}; } + @Override + protected int calculateBalance(ActivityAmounts activityAmounts) { + int balance = 0; + for (ActivityAmount amount : activityAmounts.getAmounts()) { + balance += amount.getTotalSteps(); + } + return balance; + } + @Override protected String formatPieValue(int value) { return String.valueOf(value); @@ -96,11 +103,9 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment { } @Override - String getBalance() { - final long balance = this.mBalance; - this.mBalance = 0; + protected String getBalanceMessage(int balance, int targetValue) { if (balance > 0) { - final long totalBalance = balance - (mTargetValue * TOTAL_DAYS); + final long totalBalance = balance - (targetValue * TOTAL_DAYS); if (totalBalance > 0) return getString(R.string.overstep, (int) Math.abs(totalBalance)); else diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d4e5f6135..2e863afcc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -392,7 +392,7 @@ Live activity Steps today, target: %1$s - Lack of step: %1$d + Lack of steps: %1$d Overstep: %1$d Do not ACK activity data transfer If the activity data are not acked to the band, they will not be cleared. Useful if GB is used together with other apps.