1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 20:36:51 +01:00

Add gauge to hrv status fragment page

This commit is contained in:
a0z 2024-09-18 12:58:32 +02:00
parent d238a321df
commit 4a3dc35c19
3 changed files with 114 additions and 38 deletions

View File

@ -18,10 +18,12 @@ package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import static java.util.stream.Collectors.toCollection;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.github.mikephil.charting.charts.Chart;
@ -47,6 +49,8 @@ import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardHrvWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.GaugeDrawer;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
@ -59,6 +63,8 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
protected static final Logger LOG = LoggerFactory.getLogger(HRVStatusFragment.class);
protected final int TOTAL_DAYS = 7;
protected GaugeDrawer gaugeDrawer;
private ImageView mHRVStatusGauge;
private LineChart mWeeklyHRVStatusChart;
private TextView mHRVStatusSevenDaysAvg;
private TextView mHRVStatusSevenDaysAvgStatus; // Balanced, Unbalanced, Low
@ -67,6 +73,8 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
private TextView mHRVStatusDayAvg;
private TextView mHRVStatusBaseline;
private TextView mDateView;
private TextView mHRVGaugeValue;
private TextView mHRVGaugeStatus;
protected int CHART_TEXT_COLOR;
protected int LEGEND_TEXT_COLOR;
protected int TEXT_COLOR;
@ -75,6 +83,12 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_hrv_status, container, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
rootView.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
getChartsHost().enableSwipeRefresh(scrollY == 0);
});
}
mWeeklyHRVStatusChart = rootView.findViewById(R.id.hrv_weekly_line_chart);
mHRVStatusLastNight = rootView.findViewById(R.id.hrv_status_last_night);
mHRVStatusSevenDaysAvg = rootView.findViewById(R.id.hrv_status_seven_days_avg);
@ -83,7 +97,11 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
mHRVStatusDayAvg = rootView.findViewById(R.id.hrv_status_day_avg);
mHRVStatusBaseline = rootView.findViewById(R.id.hrv_status_baseline);
mDateView = rootView.findViewById(R.id.hrv_status_date_view);
mHRVStatusGauge = rootView.findViewById(R.id.hrv_status_gauge_bar);
mHRVGaugeValue = rootView.findViewById(R.id.hrv_gauge_value);
mHRVGaugeStatus = rootView.findViewById(R.id.hrv_gauge_status);
gaugeDrawer = new GaugeDrawer();
setupLineChart();
refresh();
@ -186,24 +204,38 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
case NONE:
mHRVStatusSevenDaysAvgStatus.setText("-");
mHRVStatusSevenDaysAvgStatus.setTextColor(TEXT_COLOR);
mHRVGaugeStatus.setText("");
mHRVGaugeStatus.setTextColor(TEXT_COLOR);
break;
case POOR:
mHRVStatusSevenDaysAvgStatus.setText(getString(R.string.hrv_status_poor));
mHRVStatusSevenDaysAvgStatus.setTextColor(getResources().getColor(R.color.hrv_status_poor));
mHRVGaugeStatus.setText(getString(R.string.hrv_status_poor));
mHRVGaugeStatus.setTextColor(getResources().getColor(R.color.hrv_status_poor));
break;
case LOW:
mHRVStatusSevenDaysAvgStatus.setText(getString(R.string.hrv_status_low));
mHRVStatusSevenDaysAvgStatus.setTextColor(getResources().getColor(R.color.hrv_status_low));
mHRVGaugeStatus.setText(getString(R.string.hrv_status_low));
mHRVGaugeStatus.setTextColor(getResources().getColor(R.color.hrv_status_low));
break;
case UNBALANCED:
mHRVStatusSevenDaysAvgStatus.setText(getString(R.string.hrv_status_unbalanced));
mHRVStatusSevenDaysAvgStatus.setTextColor(getResources().getColor(R.color.hrv_status_unbalanced));
mHRVGaugeStatus.setText(getString(R.string.hrv_status_unbalanced));
mHRVGaugeStatus.setTextColor(getResources().getColor(R.color.hrv_status_unbalanced));
break;
case BALANCED:
mHRVStatusSevenDaysAvgStatus.setText(getString(R.string.hrv_status_balanced));
mHRVStatusSevenDaysAvgStatus.setTextColor(getResources().getColor(R.color.hrv_status_balanced));
mHRVGaugeStatus.setText(getString(R.string.hrv_status_balanced));
mHRVGaugeStatus.setTextColor(getResources().getColor(R.color.hrv_status_balanced));
break;
}
float value = DashboardHrvWidget.calculateGaugeValue(today.weeklyAvg, today.baseLineLowUpper, today.baseLineBalancedLower, today.baseLineBalancedUpper);
final String valueText = value > 0 ? getString(R.string.hrv_status_unit, today.weeklyAvg) : getString(R.string.stats_empty_value);
mHRVGaugeValue.setText(valueText);
gaugeDrawer.drawSegmentedGauge(mHRVStatusGauge, DashboardHrvWidget.getColors(), DashboardHrvWidget.getSegments(), value, false, true);
}
private List<HRVStatusDayData> getWeeklyData(DBHandler db, Calendar day, GBDevice device) {
@ -231,6 +263,7 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
sample.getWeeklyAverage() != null ? sample.getWeeklyAverage() : 0,
sample.getLastNightAverage() != null ? sample.getLastNightAverage() : 0,
sample.getLastNight5MinHigh() != null ? sample.getLastNight5MinHigh() : 0,
sample.getBaselineLowUpper() != null ? sample.getBaselineLowUpper() : 0,
sample.getBaselineBalancedLower() != null ? sample.getBaselineBalancedLower() : 0,
sample.getBaselineBalancedUpper() != null ? sample.getBaselineBalancedUpper() : 0,
sample.getStatus() != null ? sample.getStatus() : HrvSummarySample.Status.NONE
@ -247,6 +280,7 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
0,
0,
0,
0,
HrvSummarySample.Status.NONE
);
weeklyData.add(d);
@ -346,6 +380,7 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
public Integer dayAvg;
public Integer baseLineBalancedLower;
public Integer baseLineBalancedUpper;
public Integer baseLineLowUpper;
public HrvSummarySample.Status status;
public Calendar day;
@ -355,6 +390,7 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
Integer weeklyAvg,
Integer lastNight,
Integer lastNight5MinHigh,
Integer baseLineLowUpper,
Integer baseLineBalancedLower,
Integer baseLineBalancedUpper,
HrvSummarySample.Status status) {
@ -366,6 +402,7 @@ public class HRVStatusFragment extends AbstractChartFragment<HRVStatusFragment.H
this.status = status;
this.day = day;
this.dayAvg = dayAvg;
this.baseLineLowUpper = baseLineLowUpper;
this.baseLineBalancedLower = baseLineBalancedLower;
this.baseLineBalancedUpper = baseLineBalancedUpper;
}

