1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-21 16:02:04 +02:00

Round measurement value when loading it

So that the diff value is correctly calculated even when there is a
non-visible difference between current and previous value.
This commit is contained in:
Erik Johansson
2018-05-13 23:16:07 +02:00
parent b9763ec79f
commit 5e481141c1
2 changed files with 19 additions and 13 deletions

View File

@@ -43,14 +43,6 @@ public class BMRMeasurementView extends FloatMeasurementView {
return false; return false;
} }
@Override
protected String formatValue(float value, boolean withUnit) {
if (withUnit) {
return String.format(Locale.getDefault(), "%d %s", Math.round(value), getUnit());
}
return String.format(Locale.getDefault(), "%d", Math.round(value));
}
@Override @Override
protected float getMeasurementValue(ScaleMeasurement measurement) { protected float getMeasurementValue(ScaleMeasurement measurement) {
return measurement.getBMR(getScaleUser()); return measurement.getBMR(getScaleUser());
@@ -71,6 +63,11 @@ public class BMRMeasurementView extends FloatMeasurementView {
return 5000; return 5000;
} }
@Override
protected int getDecimalPlaces() {
return 0;
}
@Override @Override
public int getColor() { public int getColor() {
return Color.parseColor("#26A69A"); return Color.parseColor("#26A69A");

View File

@@ -130,6 +130,11 @@ public abstract class FloatMeasurementView extends MeasurementView {
return Math.max(0.0f, Math.min(getMaxValue(), value)); return Math.max(0.0f, Math.min(getMaxValue(), value));
} }
private float roundValue(float value) {
final float factor = (float) Math.pow(10, getDecimalPlaces());
return Math.round(value * factor) / factor;
}
private void setValueInner(float newValue, boolean callListener) { private void setValueInner(float newValue, boolean callListener) {
value = newValue; value = newValue;
evaluationResult = null; evaluationResult = null;
@@ -221,11 +226,10 @@ public abstract class FloatMeasurementView extends MeasurementView {
setValue(clampValue(value - INC_DEC_DELTA), previousValue, true); setValue(clampValue(value - INC_DEC_DELTA), previousValue, true);
} }
protected String formatValue(float value, boolean withUnit) { private String formatValue(float value, boolean withUnit) {
if (withUnit && !getUnit().isEmpty()) { final String format = String.format(Locale.getDefault(), "%%.%df%s",
return String.format(Locale.getDefault(), "%.2f %s", value, getUnit()); getDecimalPlaces(), withUnit && !getUnit().isEmpty() ? " %s" : "");
} return String.format(Locale.getDefault(), format, value, getUnit());
return String.format(Locale.getDefault(), "%.2f", value);
} }
protected String formatValue(float value) { protected String formatValue(float value) {
@@ -237,6 +241,9 @@ public abstract class FloatMeasurementView extends MeasurementView {
public abstract String getUnit(); public abstract String getUnit();
protected abstract float getMaxValue(); protected abstract float getMaxValue();
protected int getDecimalPlaces() {
return 2;
}
public abstract int getColor(); public abstract int getColor();
@@ -335,6 +342,7 @@ public abstract class FloatMeasurementView extends MeasurementView {
newValue = getMeasurementValue(measurement); newValue = getMeasurementValue(measurement);
newValue = maybeConvertValue(newValue); newValue = maybeConvertValue(newValue);
newValue = clampValue(newValue); newValue = clampValue(newValue);
newValue = roundValue(newValue);
if (previousMeasurement != null) { if (previousMeasurement != null) {
float saveUserConvertedWeight = userConvertedWeight; float saveUserConvertedWeight = userConvertedWeight;
@@ -343,6 +351,7 @@ public abstract class FloatMeasurementView extends MeasurementView {
newPreviousValue = getMeasurementValue(previousMeasurement); newPreviousValue = getMeasurementValue(previousMeasurement);
newPreviousValue = maybeConvertValue(newPreviousValue); newPreviousValue = maybeConvertValue(newPreviousValue);
newPreviousValue = clampValue(newPreviousValue); newPreviousValue = clampValue(newPreviousValue);
newPreviousValue = roundValue(newPreviousValue);
userConvertedWeight = saveUserConvertedWeight; userConvertedWeight = saveUserConvertedWeight;
} }