diff --git a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java index 475d9966..49aa3ea8 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java @@ -286,13 +286,6 @@ public class OpenScale { scaleMeasurement.setWater(waterMetric.getWater(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement)); } - settings = new MeasurementViewSettings(prefs, LBMMeasurementView.KEY); - if (settings.isEnabled() && settings.isEstimationEnabled()) { - EstimatedLBMMetric lbmMetric = EstimatedLBMMetric.getEstimatedMetric( - EstimatedLBMMetric.FORMULA.valueOf(settings.getEstimationFormula())); - scaleMeasurement.setLbm(lbmMetric.getLBM(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement)); - } - settings = new MeasurementViewSettings(prefs, FatMeasurementView.KEY); if (settings.isEnabled() && settings.isEstimationEnabled()) { EstimatedFatMetric fatMetric = EstimatedFatMetric.getEstimatedMetric( @@ -300,6 +293,14 @@ public class OpenScale { scaleMeasurement.setFat(fatMetric.getFat(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement)); } + // Must be after fat estimation as one formula is based on fat + settings = new MeasurementViewSettings(prefs, LBMMeasurementView.KEY); + if (settings.isEnabled() && settings.isEstimationEnabled()) { + EstimatedLBMMetric lbmMetric = EstimatedLBMMetric.getEstimatedMetric( + EstimatedLBMMetric.FORMULA.valueOf(settings.getEstimationFormula())); + scaleMeasurement.setLbm(lbmMetric.getLBM(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement)); + } + if (measurementDAO.insert(scaleMeasurement) != -1) { ScaleUser scaleUser = getScaleUser(scaleMeasurement.getUserId()); diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedLBMMetric.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedLBMMetric.java index 0b5e5b7b..334e6175 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedLBMMetric.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedLBMMetric.java @@ -15,12 +15,14 @@ */ package com.health.openscale.core.bodymetric; +import android.content.Context; + import com.health.openscale.core.datatypes.ScaleMeasurement; import com.health.openscale.core.datatypes.ScaleUser; public abstract class EstimatedLBMMetric { // Don't change enum names, they are stored persistent in preferences - public enum FORMULA { LBW_HUME, LBW_BOER } + public enum FORMULA { LBW_HUME, LBW_BOER, LBW_WEIGHT_MINUS_FAT } public static EstimatedLBMMetric getEstimatedMetric(FORMULA metric) { switch (metric) { @@ -28,11 +30,13 @@ public abstract class EstimatedLBMMetric { return new LBMHume(); case LBW_BOER: return new LBMBoer(); + case LBW_WEIGHT_MINUS_FAT: + return new LBMWeightMinusFat(); } return null; } - public abstract String getName(); + public abstract String getName(Context context); public abstract float getLBM(ScaleUser user, ScaleMeasurement data); } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMBoer.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMBoer.java index e227f4c3..05be7bda 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMBoer.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMBoer.java @@ -16,12 +16,14 @@ package com.health.openscale.core.bodymetric; +import android.content.Context; + import com.health.openscale.core.datatypes.ScaleMeasurement; import com.health.openscale.core.datatypes.ScaleUser; public class LBMBoer extends EstimatedLBMMetric { @Override - public String getName() { + public String getName(Context context) { return "Boer (1984)"; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMHume.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMHume.java index 8ef294b9..0518271c 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMHume.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMHume.java @@ -16,12 +16,14 @@ package com.health.openscale.core.bodymetric; +import android.content.Context; + import com.health.openscale.core.datatypes.ScaleMeasurement; import com.health.openscale.core.datatypes.ScaleUser; public class LBMHume extends EstimatedLBMMetric { @Override - public String getName() { + public String getName(Context context) { return "Hume (1966)"; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMWeightMinusFat.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMWeightMinusFat.java new file mode 100644 index 00000000..90129dba --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/LBMWeightMinusFat.java @@ -0,0 +1,42 @@ +/* Copyright (C) 2018 Erik Johansson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.health.openscale.core.bodymetric; + +import android.content.Context; + +import com.health.openscale.R; +import com.health.openscale.core.datatypes.ScaleMeasurement; +import com.health.openscale.core.datatypes.ScaleUser; + +class LBMWeightMinusFat extends EstimatedLBMMetric { + @Override + public String getName(Context context) { + return String.format("%s - %s", + context.getResources().getString(R.string.label_weight), + context.getResources().getString(R.string.label_fat)); + } + + @Override + public float getLBM(ScaleUser user, ScaleMeasurement data) { + if (data.getFat() == 0) { + return 0; + } + + float absFat = data.getWeight() * data.getFat() / 100.0f; + return data.getWeight() - absFat; + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/LBMMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/LBMMeasurementView.java index 058f0d66..1635360a 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/LBMMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/LBMMeasurementView.java @@ -75,7 +75,7 @@ public class LBMMeasurementView extends FloatMeasurementView { int idx = 0; for (EstimatedLBMMetric.FORMULA formula : EstimatedLBMMetric.FORMULA.values()) { - entries[idx] = EstimatedLBMMetric.getEstimatedMetric(formula).getName(); + entries[idx] = EstimatedLBMMetric.getEstimatedMetric(formula).getName(getContext()); values[idx] = formula.name(); ++idx; }