View File

@ -89,41 +89,12 @@ public class DashboardHrvWidget extends AbstractGaugeWidget {
@Override
protected void draw(final DashboardFragment.DashboardData dashboardData) {
final int[] colors = new int[]{
ContextCompat.getColor(GBApplication.getContext(), R.color.hrv_status_low),
ContextCompat.getColor(GBApplication.getContext(), R.color.hrv_status_unbalanced),
ContextCompat.getColor(GBApplication.getContext(), R.color.hrv_status_balanced),
ContextCompat.getColor(GBApplication.getContext(), R.color.hrv_status_unbalanced),
};
final float[] segments = new float[]{
0.125f, // low
0.125f, // unbalanced
0.5f, // normal
0.25f, // unbalanced
};
final int[] colors = getColors();
final float[] segments = getSegments();
final HrvData hrvData = (HrvData) dashboardData.get("hrv");
final float value;
final float value = hrvData != null ? calculateGaugeValue(hrvData.weeklyAverage, hrvData.baselineLowUpper, hrvData.baselineBalancedLower, hrvData.baselineBalancedUpper) : -1;
final String valueText;
if (hrvData != null && hrvData.weeklyAverage != 0 && hrvData.hasBaselines()) {
valueText = getString(R.string.hrv_status_unit, hrvData.weeklyAverage);
if (hrvData.weeklyAverage < hrvData.baselineLowUpper) {
value = 0.125f * (float) GaugeDrawer.normalize(hrvData.weeklyAverage, 0f, hrvData.baselineLowUpper);
} else if (hrvData.weeklyAverage < hrvData.baselineBalancedLower) {
value = 0.125f + 0.125f * (float) GaugeDrawer.normalize((float) hrvData.weeklyAverage, hrvData.baselineLowUpper, hrvData.baselineBalancedLower);
} else if (hrvData.weeklyAverage < hrvData.baselineBalancedUpper) {
value = 0.125f + 0.125f + 0.5f * (float) GaugeDrawer.normalize((float) hrvData.weeklyAverage, hrvData.baselineBalancedLower, hrvData.baselineBalancedUpper);
} else {
value = 0.125f + 0.125f + 0.5f + 0.125f * (float) GaugeDrawer.normalize((float) hrvData.weeklyAverage, hrvData.baselineBalancedUpper, 2 * hrvData.baselineBalancedUpper);
}
} else {
value = -1;
valueText = getString(R.string.stats_empty_value);
}
valueText = value > 0 ? getString(R.string.hrv_status_unit, hrvData.weeklyAverage) : getString(R.string.stats_empty_value);
setText(valueText);
drawSegmentedGauge(
colors,
@ -134,6 +105,42 @@ public class DashboardHrvWidget extends AbstractGaugeWidget {
);
}
public static int[] getColors() {
return new int[]{
ContextCompat.getColor(GBApplication.getContext(), R.color.hrv_status_low),
ContextCompat.getColor(GBApplication.getContext(), R.color.hrv_status_unbalanced),
ContextCompat.getColor(GBApplication.getContext(), R.color.hrv_status_balanced),
ContextCompat.getColor(GBApplication.getContext(), R.color.hrv_status_unbalanced),
};
}
public static float[] getSegments() {
return new float[]{
0.125f, // low
0.125f, // unbalanced
0.5f, // normal
0.25f, // unbalanced
};
}
public static float calculateGaugeValue(int weeklyAverage, int baselineLowUpper, int baselineBalancedLower, int baselineBalancedUpper) {
final float value;
if (weeklyAverage != 0 && baselineLowUpper != 0 && baselineBalancedLower != 0 && baselineBalancedUpper != 0) {
if (weeklyAverage < baselineLowUpper) {
value = 0.125f * (float) GaugeDrawer.normalize(weeklyAverage, 0f, baselineLowUpper);
} else if (weeklyAverage < baselineBalancedLower) {
value = 0.125f + 0.125f * (float) GaugeDrawer.normalize((float) weeklyAverage, baselineLowUpper, baselineBalancedLower);
} else if (weeklyAverage < baselineBalancedUpper) {
value = 0.125f + 0.125f + 0.5f * (float) GaugeDrawer.normalize((float) weeklyAverage, baselineBalancedLower, baselineBalancedUpper);
} else {
value = 0.125f + 0.125f + 0.5f + 0.125f * (float) GaugeDrawer.normalize((float) weeklyAverage, baselineBalancedUpper, 2 * baselineBalancedUpper);
}
} else {
value = -1;
}
return value;
}
private static class HrvData implements Serializable {
private int weeklyAverage;
private int lastNightAverage;

View File

@ -1,4 +1,4 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -6,7 +6,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
@ -18,11 +18,44 @@
android:layout_marginTop="15dp"
/>
<RelativeLayout
android:id="@+id/hrv_gauge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical"
>
<ImageView
android:id="@+id/hrv_status_gauge_bar"
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:scaleType="fitStart" />
<TextView
android:id="@+id/hrv_gauge_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="@string/stats_empty_value"
android:textSize="30sp" />
<TextView
android:id="@+id/hrv_gauge_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hrv_gauge_value"
android:layout_centerHorizontal="true"
android:text="" />
</RelativeLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="30dp"
android:layout_weight="3"
android:shrinkColumns="*"
android:stretchColumns="*">
@ -259,5 +292,4 @@
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>