mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-21 16:02:04 +02:00
Refactor calculations: introduce ScaleCalculator
* does calculations based on ScaleData and user's body weight * redactoring reduces number of parameters for calculations and makes it possible to introduce more calculations based on the same parameters
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
package com.health.openscale.core;
|
||||||
|
|
||||||
|
public class ScaleCalculator {
|
||||||
|
|
||||||
|
public float weight;
|
||||||
|
public float fat;
|
||||||
|
public float water;
|
||||||
|
public float muscle;
|
||||||
|
public float waist;
|
||||||
|
public float hip;
|
||||||
|
public int body_height;
|
||||||
|
|
||||||
|
public void setScaleData(ScaleData scaleData) {
|
||||||
|
weight = scaleData.weight;
|
||||||
|
fat = scaleData.fat;
|
||||||
|
water = scaleData.water;
|
||||||
|
muscle = scaleData.weight;
|
||||||
|
waist = scaleData.waist;
|
||||||
|
hip = scaleData.hip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getBMI() {
|
||||||
|
return weight / ((body_height / 100.0f)*(body_height / 100.0f));
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getWHtR() {
|
||||||
|
return waist / (float)body_height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getWHR() {
|
||||||
|
if (hip == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return waist / hip;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -49,22 +49,6 @@ public class ScaleUser {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getBMI(float weight) {
|
|
||||||
return weight / ((body_height / 100.0f)*(body_height / 100.0f));
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getWHtR(float waist) {
|
|
||||||
return waist / (float)body_height;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getWHR(float waist, float hip) {
|
|
||||||
if (hip == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return waist / hip;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
|
@@ -41,6 +41,7 @@ import com.health.openscale.core.EvaluationResult;
|
|||||||
import com.health.openscale.core.EvaluationSheet;
|
import com.health.openscale.core.EvaluationSheet;
|
||||||
import com.health.openscale.core.LinearGaugeView;
|
import com.health.openscale.core.LinearGaugeView;
|
||||||
import com.health.openscale.core.OpenScale;
|
import com.health.openscale.core.OpenScale;
|
||||||
|
import com.health.openscale.core.ScaleCalculator;
|
||||||
import com.health.openscale.core.ScaleData;
|
import com.health.openscale.core.ScaleData;
|
||||||
import com.health.openscale.core.ScaleUser;
|
import com.health.openscale.core.ScaleUser;
|
||||||
|
|
||||||
@@ -359,16 +360,20 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
linearGaugeHip.setMinMaxValue(30, 200);
|
linearGaugeHip.setMinMaxValue(30, 200);
|
||||||
linearGaugeWHR.setMinMaxValue(0, 1);
|
linearGaugeWHR.setMinMaxValue(0, 1);
|
||||||
|
|
||||||
|
ScaleCalculator calculator = new ScaleCalculator();
|
||||||
|
calculator.body_height = currentScaleUser.body_height;
|
||||||
|
calculator.setScaleData(lastScaleData);
|
||||||
|
|
||||||
EvaluationSheet evalSheet = new EvaluationSheet(currentScaleUser);
|
EvaluationSheet evalSheet = new EvaluationSheet(currentScaleUser);
|
||||||
|
|
||||||
EvaluationResult sheetWeight = evalSheet.evaluateWeight(lastScaleData.weight);
|
EvaluationResult sheetWeight = evalSheet.evaluateWeight(lastScaleData.weight);
|
||||||
EvaluationResult sheetBMI = evalSheet.evaluateBMI(currentScaleUser.getBMI(lastScaleData.weight));
|
EvaluationResult sheetBMI = evalSheet.evaluateBMI(calculator.getBMI());
|
||||||
EvaluationResult sheetFat = evalSheet.evaluateBodyFat(lastScaleData.fat);
|
EvaluationResult sheetFat = evalSheet.evaluateBodyFat(lastScaleData.fat);
|
||||||
EvaluationResult sheetMuscle = evalSheet.evaluateBodyMuscle(lastScaleData.muscle);
|
EvaluationResult sheetMuscle = evalSheet.evaluateBodyMuscle(lastScaleData.muscle);
|
||||||
EvaluationResult sheetWater = evalSheet.evaluateBodyWater(lastScaleData.water);
|
EvaluationResult sheetWater = evalSheet.evaluateBodyWater(lastScaleData.water);
|
||||||
EvaluationResult sheetWaist = evalSheet.evaluateWaist(lastScaleData.waist);
|
EvaluationResult sheetWaist = evalSheet.evaluateWaist(lastScaleData.waist);
|
||||||
EvaluationResult sheetWHtR = evalSheet.evaluateWHtR(currentScaleUser.getWHtR(lastScaleData.waist));
|
EvaluationResult sheetWHtR = evalSheet.evaluateWHtR(calculator.getWHtR());
|
||||||
EvaluationResult sheetWHR = evalSheet.evaluateWHR(currentScaleUser.getWHR(lastScaleData.waist, lastScaleData.hip));
|
EvaluationResult sheetWHR = evalSheet.evaluateWHR(calculator.getWHR());
|
||||||
|
|
||||||
updateIndicator((ImageView)overviewView.findViewById(R.id.indicatorWeight), sheetWeight.eval_state);
|
updateIndicator((ImageView)overviewView.findViewById(R.id.indicatorWeight), sheetWeight.eval_state);
|
||||||
updateIndicator((ImageView)overviewView.findViewById(R.id.indicatorBMI), sheetBMI.eval_state);
|
updateIndicator((ImageView)overviewView.findViewById(R.id.indicatorBMI), sheetBMI.eval_state);
|
||||||
@@ -391,14 +396,14 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
linearGaugeWHR.setLimits(sheetWHR.lowLimit, sheetWHR.highLimit);
|
linearGaugeWHR.setLimits(sheetWHR.lowLimit, sheetWHR.highLimit);
|
||||||
|
|
||||||
linearGaugeWeight.setValue(lastScaleData.weight);
|
linearGaugeWeight.setValue(lastScaleData.weight);
|
||||||
linearGaugeBMI.setValue(currentScaleUser.getBMI(lastScaleData.weight));
|
linearGaugeBMI.setValue(calculator.getBMI());
|
||||||
linearGaugeFat.setValue(lastScaleData.fat);
|
linearGaugeFat.setValue(lastScaleData.fat);
|
||||||
linearGaugeMuscle.setValue(lastScaleData.muscle);
|
linearGaugeMuscle.setValue(lastScaleData.muscle);
|
||||||
linearGaugeWater.setValue(lastScaleData.water);
|
linearGaugeWater.setValue(lastScaleData.water);
|
||||||
linearGaugeWaist.setValue(lastScaleData.waist);
|
linearGaugeWaist.setValue(lastScaleData.waist);
|
||||||
linearGaugeWHtR.setValue(currentScaleUser.getWHtR(lastScaleData.waist));
|
linearGaugeWHtR.setValue(calculator.getWHtR());
|
||||||
linearGaugeHip.setValue(lastScaleData.hip);
|
linearGaugeHip.setValue(lastScaleData.hip);
|
||||||
linearGaugeWHR.setValue(currentScaleUser.getWHR(lastScaleData.waist, lastScaleData.hip));
|
linearGaugeWHR.setValue(calculator.getWHR());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateIndicator(ImageView view, EvaluationResult.EVAL_STATE state) {
|
private void updateIndicator(ImageView view, EvaluationResult.EVAL_STATE state) {
|
||||||
@@ -420,15 +425,19 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateLastMeasurement() {
|
private void updateLastMeasurement() {
|
||||||
|
ScaleCalculator calculator = new ScaleCalculator();
|
||||||
|
calculator.body_height = currentScaleUser.body_height;
|
||||||
|
calculator.setScaleData(lastScaleData);
|
||||||
|
|
||||||
txtWeightLast.setText(lastScaleData.weight + " " + ScaleUser.UNIT_STRING[currentScaleUser.scale_unit]);
|
txtWeightLast.setText(lastScaleData.weight + " " + ScaleUser.UNIT_STRING[currentScaleUser.scale_unit]);
|
||||||
txtBMILast.setText(String.format("%.1f", currentScaleUser.getBMI(lastScaleData.weight)));
|
txtBMILast.setText(String.format("%.1f", calculator.getBMI()));
|
||||||
txtFatLast.setText(lastScaleData.fat + " %");
|
txtFatLast.setText(lastScaleData.fat + " %");
|
||||||
txtWaterLast.setText(lastScaleData.water + " %");
|
txtWaterLast.setText(lastScaleData.water + " %");
|
||||||
txtMuscleLast.setText(lastScaleData.muscle + " %");
|
txtMuscleLast.setText(lastScaleData.muscle + " %");
|
||||||
txtWaistLast.setText(lastScaleData.waist + " cm");
|
txtWaistLast.setText(lastScaleData.waist + " cm");
|
||||||
txtWHtRLast.setText(String.format("%.2f", currentScaleUser.getWHtR(lastScaleData.waist)));
|
txtWHtRLast.setText(String.format("%.2f", calculator.getWHtR()));
|
||||||
txtHipLast.setText(lastScaleData.hip + " cm");
|
txtHipLast.setText(lastScaleData.hip + " cm");
|
||||||
txtWHRLast.setText(String.format("%.2f", currentScaleUser.getWHR(lastScaleData.waist, lastScaleData.hip)));
|
txtWHRLast.setText(String.format("%.2f", calculator.getWHR()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateGoal(ArrayList<ScaleData> scaleDataList) {
|
private void updateGoal(ArrayList<ScaleData> scaleDataList) {
|
||||||
@@ -444,9 +453,40 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
long days = daysBetween(curDate, goalDate);
|
long days = daysBetween(curDate, goalDate);
|
||||||
txtGoalDayLeft.setText(days + " " + getResources().getString(R.string.label_days));
|
txtGoalDayLeft.setText(days + " " + getResources().getString(R.string.label_days));
|
||||||
|
|
||||||
txtLabelGoalWeight.setText(Html.fromHtml(getResources().getString(R.string.label_goal_weight) + " <br> <font color='grey'><small>BMI " + String.format("%.1f", currentScaleUser.getBMI(currentScaleUser.goal_weight)) + " </small></font>"));
|
ScaleCalculator currentCalculator = new ScaleCalculator();
|
||||||
txtLabelGoalDiff.setText(Html.fromHtml(getResources().getString(R.string.label_weight_difference) + " <br> <font color='grey'><small>BMI " + String.format("%.1f", currentScaleUser.getBMI(lastScaleData.weight) - currentScaleUser.getBMI(currentScaleUser.goal_weight)) + " </small></font>"));
|
currentCalculator.body_height = currentScaleUser.body_height;
|
||||||
txtLabelDayLeft.setText(Html.fromHtml(getResources().getString(R.string.label_days_left) + " <br> <font color='grey'><small>" + getResources().getString(R.string.label_goal_date_is) + " " + DateFormat.getDateInstance(DateFormat.LONG).format(currentScaleUser.goal_date) + " </small></font>")); // currentScaleUser.goal_date
|
currentCalculator.weight = lastScaleData.weight;
|
||||||
|
|
||||||
|
ScaleCalculator goalCalculator = new ScaleCalculator();
|
||||||
|
goalCalculator.body_height = currentScaleUser.body_height;
|
||||||
|
goalCalculator.weight = currentScaleUser.goal_weight;
|
||||||
|
|
||||||
|
txtLabelGoalWeight.setText(
|
||||||
|
Html.fromHtml(
|
||||||
|
getResources().getString(R.string.label_goal_weight) +
|
||||||
|
" <br> <font color='grey'><small>BMI " +
|
||||||
|
String.format("%.1f", goalCalculator.getBMI()) +
|
||||||
|
" </small></font>"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
txtLabelGoalDiff.setText(
|
||||||
|
Html.fromHtml(
|
||||||
|
getResources().getString(R.string.label_weight_difference) +
|
||||||
|
" <br> <font color='grey'><small>BMI " +
|
||||||
|
String.format("%.1f", currentCalculator.getBMI() - goalCalculator.getBMI()) +
|
||||||
|
" </small></font>"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
txtLabelDayLeft.setText(
|
||||||
|
Html.fromHtml(
|
||||||
|
getResources().getString(R.string.label_days_left) +
|
||||||
|
" <br> <font color='grey'><small>" +
|
||||||
|
getResources().getString(R.string.label_goal_date_is) +
|
||||||
|
" "
|
||||||
|
+ DateFormat.getDateInstance(DateFormat.LONG).format(currentScaleUser.goal_date) +
|
||||||
|
" </small></font>"
|
||||||
|
)
|
||||||
|
); // currentScaleUser.goal_date
|
||||||
|
|
||||||
ListIterator<ScaleData> scaleDataIterator = scaleDataList.listIterator();
|
ListIterator<ScaleData> scaleDataIterator = scaleDataList.listIterator();
|
||||||
|
|
||||||
@@ -457,6 +497,14 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
if (scaleDataIterator.hasNext()) {
|
if (scaleDataIterator.hasNext()) {
|
||||||
ScaleData diffScaleData = scaleDataIterator.next();
|
ScaleData diffScaleData = scaleDataIterator.next();
|
||||||
|
|
||||||
|
ScaleCalculator lastScaleCalculator = new ScaleCalculator();
|
||||||
|
lastScaleCalculator.body_height = currentScaleUser.body_height;
|
||||||
|
lastScaleCalculator.setScaleData(lastScaleData);
|
||||||
|
|
||||||
|
ScaleCalculator diffScaleCalculator = new ScaleCalculator();
|
||||||
|
diffScaleCalculator.body_height = currentScaleUser.body_height;
|
||||||
|
diffScaleCalculator.setScaleData(diffScaleData);
|
||||||
|
|
||||||
ScaleDiff.setDiff(
|
ScaleDiff.setDiff(
|
||||||
txtLabelWeight,
|
txtLabelWeight,
|
||||||
lastScaleData.weight - diffScaleData.weight,
|
lastScaleData.weight - diffScaleData.weight,
|
||||||
@@ -466,7 +514,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
);
|
);
|
||||||
ScaleDiff.setDiff(
|
ScaleDiff.setDiff(
|
||||||
txtLabelBMI,
|
txtLabelBMI,
|
||||||
currentScaleUser.getBMI(lastScaleData.weight) - currentScaleUser.getBMI(diffScaleData.weight),
|
lastScaleCalculator.getBMI() - diffScaleCalculator.getBMI(),
|
||||||
getResources().getString(R.string.label_bmi),
|
getResources().getString(R.string.label_bmi),
|
||||||
"%.1f ",
|
"%.1f ",
|
||||||
""
|
""
|
||||||
@@ -501,7 +549,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
);
|
);
|
||||||
ScaleDiff.setDiff(
|
ScaleDiff.setDiff(
|
||||||
txtLabelWHtR,
|
txtLabelWHtR,
|
||||||
currentScaleUser.getWHtR(lastScaleData.waist) - currentScaleUser.getWHtR(diffScaleData.waist),
|
lastScaleCalculator.getWHtR() - diffScaleCalculator.getWHtR(),
|
||||||
getResources().getString(R.string.label_whtr),
|
getResources().getString(R.string.label_whtr),
|
||||||
"%.2f ",
|
"%.2f ",
|
||||||
""
|
""
|
||||||
@@ -515,7 +563,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
);
|
);
|
||||||
ScaleDiff.setDiff(
|
ScaleDiff.setDiff(
|
||||||
txtLabelWHR,
|
txtLabelWHR,
|
||||||
currentScaleUser.getWHR(lastScaleData.waist, lastScaleData.hip) - currentScaleUser.getWHR(diffScaleData.waist, diffScaleData.hip),
|
lastScaleCalculator.getWHR() - diffScaleCalculator.getWHR(),
|
||||||
getResources().getString(R.string.label_whr),
|
getResources().getString(R.string.label_whr),
|
||||||
"%.2f ",
|
"%.2f ",
|
||||||
""
|
""
|
||||||
@@ -564,32 +612,36 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
|||||||
{
|
{
|
||||||
histDate.setTime(scaleData.date_time);
|
histDate.setTime(scaleData.date_time);
|
||||||
|
|
||||||
|
ScaleCalculator calculator = new ScaleCalculator();
|
||||||
|
calculator.body_height = currentScaleUser.body_height;
|
||||||
|
calculator.setScaleData(scaleData);
|
||||||
|
|
||||||
if (weekPastDate.before(histDate)) {
|
if (weekPastDate.before(histDate)) {
|
||||||
weekSize++;
|
weekSize++;
|
||||||
|
|
||||||
weekAvgWeight += scaleData.weight;
|
weekAvgWeight += scaleData.weight;
|
||||||
weekAvgBMI += currentScaleUser.getBMI(scaleData.weight);
|
weekAvgBMI += calculator.getBMI();
|
||||||
weekAvgFat += scaleData.fat;
|
weekAvgFat += scaleData.fat;
|
||||||
weekAvgWater += scaleData.water;
|
weekAvgWater += scaleData.water;
|
||||||
weekAvgMuscle += scaleData.muscle;
|
weekAvgMuscle += scaleData.muscle;
|
||||||
weekAvgWaist += scaleData.waist;
|
weekAvgWaist += scaleData.waist;
|
||||||
weekAvgHip += scaleData.hip;
|
weekAvgHip += scaleData.hip;
|
||||||
weekAvgWHtR += currentScaleUser.getWHtR(scaleData.waist);
|
weekAvgWHtR += calculator.getWHtR();
|
||||||
weekAvgWHR += currentScaleUser.getWHR(scaleData.waist, scaleData.hip);
|
weekAvgWHR += calculator.getWHR();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (monthPastDate.before(histDate)) {
|
if (monthPastDate.before(histDate)) {
|
||||||
monthSize++;
|
monthSize++;
|
||||||
|
|
||||||
monthAvgWeight += scaleData.weight;
|
monthAvgWeight += scaleData.weight;
|
||||||
monthAvgBMI += currentScaleUser.getBMI(scaleData.weight);
|
monthAvgBMI += calculator.getBMI();
|
||||||
monthAvgFat += scaleData.fat;
|
monthAvgFat += scaleData.fat;
|
||||||
monthAvgWater += scaleData.water;
|
monthAvgWater += scaleData.water;
|
||||||
monthAvgMuscle += scaleData.muscle;
|
monthAvgMuscle += scaleData.muscle;
|
||||||
monthAvgWaist += scaleData.waist;
|
monthAvgWaist += scaleData.waist;
|
||||||
monthAvgHip += scaleData.hip;
|
monthAvgHip += scaleData.hip;
|
||||||
monthAvgWHtR += currentScaleUser.getWHtR(scaleData.waist);
|
monthAvgWHtR += calculator.getWHtR();
|
||||||
monthAvgWHR += currentScaleUser.getWHR(scaleData.waist, scaleData.hip);
|
monthAvgWHR += calculator.getWHR();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user