Restore steps balance on weekly and monthly charts

This commit is contained in:
José Rebelo 2025-01-04 13:46:33 +00:00
parent d432800ae4
commit ef5f4d9fd0
5 changed files with 45 additions and 88 deletions

View File

@ -19,12 +19,6 @@ package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import android.app.Activity; import android.app.Activity;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.LimitLine;
@ -68,9 +62,8 @@ public abstract class AbstractWeekChartFragment extends AbstractActivityChartFra
protected int mTargetValue = 0; protected int mTargetValue = 0;
protected BarChart mWeekChart; protected BarChart mWeekChart;
protected TextView mBalanceView;
private int mOffsetHours = getOffsetHours(); private final int mOffsetHours = getOffsetHours();
protected String getWeeksChartsLabel(Calendar day){ protected String getWeeksChartsLabel(Calendar day){
if (TOTAL_DAYS > 7) { if (TOTAL_DAYS > 7) {
@ -210,35 +203,6 @@ public abstract class AbstractWeekChartFragment extends AbstractActivityChartFra
return lineDataSet; return lineDataSet;
}; };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mLocale = getResources().getConfiguration().locale;
View rootView = inflater.inflate(R.layout.fragment_weeksteps_chart, container, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
rootView.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
getChartsHost().enableSwipeRefresh(scrollY == 0);
});
}
final int goal = getGoal();
if (goal >= 0) {
mTargetValue = goal;
}
mWeekChart = rootView.findViewById(R.id.weekstepschart);
mBalanceView = rootView.findViewById(R.id.balance);
setupWeekChart();
// refresh immediately instead of use refreshIfVisible(), for perceived performance
refresh();
return rootView;
}
protected void setupWeekChart() { protected void setupWeekChart() {
mWeekChart.setBackgroundColor(BACKGROUND_COLOR); mWeekChart.setBackgroundColor(BACKGROUND_COLOR);
mWeekChart.getDescription().setTextColor(DESCRIPTION_COLOR); mWeekChart.getDescription().setTextColor(DESCRIPTION_COLOR);

View File

@ -1,5 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.activities.charts; package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -46,6 +47,8 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
private TextView distanceTotal; private TextView distanceTotal;
private BarChart stepsChart; private BarChart stepsChart;
private TextView mBalanceView;
protected int CHART_TEXT_COLOR; protected int CHART_TEXT_COLOR;
protected int TEXT_COLOR; protected int TEXT_COLOR;
protected int STEPS_GOAL; protected int STEPS_GOAL;
@ -53,7 +56,7 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
protected int BACKGROUND_COLOR; protected int BACKGROUND_COLOR;
protected int DESCRIPTION_COLOR; protected int DESCRIPTION_COLOR;
public static StepsPeriodFragment newInstance ( int totalDays ) { public static StepsPeriodFragment newInstance(int totalDays) {
StepsPeriodFragment fragmentFirst = new StepsPeriodFragment(); StepsPeriodFragment fragmentFirst = new StepsPeriodFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putInt("totalDays", totalDays); args.putInt("totalDays", totalDays);
@ -84,6 +87,9 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
stepsTotal = rootView.findViewById(R.id.steps_total); stepsTotal = rootView.findViewById(R.id.steps_total);
distanceTotal = rootView.findViewById(R.id.distance_total); distanceTotal = rootView.findViewById(R.id.distance_total);
STEPS_GOAL = GBApplication.getPrefs().getInt(ActivityUser.PREF_USER_STEPS_GOAL, ActivityUser.defaultUserStepsGoal); STEPS_GOAL = GBApplication.getPrefs().getInt(ActivityUser.PREF_USER_STEPS_GOAL, ActivityUser.defaultUserStepsGoal);
mBalanceView = rootView.findViewById(R.id.balance);
setupStepsChart(); setupStepsChart();
refresh(); refresh();
@ -126,7 +132,7 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
yAxisRight.setDrawAxisLine(true); yAxisRight.setDrawAxisLine(true);
} }
@Override @Override
public String getTitle() { public String getTitle() {
return getString(R.string.steps); return getString(R.string.steps);
} }
@ -151,7 +157,7 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
@Override @Override
protected void updateChartsnUIThread(StepsData stepsData) { protected void updateChartsnUIThread(StepsData stepsData) {
Date to = new Date((long) getTSEnd() * 1000); Date to = new Date((long) getTSEnd() * 1000);
Date from = DateUtils.addDays(to,-(TOTAL_DAYS - 1)); Date from = DateUtils.addDays(to, -(TOTAL_DAYS - 1));
String toFormattedDate = new SimpleDateFormat("E, MMM dd").format(to); String toFormattedDate = new SimpleDateFormat("E, MMM dd").format(to);
String fromFormattedDate = new SimpleDateFormat("E, MMM dd").format(from); String fromFormattedDate = new SimpleDateFormat("E, MMM dd").format(from);
mDateView.setText(fromFormattedDate + " - " + toFormattedDate); mDateView.setText(fromFormattedDate + " - " + toFormattedDate);
@ -160,7 +166,7 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
List<BarEntry> entries = new ArrayList<>(); List<BarEntry> entries = new ArrayList<>();
int counter = 0; int counter = 0;
for(StepsDay day : stepsData.days) { for (StepsDay day : stepsData.days) {
entries.add(new BarEntry(counter, day.steps)); entries.add(new BarEntry(counter, day.steps));
counter++; counter++;
} }
@ -183,6 +189,8 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
distanceAvg.setText(valueFormatter.formatValue(stepsData.distanceDailyAvg, "km")); distanceAvg.setText(valueFormatter.formatValue(stepsData.distanceDailyAvg, "km"));
stepsTotal.setText(String.format(String.valueOf(stepsData.totalSteps))); stepsTotal.setText(String.format(String.valueOf(stepsData.totalSteps)));
distanceTotal.setText(valueFormatter.formatValue(stepsData.totalDistance, "km")); distanceTotal.setText(valueFormatter.formatValue(stepsData.totalDistance, "km"));
mBalanceView.setText(stepsData.getBalanceMessage(getContext(), STEPS_GOAL));
} }
ValueFormatter getStepsChartDayValueFormatter(StepsPeriodFragment.StepsData stepsData) { ValueFormatter getStepsChartDayValueFormatter(StepsPeriodFragment.StepsData stepsData) {
@ -202,7 +210,8 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
stepsChart.invalidate(); stepsChart.invalidate();
} }
protected void setupLegend(Chart<?> chart) {} protected void setupLegend(Chart<?> chart) {
}
protected static class StepsData extends ChartsData { protected static class StepsData extends ChartsData {
List<StepsDay> days; List<StepsDay> days;
@ -211,10 +220,11 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
long totalSteps = 0; long totalSteps = 0;
double totalDistance = 0; double totalDistance = 0;
StepsDay todayStepsDay; StepsDay todayStepsDay;
protected StepsData(List<StepsDay> days) { protected StepsData(List<StepsDay> days) {
this.days = days; this.days = days;
int daysCounter = 0; int daysCounter = 0;
for(StepsDay day : days) { for (StepsDay day : days) {
this.totalSteps += day.steps; this.totalSteps += day.steps;
this.totalDistance += day.distance; this.totalDistance += day.distance;
if (day.steps > 0) { if (day.steps > 0) {
@ -227,5 +237,19 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
} }
this.todayStepsDay = days.get(days.size() - 1); this.todayStepsDay = days.get(days.size() - 1);
} }
protected String getBalanceMessage(final Context context, final int targetValue) {
if (totalSteps == 0) {
return context.getString(R.string.no_data);
}
final long totalBalance = totalSteps - ((long) targetValue * days.size());
if (totalBalance > 0) {
return context.getString(R.string.overstep, Math.abs(totalBalance));
} else {
return context.getString(R.string.lack_of_step, Math.abs(totalBalance));
}
}
} }
} }

View File

@ -67,6 +67,8 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
private LinearLayout sleepScoreWrapper; private LinearLayout sleepScoreWrapper;
private LineChart sleepScoreChart; private LineChart sleepScoreChart;
private TextView mBalanceView;
public static WeekSleepChartFragment newInstance ( int totalDays ) { public static WeekSleepChartFragment newInstance ( int totalDays ) {
WeekSleepChartFragment fragmentFirst = new WeekSleepChartFragment(); WeekSleepChartFragment fragmentFirst = new WeekSleepChartFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();

View File

@ -20,10 +20,21 @@
android:gravity="center" android:gravity="center"
android:textSize="20sp" android:textSize="20sp"
android:layout_marginTop="15dp" android:layout_marginTop="15dp"
android:layout_marginBottom="20dp" android:layout_marginBottom="10dp"
/> />
</RelativeLayout> </RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center">
<TextView
android:id="@+id/balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="250sp" android:layout_height="250sp"

View File

@ -1,44 +0,0 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ActivityChartsActivity$PlaceholderFragment">
<TextView
android:id="@+id/balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/todaystepschart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/weekstepschart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />
</LinearLayout>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/steps_streaks_button"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:visibility="visible"
app:srcCompat="@drawable/ic_events" />
</RelativeLayout>