1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 04:46:51 +01:00

Stress fragment redesign

This commit is contained in:
a0z 2024-08-24 17:44:18 +02:00
parent bb50796d0a
commit b43c109903
6 changed files with 262 additions and 116 deletions

View File

@ -150,7 +150,7 @@ public class SleepChartFragment extends AbstractActivityChartFragment<SleepChart
} else {
entries.add(new PieEntry(1));
colors.add(Color.GRAY);
colors.add(getResources().getColor(R.color.gauge_line_color));
}
PieDataSet set = new PieDataSet(entries, "");

View File

@ -18,10 +18,16 @@ package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
@ -45,7 +51,9 @@ import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -66,6 +74,11 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
private LineChart mStressChart;
private PieChart mStressLevelsPieChart;
private TextView stressChartRelaxedTime;
private TextView stressChartMildTime;
private TextView stressChartModerateTime;
private TextView stressChartHighTime;
private TextView stressDate;
private int BACKGROUND_COLOR;
private int DESCRIPTION_COLOR;
@ -115,10 +128,48 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
@Override
protected void updateChartsnUIThread(final StressChartsData stressData) {
final PieData pieData = stressData.getPieData();
if (stressData.getAverage() > 0) {
mStressLevelsPieChart.setCenterText(requireContext().getString(R.string.average, String.valueOf(stressData.getAverage())));
Date date = new Date((long) this.getTSEnd() * 1000);
String formattedDate = new SimpleDateFormat("E, MMM dd").format(date);
stressDate.setText(formattedDate);
Map<StressType, Integer> stressZoneTimes = stressData.getStressZoneTimes();
Integer relaxedTime = stressZoneTimes.get(StressType.RELAXED);
if (0 < relaxedTime) {
stressChartRelaxedTime.setText(DateTimeUtils.formatDurationHoursMinutes(relaxedTime, TimeUnit.SECONDS));
} else {
mStressLevelsPieChart.setCenterText(requireContext().getString(R.string.no_data));
stressChartRelaxedTime.setText(R.string.stats_empty_value);
}
Integer mildTime = stressZoneTimes.get(StressType.MILD);
if (mildTime > 0) {
stressChartMildTime.setText(DateTimeUtils.formatDurationHoursMinutes(mildTime, TimeUnit.SECONDS));
} else {
stressChartMildTime.setText(R.string.stats_empty_value);
}
Integer moderateTime = stressZoneTimes.get(StressType.MODERATE);
if (moderateTime > 0) {
stressChartModerateTime.setText(DateTimeUtils.formatDurationHoursMinutes(moderateTime, TimeUnit.SECONDS));
} else {
stressChartModerateTime.setText(R.string.stats_empty_value);
}
Integer highTime = stressZoneTimes.get(StressType.HIGH);
if (highTime > 0) {
stressChartHighTime.setText(DateTimeUtils.formatDurationHoursMinutes(highTime, TimeUnit.SECONDS));
} else {
stressChartHighTime.setText(R.string.stats_empty_value);
}
if (stressData.getAverage() > 0) {
int noc = String.valueOf(stressData.getAverage()).length();
SpannableString pieChartCenterText = new SpannableString(stressData.getAverage() + "\n" + requireContext().getString(R.string.stress_average));
pieChartCenterText.setSpan(new RelativeSizeSpan(1.75f), 0, noc, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
pieChartCenterText.setSpan(new RelativeSizeSpan(0.72f), noc, pieChartCenterText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mStressLevelsPieChart.setCenterText(pieChartCenterText);
} else {
SpannableString pieChartCenterText = new SpannableString("-\n" + requireContext().getString(R.string.stress_average));
pieChartCenterText.setSpan(new RelativeSizeSpan(1.25f), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
pieChartCenterText.setSpan(new RelativeSizeSpan(0.72f), 2, pieChartCenterText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mStressLevelsPieChart.setCenterText(pieChartCenterText);
}
mStressLevelsPieChart.setData(pieData);
@ -147,8 +198,19 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
final Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_stresschart, container, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
rootView.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
getChartsHost().enableSwipeRefresh(scrollY == 0);
});
}
mStressChart = rootView.findViewById(R.id.stress_line_chart);
mStressLevelsPieChart = rootView.findViewById(R.id.stress_pie_chart);
stressChartRelaxedTime = rootView.findViewById(R.id.stress_chart_relaxed_time);
stressChartMildTime = rootView.findViewById(R.id.stress_chart_mild_time);
stressChartModerateTime = rootView.findViewById(R.id.stress_chart_moderate_time);
stressChartHighTime = rootView.findViewById(R.id.stress_chart_high_time);
stressDate = rootView.findViewById(R.id.stress_date);
setupLineChart();
setupPieChart();
@ -165,6 +227,12 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
mStressLevelsPieChart.setEntryLabelColor(DESCRIPTION_COLOR);
mStressLevelsPieChart.getDescription().setText("");
mStressLevelsPieChart.setNoDataText("");
mStressLevelsPieChart.setTouchEnabled(false);
mStressLevelsPieChart.setCenterTextColor(GBApplication.getTextColor(getContext()));
mStressLevelsPieChart.setCenterTextSize(18f);
mStressLevelsPieChart.setHoleColor(getContext().getResources().getColor(R.color.transparent));
mStressLevelsPieChart.setHoleRadius(85);
mStressLevelsPieChart.setDrawEntryLabels(false);
mStressLevelsPieChart.getLegend().setEnabled(false);
}
@ -380,18 +448,25 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
final List<ILineDataSet> lineDataSets = new ArrayList<>();
final List<PieEntry> pieEntries = new ArrayList<>();
final List<Integer> pieColors = new ArrayList<>();
final Map<StressType, Integer> stressZoneTimes = new HashMap<>();
for (final StressType stressType : StressType.values()) {
final List<Entry> stressEntries = lineEntriesPerLevel.get(stressType);
lineDataSets.add(createDataSet(stressType, stressEntries));
final Integer stressTime = accumulator.get(stressType);
stressZoneTimes.put(stressType, stressTime);
if (stressType != StressType.UNKNOWN && stressTime != null && stressTime != 0) {
pieEntries.add(new PieEntry(stressTime, stressType.getLabel(requireContext())));
pieColors.add(stressType.getColor(requireContext()));
}
}
if (pieEntries.isEmpty()) {
pieEntries.add(new PieEntry(1));
pieColors.add(getResources().getColor(R.color.gauge_line_color));
}
final PieDataSet pieDataSet = new PieDataSet(pieEntries, "");
pieDataSet.setValueFormatter(new ValueFormatter() {
@Override
@ -404,12 +479,14 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
pieDataSet.setValueTextSize(13f);
pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
pieDataSet.setDrawValues(false);
pieDataSet.setSliceSpace(2f);
final PieData pieData = new PieData(pieDataSet);
final LineData lineData = new LineData(lineDataSets);
final ValueFormatter xValueFormatter = new SampleXLabelFormatter(tsTranslation);
final DefaultChartsData<LineData> chartsData = new DefaultChartsData<>(lineData, xValueFormatter);
return new StressChartsData(pieData, chartsData, Math.round((float) averageSum / averageNumSamples));
return new StressChartsData(pieData, chartsData, Math.round((float) averageSum / averageNumSamples), stressZoneTimes);
}
}
@ -417,11 +494,17 @@ public class StressChartFragment extends AbstractChartFragment<StressChartFragme
private final PieData pieData;
private final DefaultChartsData<LineData> chartsData;
private final int average;
private Map<StressType, Integer> stressZoneTimes;
public StressChartsData(final PieData pieData, final DefaultChartsData<LineData> chartsData, final int average) {
public StressChartsData(final PieData pieData, final DefaultChartsData<LineData> chartsData, final int average, Map<StressType, Integer> stressZoneTimes) {
this.pieData = pieData;
this.chartsData = chartsData;
this.average = average;
this.stressZoneTimes = stressZoneTimes;
}
public Map<StressType, Integer> getStressZoneTimes() {
return stressZoneTimes;
}
public PieData getPieData() {

View File

@ -1,69 +0,0 @@
<RelativeLayout 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"
android:orientation="vertical"
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ActivityChartsActivity$PlaceholderFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/sleepchart_info"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarFadeDuration="0"
android:layout_weight="90" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10"
android:orientation="horizontal">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/sleepchart_pie_light_deep"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="40" />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/sleepchart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />
</LinearLayout>
<!--<TextView-->
<!--android:text="Test"-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="fill_parent"-->
<!--android:layout_weight="20" />-->
</LinearLayout>
<include
layout="@layout/layout_widget_heartrate_average"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_gravity="top|end"
android:layout_marginTop="25dp"
android:layout_marginStart="170dp" />
<include
layout="@layout/layout_widget_intensity_total"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_gravity="top|end"
android:layout_marginTop="75dp"
android:layout_marginStart="171dp" />
</RelativeLayout>

View File

@ -1,32 +0,0 @@
<RelativeLayout 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"
android:orientation="vertical"
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ActivityChartsActivity$PlaceholderFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10"
android:orientation="horizontal">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/stress_pie_chart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="40" />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/stress_line_chart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

View File

@ -1,25 +1,188 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="nodomain.freeyourgadget.gadgetbridge.activities.charts.ActivityChartsActivity$PlaceholderFragment">
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/stress_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:textSize="20sp" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/stress_pie_chart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="15dp"
android:layout_weight="2" />
<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="*">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="20dip"
android:paddingTop="20dip"
android:paddingRight="20dip">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="@color/chart_stress_relaxed" />
<TextView
android:id="@+id/stress_chart_relaxed_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="20dip"
android:text="@string/stats_empty_value"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="@string/stress_relaxed"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="20dip"
android:paddingTop="20dip"
android:paddingRight="20dip">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="@color/chart_stress_mild" />
<TextView
android:id="@+id/stress_chart_mild_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="20dip"
android:text="@string/stats_empty_value"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="@string/stress_mild"
android:textSize="12sp" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="20dip"
android:paddingTop="20dip"
android:paddingRight="20dip">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="@color/chart_stress_moderate" />
<TextView
android:id="@+id/stress_chart_moderate_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="20dip"
android:gravity="center"
android:text="@string/stats_empty_value"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="center"
android:text="@string/stress_moderate"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="20dip"
android:paddingTop="20dip"
android:paddingRight="20dip">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="@color/chart_stress_high" />
<TextView
android:id="@+id/stress_chart_high_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="20dip"
android:gravity="center"
android:text="@string/stats_empty_value"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="center"
android:text="@string/stress_high"
android:textSize="12sp" />
</LinearLayout>
</TableRow>
</TableLayout>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/stress_line_chart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_height="300dp"
android:layout_marginBottom="25dp"
android:layout_weight="2" />
</LinearLayout>
</RelativeLayout>
</ScrollView>

View File

@ -954,6 +954,7 @@
<string name="lack_of_step">Lack of steps: %1$d</string>
<string name="overstep">Overstep: %1$d</string>
<string name="average">Average: %1$s</string>
<string name="stress_average">Average</string>
<string name="pref_huami_truncate_fetch_operation_timestamps_title">Truncate fetch operation timestamps</string>
<string name="pref_huami_truncate_fetch_operation_timestamps_summary">Truncate the fetch operation timestamps to minutes. Disable this setting to keep the timestamps in seconds, if you face issues while fetching very short workouts.</string>
<string name="pref_title_dont_ack_transfer">Do not ACK activity data transfer</string>