mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-01-29 02:57:32 +01:00
Improve balance handling
This commit is contained in:
parent
4592d35cc6
commit
88ad6cf0e0
@ -33,6 +33,7 @@ import com.github.mikephil.charting.components.YAxis;
|
|||||||
import com.github.mikephil.charting.data.BarData;
|
import com.github.mikephil.charting.data.BarData;
|
||||||
import com.github.mikephil.charting.data.BarDataSet;
|
import com.github.mikephil.charting.data.BarDataSet;
|
||||||
import com.github.mikephil.charting.data.BarEntry;
|
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.PieData;
|
||||||
import com.github.mikephil.charting.data.PieDataSet;
|
import com.github.mikephil.charting.data.PieDataSet;
|
||||||
import com.github.mikephil.charting.data.PieEntry;
|
import com.github.mikephil.charting.data.PieEntry;
|
||||||
@ -60,8 +61,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
|
|||||||
protected final int TOTAL_DAYS = 7;
|
protected final int TOTAL_DAYS = 7;
|
||||||
|
|
||||||
private Locale mLocale;
|
private Locale mLocale;
|
||||||
protected int mTargetValue = 0;
|
private int mTargetValue = 0;
|
||||||
protected long mBalance = 0;
|
|
||||||
|
|
||||||
private PieChart mTodayPieChart;
|
private PieChart mTodayPieChart;
|
||||||
private BarChart mWeekChart;
|
private BarChart mWeekChart;
|
||||||
@ -75,7 +75,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
|
|||||||
day.setTime(chartsHost.getEndDate());
|
day.setTime(chartsHost.getEndDate());
|
||||||
//NB: we could have omitted the day, but this way we can move things to the past easily
|
//NB: we could have omitted the day, but this way we can move things to the past easily
|
||||||
DayData dayData = refreshDayPie(db, day, device);
|
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);
|
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(null); // workaround for https://github.com/PhilJay/MPAndroidChart/issues/2317
|
||||||
mWeekChart.setData(mcd.getWeekBeforeData().getData());
|
mWeekChart.setData(mcd.getWeekBeforeData().getData());
|
||||||
mWeekChart.getXAxis().setValueFormatter(mcd.getWeekBeforeData().getXValueFormatter());
|
mWeekChart.getXAxis().setValueFormatter(mcd.getWeekBeforeData().getXValueFormatter());
|
||||||
|
|
||||||
|
mBalanceView.setText(mcd.getWeekBeforeData().getBalanceMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void renderCharts() {
|
protected void renderCharts() {
|
||||||
mWeekChart.invalidate();
|
mWeekChart.invalidate();
|
||||||
mTodayPieChart.invalidate();
|
mTodayPieChart.invalidate();
|
||||||
mBalanceView.setText(getBalance());
|
// mBalanceView.setText(getBalanceMessage(balance));
|
||||||
}
|
}
|
||||||
|
|
||||||
private DefaultChartsData<BarData> refreshWeekBeforeData(DBHandler db, BarChart barChart, Calendar day, GBDevice device) {
|
private WeekChartsData<BarData> refreshWeekBeforeData(DBHandler db, BarChart barChart, Calendar day, GBDevice device) {
|
||||||
day = (Calendar) day.clone(); // do not modify the caller's argument
|
day = (Calendar) day.clone(); // do not modify the caller's argument
|
||||||
day.add(Calendar.DATE, -TOTAL_DAYS);
|
day.add(Calendar.DATE, -TOTAL_DAYS);
|
||||||
List<BarEntry> entries = new ArrayList<>();
|
List<BarEntry> entries = new ArrayList<>();
|
||||||
ArrayList<String> labels = new ArrayList<String>();
|
ArrayList<String> labels = new ArrayList<String>();
|
||||||
|
|
||||||
|
int balance = 0;
|
||||||
for (int counter = 0; counter < TOTAL_DAYS; counter++) {
|
for (int counter = 0; counter < TOTAL_DAYS; counter++) {
|
||||||
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
|
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
|
||||||
|
|
||||||
|
balance += calculateBalance(amounts);
|
||||||
entries.add(new BarEntry(counter, getTotalsForActivityAmounts(amounts)));
|
entries.add(new BarEntry(counter, getTotalsForActivityAmounts(amounts)));
|
||||||
labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale));
|
labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale));
|
||||||
day.add(Calendar.DATE, 1);
|
day.add(Calendar.DATE, 1);
|
||||||
@ -126,7 +130,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
|
|||||||
barChart.getAxisLeft().removeAllLimitLines();
|
barChart.getAxisLeft().removeAllLimitLines();
|
||||||
barChart.getAxisLeft().addLimitLine(target);
|
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) {
|
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 static class MyChartsData extends ChartsData {
|
||||||
private final DefaultChartsData<BarData> weekBeforeData;
|
private final WeekChartsData<BarData> weekBeforeData;
|
||||||
private final DayData dayData;
|
private final DayData dayData;
|
||||||
|
|
||||||
MyChartsData(DayData dayData, DefaultChartsData<BarData> weekBeforeData) {
|
MyChartsData(DayData dayData, WeekChartsData<BarData> weekBeforeData) {
|
||||||
this.dayData = dayData;
|
this.dayData = dayData;
|
||||||
this.weekBeforeData = weekBeforeData;
|
this.weekBeforeData = weekBeforeData;
|
||||||
}
|
}
|
||||||
@ -283,7 +287,7 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
|
|||||||
return dayData;
|
return dayData;
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultChartsData<BarData> getWeekBeforeData() {
|
WeekChartsData<BarData> getWeekBeforeData() {
|
||||||
return weekBeforeData;
|
return weekBeforeData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -331,5 +335,20 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
|
|||||||
|
|
||||||
abstract String getPieDescription(int targetValue);
|
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<T extends ChartData<?>> extends DefaultChartsData<T> {
|
||||||
|
private final String balanceMessage;
|
||||||
|
|
||||||
|
public WeekChartsData(T data, PreformattedXIndexLabelFormatter xIndexLabelFormatter, String balanceMessage) {
|
||||||
|
super(data, xIndexLabelFormatter);
|
||||||
|
this.balanceMessage = balanceMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBalanceMessage() {
|
||||||
|
return balanceMessage;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,12 +58,23 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
|
|||||||
return -12;
|
return -12;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String getBalance() {
|
protected int calculateBalance(ActivityAmounts activityAmounts) {
|
||||||
final long balance = this.mBalance;
|
long balance = 0;
|
||||||
this.mBalance = 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) {
|
if (balance > 0) {
|
||||||
final long totalBalance = balance - (mTargetValue * TOTAL_DAYS);
|
final long totalBalance = balance - (targetValue * TOTAL_DAYS);
|
||||||
if (totalBalance > 0)
|
if (totalBalance > 0)
|
||||||
return getString(R.string.overslept, getHM((int) totalBalance));
|
return getString(R.string.overslept, getHM((int) totalBalance));
|
||||||
else
|
else
|
||||||
@ -85,7 +96,6 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
|
|||||||
}
|
}
|
||||||
int totalMinutesDeepSleep = (int) (totalSecondsDeepSleep / 60);
|
int totalMinutesDeepSleep = (int) (totalSecondsDeepSleep / 60);
|
||||||
int totalMinutesLightSleep = (int) (totalSecondsLightSleep / 60);
|
int totalMinutesLightSleep = (int) (totalSecondsLightSleep / 60);
|
||||||
mBalance = mBalance + totalMinutesDeepSleep + totalMinutesLightSleep;
|
|
||||||
return new float[]{totalMinutesDeepSleep, totalMinutesLightSleep};
|
return new float[]{totalMinutesDeepSleep, totalMinutesLightSleep};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,8 +165,6 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getHM(int value) {
|
private String getHM(int value) {
|
||||||
int hours = value / 60;
|
return DateTimeUtils.formatDurationHoursMinutes(value, TimeUnit.MINUTES);
|
||||||
int minutes = value % 60;
|
|
||||||
return String.format("%d:%02d", hours, minutes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,12 +53,19 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment {
|
|||||||
int totalSteps = 0;
|
int totalSteps = 0;
|
||||||
for (ActivityAmount amount : activityAmounts.getAmounts()) {
|
for (ActivityAmount amount : activityAmounts.getAmounts()) {
|
||||||
totalSteps += amount.getTotalSteps();
|
totalSteps += amount.getTotalSteps();
|
||||||
amount.getTotalSteps();
|
|
||||||
mBalance = mBalance + totalSteps;
|
|
||||||
}
|
}
|
||||||
return new float[]{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
|
@Override
|
||||||
protected String formatPieValue(int value) {
|
protected String formatPieValue(int value) {
|
||||||
return String.valueOf(value);
|
return String.valueOf(value);
|
||||||
@ -96,11 +103,9 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String getBalance() {
|
protected String getBalanceMessage(int balance, int targetValue) {
|
||||||
final long balance = this.mBalance;
|
|
||||||
this.mBalance = 0;
|
|
||||||
if (balance > 0) {
|
if (balance > 0) {
|
||||||
final long totalBalance = balance - (mTargetValue * TOTAL_DAYS);
|
final long totalBalance = balance - (targetValue * TOTAL_DAYS);
|
||||||
if (totalBalance > 0)
|
if (totalBalance > 0)
|
||||||
return getString(R.string.overstep, (int) Math.abs(totalBalance));
|
return getString(R.string.overstep, (int) Math.abs(totalBalance));
|
||||||
else
|
else
|
||||||
|
@ -392,7 +392,7 @@
|
|||||||
|
|
||||||
<string name="liveactivity_live_activity">Live activity</string>
|
<string name="liveactivity_live_activity">Live activity</string>
|
||||||
<string name="weeksteps_today_steps_description">Steps today, target: %1$s</string>
|
<string name="weeksteps_today_steps_description">Steps today, target: %1$s</string>
|
||||||
<string name="lack_of_step">Lack of step: %1$d</string>
|
<string name="lack_of_step">Lack of steps: %1$d</string>
|
||||||
<string name="overstep">Overstep: %1$d</string>
|
<string name="overstep">Overstep: %1$d</string>
|
||||||
<string name="pref_title_dont_ack_transfer">Do not ACK activity data transfer</string>
|
<string name="pref_title_dont_ack_transfer">Do not ACK activity data transfer</string>
|
||||||
<string name="pref_summary_dont_ack_transfers">If the activity data are not acked to the band, they will not be cleared. Useful if GB is used together with other apps.</string>
|
<string name="pref_summary_dont_ack_transfers">If the activity data are not acked to the band, they will not be cleared. Useful if GB is used together with other apps.</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user