1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 03:16:51 +01:00

Calculation and display of weekly balance by steps and time of sleep.

This commit is contained in:
Pavel Elagin 2018-09-06 16:07:48 +03:00 committed by cpfeiffer
parent bf80474b71
commit 4592d35cc6
7 changed files with 85 additions and 15 deletions

View File

@ -23,6 +23,7 @@ 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.PieChart;
@ -56,12 +57,15 @@ import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue;
public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
protected static final Logger LOG = LoggerFactory.getLogger(AbstractWeekChartFragment.class);
protected final int TOTAL_DAYS = 7;
private Locale mLocale;
private int mTargetValue = 0;
protected int mTargetValue = 0;
protected long mBalance = 0;
private PieChart mTodayPieChart;
private BarChart mWeekChart;
private TextView mBalanceView;
private int mOffsetHours = getOffsetHours();
@ -93,15 +97,16 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
protected void renderCharts() {
mWeekChart.invalidate();
mTodayPieChart.invalidate();
mBalanceView.setText(getBalance());
}
private DefaultChartsData<BarData> refreshWeekBeforeData(DBHandler db, BarChart barChart, Calendar day, GBDevice device) {
day = (Calendar) day.clone(); // do not modify the caller's argument
day.add(Calendar.DATE, -7);
day.add(Calendar.DATE, -TOTAL_DAYS);
List<BarEntry> entries = new ArrayList<>();
ArrayList<String> labels = new ArrayList<String>();
for (int counter = 0; counter < 7; counter++) {
for (int counter = 0; counter < TOTAL_DAYS; counter++) {
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
entries.add(new BarEntry(counter, getTotalsForActivityAmounts(amounts)));
@ -177,8 +182,9 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
mTargetValue = goal;
}
mTodayPieChart = (PieChart) rootView.findViewById(R.id.todaystepschart);
mWeekChart = (BarChart) rootView.findViewById(R.id.weekstepschart);
mTodayPieChart = rootView.findViewById(R.id.todaystepschart);
mWeekChart = rootView.findViewById(R.id.weekstepschart);
mBalanceView = rootView.findViewById(R.id.balance);
setupWeekChart();
setupTodayPieChart();
@ -324,4 +330,6 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
abstract int[] getColors();
abstract String getPieDescription(int targetValue);
abstract String getBalance();
}

View File

@ -58,6 +58,20 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
return -12;
}
@Override
String getBalance() {
final long balance = this.mBalance;
this.mBalance = 0;
if (balance > 0) {
final long totalBalance = balance - (mTargetValue * TOTAL_DAYS);
if (totalBalance > 0)
return getString(R.string.overslept, getHM((int) totalBalance));
else
return getString(R.string.lack_of_sleep, getHM((int) Math.abs(totalBalance)));
} else
return getString(R.string.no_data);
}
@Override
float[] getTotalsForActivityAmounts(ActivityAmounts activityAmounts) {
long totalSecondsDeepSleep = 0;
@ -69,7 +83,10 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
totalSecondsLightSleep += amount.getTotalSeconds();
}
}
return new float[]{(int) (totalSecondsDeepSleep / 60), (int) (totalSecondsLightSleep / 60)};
int totalMinutesDeepSleep = (int) (totalSecondsDeepSleep / 60);
int totalMinutesLightSleep = (int) (totalSecondsLightSleep / 60);
mBalance = mBalance + totalMinutesDeepSleep + totalMinutesLightSleep;
return new float[]{totalMinutesDeepSleep, totalMinutesLightSleep};
}
@Override
@ -136,4 +153,10 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
chart.getLegend().setWordWrapEnabled(true);
chart.getLegend().setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
}
private String getHM(int value) {
int hours = value / 60;
int minutes = value % 60;
return String.format("%d:%02d", hours, minutes);
}
}

View File

@ -54,6 +54,7 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment {
for (ActivityAmount amount : activityAmounts.getAmounts()) {
totalSteps += amount.getTotalSteps();
amount.getTotalSteps();
mBalance = mBalance + totalSteps;
}
return new float[]{totalSteps};
}
@ -93,4 +94,18 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment {
// no legend here, it is all about the steps here
chart.getLegend().setEnabled(false);
}
@Override
String getBalance() {
final long balance = this.mBalance;
this.mBalance = 0;
if (balance > 0) {
final long totalBalance = balance - (mTargetValue * TOTAL_DAYS);
if (totalBalance > 0)
return getString(R.string.overstep, (int) Math.abs(totalBalance));
else
return getString(R.string.lack_of_step, (int) Math.abs(totalBalance));
} else
return getString(R.string.no_data);
}
}

