1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 23:41:45 +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;
}
@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
protected float getMeasurementValue(ScaleMeasurement measurement) {
return measurement.getBMR(getScaleUser());
@@ -71,6 +63,11 @@ public class BMRMeasurementView extends FloatMeasurementView {
return 5000;
}
@Override
protected int getDecimalPlaces() {
return 0;
}
@Override
public int getColor() {
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));
}
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) {
value = newValue;
evaluationResult = null;
@@ -221,11 +226,10 @@ public abstract class FloatMeasurementView extends MeasurementView {
setValue(clampValue(value - INC_DEC_DELTA), previousValue, true);
}
protected String formatValue(float value, boolean withUnit) {
if (withUnit && !getUnit().isEmpty()) {
return String.format(Locale.getDefault(), "%.2f %s", value, getUnit());
}
return String.format(Locale.getDefault(), "%.2f", value);
private String formatValue(float value, boolean withUnit) {
final String format = String.format(Locale.getDefault(), "%%.%df%s",
getDecimalPlaces(), withUnit && !getUnit().isEmpty() ? " %s" : "");
return String.format(Locale.getDefault(), format, value, getUnit());
}
protected String formatValue(float value) {
@@ -237,6 +241,9 @@ public abstract class FloatMeasurementView extends MeasurementView {
public abstract String getUnit();
protected abstract float getMaxValue();
protected int getDecimalPlaces() {
return 2;
}
public abstract int getColor();
@@ -335,6 +342,7 @@ public abstract class FloatMeasurementView extends MeasurementView {
newValue = getMeasurementValue(measurement);
newValue = maybeConvertValue(newValue);
newValue = clampValue(newValue);
newValue = roundValue(newValue);
if (previousMeasurement != null) {
float saveUserConvertedWeight = userConvertedWeight;
@@ -343,6 +351,7 @@ public abstract class FloatMeasurementView extends MeasurementView {
newPreviousValue = getMeasurementValue(previousMeasurement);
newPreviousValue = maybeConvertValue(newPreviousValue);
newPreviousValue = clampValue(newPreviousValue);
newPreviousValue = roundValue(newPreviousValue);
userConvertedWeight = saveUserConvertedWeight;
}