1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 13:26:50 +01:00

Add Activity info into Device Card: add charts, split widths evenly

This commit is contained in:
vanous 2021-11-20 15:56:12 +01:00
parent b24da9a409
commit 8caa529fbb
3 changed files with 235 additions and 97 deletions

View File

@ -22,6 +22,7 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.InputType;
import android.transition.TransitionManager;
@ -44,6 +45,12 @@ import androidx.cardview.widget.CardView;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.RecyclerView;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.MPPointF;
import com.google.android.material.snackbar.Snackbar;
import com.jaredrummler.android.colorpicker.ColorPickerDialog;
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener;
@ -52,6 +59,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -677,6 +685,7 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
TextView fmFrequencyLabel;
ImageView ledColor;
//activity card
LinearLayout cardViewActivityCardLayout;
LinearLayout cardViewActivityCardStepsLayout;
LinearLayout cardViewActivityCardSleepLayout;
@ -687,6 +696,9 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
ProgressBar cardViewActivityCardStepsProgress;
ProgressBar cardViewActivityCardDistanceProgress;
ProgressBar cardViewActivityCardSleepProgress;
PieChart TotalStepsChart;
PieChart TotalDistanceChart;
PieChart SleepTimeChart;
ViewHolder(View view) {
super(view);
@ -747,6 +759,9 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
cardViewActivityCardDistanceProgress = view.findViewById(R.id.card_view_activity_card_distance_progress);
cardViewActivityCardSleepProgress = view.findViewById(R.id.card_view_activity_card_sleep_progress);
TotalStepsChart = view.findViewById(R.id.activity_dashboard_piechart1);
TotalDistanceChart = view.findViewById(R.id.activity_dashboard_piechart2);
SleepTimeChart = view.findViewById(R.id.activity_dashboard_piechart3);
}
}
@ -856,21 +871,95 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
holder.cardViewActivityCardDistanceProgress.setMax(distanceGoal);
holder.cardViewActivityCardDistanceProgress.setProgress(steps * stepLength);
setUpChart(holder.TotalStepsChart);
setChartsData(holder.TotalStepsChart, steps, stepGoal, context.getString(R.string.live_activity_total_steps), context);
setUpChart(holder.TotalDistanceChart);
setChartsData(holder.TotalDistanceChart, steps * stepLength, distanceGoal, context.getString(R.string.live_activity_total_distance), context);
setUpChart(holder.SleepTimeChart);
setChartsData(holder.SleepTimeChart, sleep, sleepGoalMinutes, context.getString(R.string.live_activity_sleep_duration), context);
boolean showActivityCard = GBApplication.getDeviceSpecificSharedPrefs(device.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREFS_ACTIVITY_IN_DEVICE_CARD, true);
holder.cardViewActivityCardLayout.setVisibility(showActivityCard ? View.VISIBLE : View.GONE);
boolean showActivitySteps = GBApplication.getDeviceSpecificSharedPrefs(device.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREFS_ACTIVITY_IN_DEVICE_CARD_STEPS, true);
holder.cardViewActivityCardStepsLayout.setVisibility(showActivitySteps ? View.VISIBLE : View.GONE);
holder.TotalStepsChart.setVisibility(showActivitySteps ? View.VISIBLE : View.GONE);
boolean showActivitySleep = GBApplication.getDeviceSpecificSharedPrefs(device.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREFS_ACTIVITY_IN_DEVICE_CARD_SLEEP, true);
holder.cardViewActivityCardSleepLayout.setVisibility(showActivitySleep ? View.VISIBLE : View.GONE);
holder.SleepTimeChart.setVisibility(showActivitySleep ? View.VISIBLE : View.GONE);
boolean showActivityDistance = GBApplication.getDeviceSpecificSharedPrefs(device.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREFS_ACTIVITY_IN_DEVICE_CARD_DISTANCE, true);
holder.cardViewActivityCardDistanceLayout.setVisibility(showActivityDistance ? View.VISIBLE : View.GONE);
holder.TotalDistanceChart.setVisibility(showActivityDistance ? View.VISIBLE : View.GONE);
}
private String getHM(long value) {
return DateTimeUtils.formatDurationHoursMinutes(value, TimeUnit.MINUTES);
}
private void setUpChart(PieChart DashboardChart) {
DashboardChart.setNoDataText("");
DashboardChart.getLegend().setEnabled(false);
DashboardChart.setDrawHoleEnabled(true);
DashboardChart.setHoleColor(Color.WHITE);
DashboardChart.getDescription().setText("");
DashboardChart.setTransparentCircleColor(Color.WHITE);
DashboardChart.setTransparentCircleAlpha(110);
DashboardChart.setHoleRadius(70f);
DashboardChart.setTransparentCircleRadius(75f);
DashboardChart.setDrawCenterText(true);
DashboardChart.setRotationEnabled(true);
DashboardChart.setHighlightPerTapEnabled(true);
DashboardChart.setCenterTextOffset(0, 0);
}
private void setChartsData(PieChart pieChart, float value, float target, String label, Context context) {
final String CHART_COLOR_START = "#e74c3c";
final String CHART_COLOR_END = "#2ecc71";
ArrayList<PieEntry> entries = new ArrayList<>();
entries.add(new PieEntry((float) value, context.getResources().getDrawable(R.drawable.ic_star_gold)));
if (value < target) {
entries.add(new PieEntry((float) (target - value)));
}
pieChart.setCenterText(String.format("%d%%\n%s", (int) (value * 100 / target), label));
float colorValue = Math.max(0, Math.min(1, value / target));
int chartColor = interpolateColor(Color.parseColor(CHART_COLOR_START), Color.parseColor(CHART_COLOR_END), colorValue);
PieDataSet dataSet = new PieDataSet(entries, "");
dataSet.setDrawIcons(false);
dataSet.setIconsOffset(new MPPointF(0, -66));
if (colorValue == 1) {
dataSet.setDrawIcons(true);
}
dataSet.setSliceSpace(0f);
dataSet.setSelectionShift(5f);
dataSet.setColors(chartColor, Color.LTGRAY);
PieData data = new PieData(dataSet);
data.setValueTextSize(0f);
data.setValueTextColor(Color.WHITE);
pieChart.setData(data);
pieChart.invalidate();
}
private float interpolate(float a, float b, float proportion) {
return (a + ((b - a) * proportion));
}
private int interpolateColor(int a, int b, float proportion) {
float[] hsva = new float[3];
float[] hsvb = new float[3];
Color.colorToHSV(a, hsva);
Color.colorToHSV(b, hsvb);
for (int i = 0; i < 3; i++) {
hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
}
return Color.HSVToColor(hsvb);
}
}