View File

@ -4,18 +4,28 @@
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity$PlaceholderFragment"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="40"
android:orientation="vertical">
<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="40">
</com.github.mikephil.charting.charts.PieChart>
android:layout_weight="40" />
</LinearLayout>
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/weekstepschart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />
<!--<TextView-->

View File

@ -5,17 +5,21 @@
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity$PlaceholderFragment"
android:orientation="vertical">
<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.PieChart>
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>

View File

@ -253,6 +253,8 @@
<string name="weeksleepchart_today_sleep_description">Сон сегодня, цель: %1$s</string>
<string name="weekstepschart_steps_a_week">Шагов в неделю</string>
<string name="activity_sleepchart_activity_and_sleep">Ваши активность и сон</string>
<string name="lack_of_sleep">Недосып: %1$s</string>
<string name="overslept">Пересып: %1$s</string>
<string name="updating_firmware">Обновление прошивки…</string>
<string name="fwapp_install_device_not_ready">Файл не может быть установлен, устройство не готово.</string>
<string name="installhandler_firmware_name">%1$s: %2$s %3$s</string>
@ -273,6 +275,8 @@
<string name="distance">Расстояние</string>
<string name="liveactivity_live_activity">Жизненная активность</string>
<string name="weeksteps_today_steps_description">Шагов сегодня, цель: %1$s</string>
<string name="lack_of_step">Недопройдено %1$d шагов.</string>
<string name="overstep">Пройдено на %1$d шагов больше.</string>
<string name="pref_title_dont_ack_transfer">Не подтверждать передачу данных об активности</string>
<string name="pref_summary_dont_ack_transfers">Если данные об активности не будут переданы на устройство, оно не будет очищено. Полезно, если GB используется с другими приложениями.</string>
<string name="pref_summary_keep_data_on_device">Хранить данные о деятельности на Mi Band, даже после синхронизации. Полезно, если Mi Band используется совместно с другими приложениями.</string>
@ -600,6 +604,7 @@
<string name="share_log">Поделиться лог-файлом</string>
<string name="share_log_warning">Файлы журнала Gadgetbridge могут содержать личную информацию, например уникальные идентификаторы: (устройство, MAC-адрес), предпочтения в музыке и т.д. Рассмотрите возможность редактирования файла и удаления этой информации перед отправкой файла общедоступное место.</string>
<string name="warning">Внимание!</string>
<string name="no_data">Нет данных</string>
<string name="pref_title_notifications_timeout">Минимальный интервал между уведомлениями</string>
<string name="no_limit">Без ограничений</string>

View File

@ -356,6 +356,8 @@
<string name="weeksleepchart_today_sleep_description">Sleep today, target: %1$s</string>
<string name="weekstepschart_steps_a_week">Steps per week</string>
<string name="activity_sleepchart_activity_and_sleep">Your activity and sleep</string>
<string name="lack_of_sleep">Lack of sleep: %1$s</string>
<string name="overslept">Overslept: %1$s</string>
<string name="updating_firmware">Flashing firmware…</string>
<string name="fwapp_install_device_not_ready">File cannot be installed, device not ready.</string>
<string name="installhandler_firmware_name">%1$s: %2$s %3$s</string>
@ -390,6 +392,8 @@
<string name="liveactivity_live_activity">Live activity</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="overstep">Overstep: %1$d</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_keep_data_on_device">Will keep activity data on the Mi Band even after synchronization. Useful if GB is used together with other apps.</string>
@ -652,6 +656,7 @@
<string name="share_log">Share log</string>
<string name="share_log_warning">Please keep in mind Gadgetbridge log files may contain lots of personal information, including but not limited to health data, unique identifiers (such as a device MAC address), music preferences, etc. Consider editing the file and removing this information before sending the file to a public issue report.</string>
<string name="warning">Warning!</string>
<string name="no_data">No data</string>
<!-- LED Color -->
<string name="preferences_led_color">LED Color</string>