1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-26 09:44:31 +02:00

refactored satistics page

This commit is contained in:
OliE
2018-01-12 21:30:05 +01:00
parent 3b563a07ae
commit 55f0709383
4 changed files with 187 additions and 263 deletions

View File

@@ -97,6 +97,28 @@ public class ScaleMeasurement implements Cloneable {
return clone;
}
public void add(final ScaleMeasurement summand) {
weight += summand.getWeight();
fat += summand.getFat();
water += summand.getWater();
muscle += summand.getMuscle();
lbw += summand.getLbw();
bone += summand.getBone();
waist += summand.getWaist();
hip += summand.getHip();
}
public void divide(final float divisor) {
weight /= divisor;
fat /= divisor;
water /= divisor;
muscle /= divisor;
lbw /= divisor;
bone /= divisor;
waist /= divisor;
hip /= divisor;
}
public int getId() {
return id;
}

View File

@@ -24,6 +24,7 @@ import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;
import com.health.openscale.R;
@@ -31,56 +32,116 @@ import com.health.openscale.core.OpenScale;
import com.health.openscale.core.datatypes.ScaleMeasurement;
import com.health.openscale.core.datatypes.ScaleUser;
import com.health.openscale.core.utils.DateTimeHelpers;
import com.health.openscale.gui.views.BoneMeasurementView;
import com.health.openscale.gui.views.FatMeasurementView;
import com.health.openscale.gui.views.HipMeasurementView;
import com.health.openscale.gui.views.LBWMeasurementView;
import com.health.openscale.gui.views.MeasurementView;
import com.health.openscale.gui.views.MuscleMeasurementView;
import com.health.openscale.gui.views.WaistMeasurementView;
import com.health.openscale.gui.views.WaterMeasurementView;
import com.health.openscale.gui.views.WeightMeasurementView;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import static com.health.openscale.gui.views.MeasurementView.MeasurementViewMode.STATISTIC;
public class StatisticsFragment extends Fragment implements FragmentUpdateListener {
private View statisticsView;
private TextView txtTitleGoal;
private TextView txtTitleStatistics;
private TextView txtGoalWeight;
private TextView txtGoalDiff;
private TextView txtGoalDayLeft;
private TextView txtAvgWeek;
private TextView txtAvgMonth;
private TextView txtLabelGoalWeight;
private TextView txtLabelGoalDiff;
private TextView txtLabelDayLeft;
private TextView txtLabelAvgWeek;
private TextView txtLabelAvgMonth;
private TableLayout tableWeekAveragesLayoutColumnA;
private TableLayout tableWeekAveragesLayoutColumnB;
private TableLayout tableMonthAveragesLayoutColumnA;
private TableLayout tableMonthAveragesLayoutColumnB;
private SharedPreferences prefs;
private ScaleUser currentScaleUser;
private ScaleMeasurement lastScaleMeasurement;
private ArrayList <MeasurementView> viewMeasurementsListWeek;
private ArrayList <MeasurementView> viewMeasurementsListMonth;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
statisticsView = inflater.inflate(R.layout.fragment_statistics, container, false);
txtTitleGoal = (TextView) statisticsView.findViewById(R.id.txtTitleGoal);
txtTitleStatistics = (TextView) statisticsView.findViewById(R.id.txtTitleStatistics);
prefs = PreferenceManager.getDefaultSharedPreferences(statisticsView.getContext());
txtGoalWeight = (TextView) statisticsView.findViewById(R.id.txtGoalWeight);
txtGoalDiff = (TextView) statisticsView.findViewById(R.id.txtGoalDiff);
txtGoalDayLeft = (TextView) statisticsView.findViewById(R.id.txtGoalDayLeft);
txtAvgWeek = (TextView) statisticsView.findViewById(R.id.txtAvgWeek);
txtAvgMonth = (TextView) statisticsView.findViewById(R.id.txtAvgMonth);
txtLabelGoalWeight = (TextView) statisticsView.findViewById(R.id.txtLabelGoalWeight);
txtLabelGoalDiff = (TextView) statisticsView.findViewById(R.id.txtLabelGoalDiff);
txtLabelDayLeft = (TextView) statisticsView.findViewById(R.id.txtLabelDayLeft);
txtLabelAvgWeek = (TextView) statisticsView.findViewById(R.id.txtLabelAvgWeek);
txtLabelAvgMonth = (TextView) statisticsView.findViewById(R.id.txtLabelAvgMonth);
tableWeekAveragesLayoutColumnA = (TableLayout) statisticsView.findViewById(R.id.tableWeekAveragesLayoutColumnA);
tableWeekAveragesLayoutColumnB = (TableLayout) statisticsView.findViewById(R.id.tableWeekAveragesLayoutColumnB);
tableMonthAveragesLayoutColumnA = (TableLayout) statisticsView.findViewById(R.id.tableMonthAveragesLayoutColumnA);
tableMonthAveragesLayoutColumnB = (TableLayout) statisticsView.findViewById(R.id.tableMonthAveragesLayoutColumnB);
viewMeasurementsListWeek = new ArrayList<>();
viewMeasurementsListWeek.add(new WeightMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new WaterMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new MuscleMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new LBWMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new FatMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new BoneMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new WaistMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new HipMeasurementView(statisticsView.getContext()));
int i=0;
for (MeasurementView measurement : viewMeasurementsListWeek) {
measurement.setEditMode(STATISTIC);
measurement.updatePreferences(prefs);
if ((i % 2) == 0) {
tableWeekAveragesLayoutColumnA.addView(measurement);
}
else {
tableWeekAveragesLayoutColumnB.addView(measurement);
}
i++;
}
viewMeasurementsListMonth = new ArrayList<>();
viewMeasurementsListMonth.add(new WeightMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new WaterMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new MuscleMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new LBWMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new FatMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new BoneMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new WaistMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new HipMeasurementView(statisticsView.getContext()));
i=0;
for (MeasurementView measurement : viewMeasurementsListMonth) {
measurement.setEditMode(STATISTIC);
measurement.updatePreferences(prefs);
if ((i % 2) == 0) {
tableMonthAveragesLayoutColumnA.addView(measurement);
}
else {
tableMonthAveragesLayoutColumnB.addView(measurement);
}
i++;
}
OpenScale.getInstance(getContext()).registerFragment(this);
@@ -95,10 +156,6 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
lastScaleMeasurement = scaleMeasurementList.get(0);
}
txtTitleGoal.setText(getResources().getString(R.string.label_title_goal).toUpperCase());
txtTitleStatistics.setText(getResources().getString(R.string.label_title_statistics).toUpperCase());
prefs = PreferenceManager.getDefaultSharedPreferences(statisticsView.getContext());
currentScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();
updateStatistics(scaleMeasurementList);
@@ -158,6 +215,7 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
}
private void updateStatistics(List<ScaleMeasurement> scaleMeasurementList) {
Calendar histDate = Calendar.getInstance();
Calendar weekPastDate = Calendar.getInstance();
Calendar monthPastDate = Calendar.getInstance();
@@ -169,167 +227,34 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
monthPastDate.add(Calendar.DATE, -30);
int weekSize = 0;
float weekAvgWeight = 0;
float weekAvgBMI = 0;
float weekAvgFat = 0;
float weekAvgWater = 0;
float weekAvgMuscle = 0;
float weekAvgLBW = 0;
float weekAvgWaist = 0;
float weekAvgBone = 0;
float weekAvgWHtR = 0;
float weekAvgHip = 0;
float weekAvgWHR = 0;
int monthSize = 0;
float monthAvgWeight = 0;
float monthAvgBMI = 0;
float monthAvgFat = 0;
float monthAvgWater = 0;
float monthAvgMuscle = 0;
float monthAvgLBW = 0;
float monthAvgWaist = 0;
float monthAvgBone = 0;
float monthAvgWHtR = 0;
float monthAvgHip = 0;
float monthAvgWHR = 0;
for (ScaleMeasurement scaleMeasurement : scaleMeasurementList)
{
histDate.setTime(scaleMeasurement.getDateTime());
ScaleMeasurement averageWeek = new ScaleMeasurement();
ScaleMeasurement averageMonth = new ScaleMeasurement();
for (ScaleMeasurement measurement : scaleMeasurementList) {
histDate.setTime(measurement.getDateTime());
if (weekPastDate.before(histDate)) {
averageWeek.add(measurement);
weekSize++;
weekAvgWeight += scaleMeasurement.getConvertedWeight(currentScaleUser.getScaleUnit());
weekAvgBMI += scaleMeasurement.getBMI(currentScaleUser.getBodyHeight());
weekAvgFat += scaleMeasurement.getFat();
weekAvgWater += scaleMeasurement.getWater();
weekAvgMuscle += scaleMeasurement.getMuscle();
weekAvgLBW += scaleMeasurement.getLbw();
weekAvgBone += scaleMeasurement.getBone();
weekAvgWaist += scaleMeasurement.getWaist();
weekAvgHip += scaleMeasurement.getHip();
weekAvgWHtR += scaleMeasurement.getWHtR(currentScaleUser.getBodyHeight());
weekAvgWHR += scaleMeasurement.getWHR();
}
if (monthPastDate.before(histDate)) {
averageMonth.add(measurement);
monthSize++;
monthAvgWeight += scaleMeasurement.getConvertedWeight(currentScaleUser.getScaleUnit());
monthAvgBMI += scaleMeasurement.getBMI(currentScaleUser.getBodyHeight());
monthAvgFat += scaleMeasurement.getFat();
monthAvgWater += scaleMeasurement.getWater();
monthAvgMuscle += scaleMeasurement.getMuscle();
monthAvgLBW += scaleMeasurement.getLbw();
monthAvgBone += scaleMeasurement.getBone();
monthAvgWaist += scaleMeasurement.getWaist();
monthAvgHip += scaleMeasurement.getHip();
monthAvgWHtR += scaleMeasurement.getWHtR(currentScaleUser.getBodyHeight());
monthAvgWHR += scaleMeasurement.getWHR();
} else {
break;
}
}
weekAvgWeight /= weekSize;
weekAvgBMI /= weekSize;
weekAvgFat /= weekSize;
weekAvgWater /= weekSize;
weekAvgMuscle /= weekSize;
weekAvgLBW /= weekSize;
weekAvgWaist /= weekSize;
weekAvgBone /= weekSize;
weekAvgWHtR /= weekSize;
weekAvgHip /= weekSize;
weekAvgWHR /= weekSize;
averageWeek.divide(weekSize);
averageMonth.divide(monthSize);
monthAvgWeight /= monthSize;
monthAvgBMI /= monthSize;
monthAvgFat /= monthSize;
monthAvgWater /= monthSize;
monthAvgMuscle /= monthSize;
monthAvgLBW /= monthSize;
monthAvgBone /= monthSize;
monthAvgWaist /= monthSize;
monthAvgWHtR /= monthSize;
monthAvgHip /= monthSize;
monthAvgWHR /= monthSize;
String info_week = new String();
String info_month = new String();
int lines = 1;
info_week += String.format("Ø-"+getResources().getString(R.string.label_weight)+": %.1f" + ScaleUser.UNIT_STRING[currentScaleUser.getScaleUnit()] + "<br>", weekAvgWeight);
info_month += String.format("Ø-"+getResources().getString(R.string.label_weight)+": %.1f" + ScaleUser.UNIT_STRING[currentScaleUser.getScaleUnit()] + "<br>", monthAvgWeight);
lines++;
info_week += String.format("Ø-"+getResources().getString(R.string.label_bmi)+": %.1f <br>", weekAvgBMI);
info_month += String.format("Ø-"+getResources().getString(R.string.label_bmi)+": %.1f <br>", monthAvgBMI);
lines++;
if (prefs.getBoolean("fatEnable", true)) {
info_week += String.format("Ø-"+getResources().getString(R.string.label_fat)+": %.1f%% <br>", weekAvgFat);
info_month += String.format("Ø-"+getResources().getString(R.string.label_fat)+": %.1f%% <br>", monthAvgFat);
lines++;
for (MeasurementView measurement : viewMeasurementsListWeek) {
measurement.loadFrom(averageWeek, null);
}
if (prefs.getBoolean("muscleEnable", true)) {
info_week += String.format("Ø-"+getResources().getString(R.string.label_muscle)+": %.1f%% <br>", weekAvgMuscle);
info_month += String.format("Ø-"+getResources().getString(R.string.label_muscle)+": %.1f%% <br>", monthAvgMuscle);
lines++;
for (MeasurementView measurement : viewMeasurementsListMonth) {
measurement.loadFrom(averageMonth, null);
}
if (prefs.getBoolean("lbwEnable", false)) {
info_week += String.format("Ø-"+getResources().getString(R.string.label_lbw)+": %.1fkg <br>", weekAvgLBW);
info_month += String.format("Ø-"+getResources().getString(R.string.label_lbw)+": %.1fkg <br>", monthAvgLBW);
lines++;
}
if (prefs.getBoolean("waterEnable", true)) {
info_week += String.format("Ø-"+getResources().getString(R.string.label_water)+": %.1f%% <br>", weekAvgWater);
info_month += String.format("Ø-"+getResources().getString(R.string.label_water)+": %.1f%% <br>", monthAvgWater);
lines++;
}
if (prefs.getBoolean("boneEnable", false)) {
info_week += String.format("Ø-"+getResources().getString(R.string.label_bone)+": %.1fkg <br>", weekAvgBone);
info_month += String.format("Ø-"+getResources().getString(R.string.label_bone)+": %.1fkg <br>",monthAvgBone);
lines++;
}
if (prefs.getBoolean("waistEnable", false)) {
info_week += String.format("Ø-"+getResources().getString(R.string.label_waist)+": %.1fcm <br>", weekAvgWaist);
info_month += String.format("Ø-"+getResources().getString(R.string.label_waist)+": %.1fcm <br>", monthAvgWaist);
lines++;
info_week += String.format("Ø-"+getResources().getString(R.string.label_whtr)+": %.2f <br>", weekAvgWHtR);
info_month += String.format("Ø-"+getResources().getString(R.string.label_whtr)+": %.2f <br>", monthAvgWHtR);
lines++;
}
if (prefs.getBoolean("hipEnable", false)) {
info_week += String.format("Ø-"+getResources().getString(R.string.label_hip)+": %.1fcm <br>", weekAvgHip);
info_month += String.format("Ø-"+getResources().getString(R.string.label_hip)+": %.1fcm <br>",monthAvgHip);
lines++;
}
if (prefs.getBoolean("hipEnable", false) && prefs.getBoolean("waistEnable", false)) {
info_week += String.format("Ø-"+getResources().getString(R.string.label_whr)+": %.2f <br>", weekAvgWHR);
info_month += String.format("Ø-"+getResources().getString(R.string.label_whr)+": %.2f <br>", monthAvgWHR);
lines++;
}
txtLabelAvgWeek.setLines(lines);
txtLabelAvgMonth.setLines(lines);
txtLabelAvgWeek.setText(Html.fromHtml(getResources().getString(R.string.label_last_week) + " <br> <font color='grey'><small> " + info_week + "</small></font>"));
txtLabelAvgMonth.setText(Html.fromHtml(getResources().getString(R.string.label_last_month) + " <br> <font color='grey'><small> " + info_month + "</small></font>"));
txtAvgWeek.setText(weekSize + " " + getResources().getString(R.string.label_measures));
txtAvgMonth.setText(monthSize + " " + getResources().getString(R.string.label_measures));
}
}

View File

@@ -45,10 +45,11 @@ import lecho.lib.hellocharts.util.ChartUtils;
import static com.health.openscale.gui.views.MeasurementView.MeasurementViewMode.ADD;
import static com.health.openscale.gui.views.MeasurementView.MeasurementViewMode.EDIT;
import static com.health.openscale.gui.views.MeasurementView.MeasurementViewMode.STATISTIC;
import static com.health.openscale.gui.views.MeasurementView.MeasurementViewMode.VIEW;
public abstract class MeasurementView extends TableLayout {
public enum MeasurementViewMode {VIEW, EDIT, ADD};
public enum MeasurementViewMode {VIEW, EDIT, ADD, STATISTIC};
private TableRow measurementRow;
private ImageView iconView;
@@ -168,12 +169,16 @@ public abstract class MeasurementView extends TableLayout {
indicatorView.setVisibility(View.VISIBLE);
editModeView.setVisibility(View.GONE);
incDecLayout.setVisibility(View.GONE);
nameView.setVisibility(View.VISIBLE);
valueView.setGravity(Gravity.RIGHT | Gravity.CENTER);
break;
case EDIT:
case ADD:
indicatorView.setVisibility(View.GONE);
editModeView.setVisibility(View.VISIBLE);
incDecLayout.setVisibility(View.VISIBLE);
nameView.setVisibility(View.VISIBLE);
valueView.setGravity(Gravity.RIGHT | Gravity.CENTER);
if (!isEditable()) {
editModeView.setImageDrawable(ContextCompat.getDrawable(getContext(),
@@ -182,6 +187,13 @@ public abstract class MeasurementView extends TableLayout {
showEvaluatorRow(false);
break;
case STATISTIC:
indicatorView.setVisibility(View.GONE);
incDecLayout.setVisibility(View.GONE);
editModeView.setVisibility(View.GONE);
nameView.setVisibility(View.GONE);
valueView.setGravity(Gravity.CENTER);
break;
}
}
@@ -322,6 +334,10 @@ public abstract class MeasurementView extends TableLayout {
private class onClickListenerEvaluation implements View.OnClickListener {
@Override
public void onClick(View v) {
if (getMeasurementMode() == STATISTIC) {
return;
}
if (getMeasurementMode() == EDIT || getMeasurementMode() == ADD) {
if (isEditable()) {
getInputDialog().show();

View File

@@ -15,12 +15,11 @@
android:orientation="vertical">
<TextView
android:id="@+id/txtTitleGoal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:text="@string/label_title_goal"
android:textSize="25dp"
android:textSize="20dp"
android:typeface="monospace" />
<View
@@ -43,7 +42,7 @@
<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="@drawable/ic_target" />
@@ -86,7 +85,7 @@
<ImageView
android:id="@+id/imageView6"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="@drawable/ic_difference" />
@@ -129,7 +128,7 @@
<ImageView
android:id="@+id/imageView7"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="@drawable/ic_daysleft" />
@@ -165,12 +164,10 @@
</TableLayout>
<TextView
android:id="@+id/txtTitleStatistics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoText="false"
android:text="@string/label_title_statistics"
android:textSize="25dp"
android:text="@string/label_last_week"
android:textSize="20dp"
android:typeface="monospace" />
<View
@@ -178,97 +175,61 @@
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="90">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="90"
android:id="@+id/tableWeekAveragesLayoutColumnA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:stretchColumns="1">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView8"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_gravity="center"
android:src="@drawable/ic_lastweek" />
<TextView
android:id="@+id/txtLabelAvgWeek"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="5dp"
android:layout_weight="80"
android:lines="2"
android:password="false"
android:phoneNumber="false"
android:singleLine="false"
android:text="Last 7 days"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="15dp" />
<TextView
android:id="@+id/txtAvgWeek"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center_vertical"
android:layout_weight="20"
android:gravity="right"
android:text="-1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="15dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow11"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:id="@+id/imageView9"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_gravity="center"
android:src="@drawable/ic_lastmonth" />
<TextView
android:id="@+id/txtLabelAvgMonth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="80"
android:lines="2"
android:password="false"
android:phoneNumber="false"
android:singleLine="false"
android:text="Last 30 days"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="15dp" />
<TextView
android:id="@+id/txtAvgMonth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center_vertical"
android:layout_weight="20"
android:gravity="right"
android:text="-1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="15dp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/tableWeekAveragesLayoutColumnB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:stretchColumns="1">
</TableLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_last_month"
android:textSize="20dp"
android:typeface="monospace" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="90">
<TableLayout
android:id="@+id/tableMonthAveragesLayoutColumnA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:stretchColumns="1">
</TableLayout>
<TableLayout
android:id="@+id/tableMonthAveragesLayoutColumnB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:stretchColumns="1">
</TableLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>