View File

@ -469,23 +469,32 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/device_info_icons"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingBottom="5dp">
android:paddingBottom="15dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_view_activity_card_steps_layout"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/card_view_activity_card_steps_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="start|center_vertical"
android:layout_gravity="start|bottom"
android:background="@drawable/ic_shoe_prints_many"
android:contentDescription="@string/icon_placeholder"
android:padding="0dp" />
@ -516,16 +525,19 @@
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_view_activity_card_distance_layout"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/card_view_activity_card_distance_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="start|center_vertical"
android:layout_gravity="start|bottom"
android:background="@drawable/ic_distance_total"
android:contentDescription="@string/icon_placeholder"
android:padding="0dp" />
@ -556,16 +568,19 @@
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_view_activity_card_sleep_layout"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/card_view_activity_card_sleep_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="start|center_vertical"
android:layout_gravity="start|bottom"
android:background="@drawable/ic_activity_sleep"
android:contentDescription="@string/icon_placeholder" />
@ -594,6 +609,38 @@
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/activity_dashboard_piechart1"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_gravity="bottom|center"
android:layout_marginBottom="0dp"
android:layout_weight="1" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/activity_dashboard_piechart2"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_gravity="bottom|center"
android:layout_marginBottom="0dp"
android:layout_weight="1" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/activity_dashboard_piechart3"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_gravity="bottom|center"
android:layout_marginBottom="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>

View File

@ -561,6 +561,8 @@
<string name="live_activity_steps_history">Steps history</string>
<string name="live_activity_current_steps_per_minute">Current steps/min</string>
<string name="live_activity_total_steps">Total steps</string>
<string name="live_activity_total_distance">Total distance</string>
<string name="live_activity_sleep_duration">Sleep time</string>
<string name="live_activity_steps_per_minute_history">Steps per minute history</string>
<string name="live_activity_start_your_activity">Start your activity</string>
<string name="live_activity_max_heart_rate">Current / Max heart rate: %1$d / %2$d</string>