mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-20 23:41:45 +02:00
Move handling of estimation/percentage enabled to base class
This commit is contained in:
@@ -16,7 +16,6 @@
|
|||||||
package com.health.openscale.gui.views;
|
package com.health.openscale.gui.views;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
|
||||||
@@ -29,9 +28,6 @@ public class FatMeasurementView extends FloatMeasurementView {
|
|||||||
public static final String KEY = "fat";
|
public static final String KEY = "fat";
|
||||||
private static final String[] DEPENDENCY = {WeightMeasurementView.KEY};
|
private static final String[] DEPENDENCY = {WeightMeasurementView.KEY};
|
||||||
|
|
||||||
private boolean estimateFatEnable;
|
|
||||||
private boolean percentageEnable;
|
|
||||||
|
|
||||||
public FatMeasurementView(Context context) {
|
public FatMeasurementView(Context context) {
|
||||||
super(context, context.getResources().getString(R.string.label_fat), ContextCompat.getDrawable(context, R.drawable.ic_fat));
|
super(context, context.getResources().getString(R.string.label_fat), ContextCompat.getDrawable(context, R.drawable.ic_fat));
|
||||||
}
|
}
|
||||||
@@ -47,15 +43,8 @@ public class FatMeasurementView extends FloatMeasurementView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updatePreferences(SharedPreferences preferences) {
|
protected boolean canConvertPercentageToAbsoluteWeight() {
|
||||||
super.updatePreferences(preferences);
|
return true;
|
||||||
estimateFatEnable = preferences.getBoolean("estimateFatEnable", false);
|
|
||||||
percentageEnable = preferences.getBoolean("fatPercentageEnable", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean shouldConvertPercentageToAbsoluteWeight() {
|
|
||||||
return !percentageEnable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -70,11 +59,11 @@ public class FatMeasurementView extends FloatMeasurementView {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUnit() {
|
public String getUnit() {
|
||||||
if (percentageEnable) {
|
if (shouldConvertPercentageToAbsoluteWeight()) {
|
||||||
return "%";
|
return getScaleUser().getScaleUnit().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return getScaleUser().getScaleUnit().toString();
|
return "%";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -88,9 +77,7 @@ public class FatMeasurementView extends FloatMeasurementView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isEstimationEnabled() {
|
protected boolean isEstimationSupported() { return true; }
|
||||||
return estimateFatEnable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected EvaluationResult evaluateSheet(EvaluationSheet evalSheet, float value) {
|
protected EvaluationResult evaluateSheet(EvaluationSheet evalSheet, float value) {
|
||||||
|
@@ -227,18 +227,20 @@ public abstract class FloatMeasurementView extends MeasurementView {
|
|||||||
|
|
||||||
public abstract int getColor();
|
public abstract int getColor();
|
||||||
|
|
||||||
protected boolean isEstimationEnabled() {
|
protected boolean isEstimationSupported() { return false; }
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract EvaluationResult evaluateSheet(EvaluationSheet evalSheet, float value);
|
protected abstract EvaluationResult evaluateSheet(EvaluationSheet evalSheet, float value);
|
||||||
|
|
||||||
private boolean useAutoValue() {
|
private boolean useAutoValue() {
|
||||||
return isEstimationEnabled() && getMeasurementMode() == MeasurementViewMode.ADD;
|
return isEstimationSupported()
|
||||||
|
&& getSettings().isEstimationEnabled()
|
||||||
|
&& getMeasurementMode() == MeasurementViewMode.ADD;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean canConvertPercentageToAbsoluteWeight() { return false; }
|
||||||
protected boolean shouldConvertPercentageToAbsoluteWeight() {
|
protected boolean shouldConvertPercentageToAbsoluteWeight() {
|
||||||
return false;
|
return canConvertPercentageToAbsoluteWeight()
|
||||||
|
&& !getSettings().isPercentageEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
private float makeAbsoluteWeight(float percentage) {
|
private float makeAbsoluteWeight(float percentage) {
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
package com.health.openscale.gui.views;
|
package com.health.openscale.gui.views;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
|
||||||
@@ -29,8 +28,6 @@ public class LBWMeasurementView extends FloatMeasurementView {
|
|||||||
public static final String KEY = "lbw";
|
public static final String KEY = "lbw";
|
||||||
private static final String[] DEPENDENCY = {};
|
private static final String[] DEPENDENCY = {};
|
||||||
|
|
||||||
private boolean estimateLBWEnable;
|
|
||||||
|
|
||||||
public LBWMeasurementView(Context context) {
|
public LBWMeasurementView(Context context) {
|
||||||
super(context, context.getResources().getString(R.string.label_lbw), ContextCompat.getDrawable(context, R.drawable.ic_lbw));
|
super(context, context.getResources().getString(R.string.label_lbw), ContextCompat.getDrawable(context, R.drawable.ic_lbw));
|
||||||
}
|
}
|
||||||
@@ -45,12 +42,6 @@ public class LBWMeasurementView extends FloatMeasurementView {
|
|||||||
return DEPENDENCY;
|
return DEPENDENCY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updatePreferences(SharedPreferences preferences) {
|
|
||||||
super.updatePreferences(preferences);
|
|
||||||
estimateLBWEnable = preferences.getBoolean("estimateLBWEnable", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected float getMeasurementValue(ScaleMeasurement measurement) {
|
protected float getMeasurementValue(ScaleMeasurement measurement) {
|
||||||
return measurement.getLbw();
|
return measurement.getLbw();
|
||||||
@@ -77,9 +68,7 @@ public class LBWMeasurementView extends FloatMeasurementView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isEstimationEnabled() {
|
protected boolean isEstimationSupported() { return true; }
|
||||||
return estimateLBWEnable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected EvaluationResult evaluateSheet(EvaluationSheet evalSheet, float value) {
|
protected EvaluationResult evaluateSheet(EvaluationSheet evalSheet, float value) {
|
||||||
|
@@ -62,7 +62,7 @@ public abstract class MeasurementView extends TableLayout {
|
|||||||
|
|
||||||
public static String PREF_MEASUREMENT_ORDER = "measurementOrder";
|
public static String PREF_MEASUREMENT_ORDER = "measurementOrder";
|
||||||
|
|
||||||
private static String PREFERENCE_SUFFIX_ENABLE = "Enable";
|
private MeasurementViewSettings settings;
|
||||||
|
|
||||||
private TableRow measurementRow;
|
private TableRow measurementRow;
|
||||||
private ImageView iconView;
|
private ImageView iconView;
|
||||||
@@ -78,13 +78,12 @@ public abstract class MeasurementView extends TableLayout {
|
|||||||
private MeasurementViewUpdateListener updateListener = null;
|
private MeasurementViewUpdateListener updateListener = null;
|
||||||
private MeasurementViewMode measurementMode = VIEW;
|
private MeasurementViewMode measurementMode = VIEW;
|
||||||
|
|
||||||
private static SharedPreferences prefs;
|
private boolean isDisabledByDependency = false;
|
||||||
|
|
||||||
private boolean updateViews = true;
|
private boolean updateViews = true;
|
||||||
|
|
||||||
public MeasurementView(Context context, String text, Drawable icon) {
|
public MeasurementView(Context context, String text, Drawable icon) {
|
||||||
super(context);
|
super(context);
|
||||||
prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
String app_theme = prefs.getString("app_theme", "Light");
|
String app_theme = prefs.getString("app_theme", "Light");
|
||||||
|
|
||||||
if (app_theme.equals("Dark")) {
|
if (app_theme.equals("Dark")) {
|
||||||
@@ -99,7 +98,10 @@ public abstract class MeasurementView extends TableLayout {
|
|||||||
|
|
||||||
public enum DateTimeOrder { FIRST, LAST, NONE }
|
public enum DateTimeOrder { FIRST, LAST, NONE }
|
||||||
|
|
||||||
public static final List<MeasurementView> getMeasurementList(Context context, DateTimeOrder dateTimeOrder) {
|
public static final List<MeasurementView> getMeasurementList(
|
||||||
|
Context context, DateTimeOrder dateTimeOrder) {
|
||||||
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
|
||||||
final List<MeasurementView> sorted = new ArrayList<>();
|
final List<MeasurementView> sorted = new ArrayList<>();
|
||||||
if (dateTimeOrder == DateTimeOrder.FIRST) {
|
if (dateTimeOrder == DateTimeOrder.FIRST) {
|
||||||
sorted.add(new DateMeasurementView(context));
|
sorted.add(new DateMeasurementView(context));
|
||||||
@@ -247,8 +249,12 @@ public abstract class MeasurementView extends TableLayout {
|
|||||||
public abstract String getKey();
|
public abstract String getKey();
|
||||||
public abstract String[] getDependencyKeys();
|
public abstract String[] getDependencyKeys();
|
||||||
|
|
||||||
public static String getPreferenceKey(String key, String suffix) {
|
public MeasurementViewSettings getSettings() {
|
||||||
return key + suffix;
|
if (settings == null) {
|
||||||
|
settings = new MeasurementViewSettings(
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(getContext()), getKey());
|
||||||
|
}
|
||||||
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement);
|
public abstract void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement);
|
||||||
@@ -259,19 +265,16 @@ public abstract class MeasurementView extends TableLayout {
|
|||||||
|
|
||||||
@CallSuper
|
@CallSuper
|
||||||
public void updatePreferences(SharedPreferences prefs) {
|
public void updatePreferences(SharedPreferences prefs) {
|
||||||
boolean enable = prefs.getBoolean(
|
isDisabledByDependency = false;
|
||||||
getPreferenceKey(getKey(), PREFERENCE_SUFFIX_ENABLE), true);
|
for (String dep : getDependencyKeys()) {
|
||||||
|
MeasurementViewSettings depSettings = new MeasurementViewSettings(prefs, dep);
|
||||||
if (enable) {
|
if (!depSettings.isEnabled()) {
|
||||||
for (String dep : getDependencyKeys()) {
|
isDisabledByDependency = true;
|
||||||
if (!prefs.getBoolean(
|
break;
|
||||||
getPreferenceKey(dep, PREFERENCE_SUFFIX_ENABLE), true)) {
|
|
||||||
enable = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean enable = !isDisabledByDependency && getSettings().isEnabled();
|
||||||
setVisible(enable);
|
setVisible(enable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,6 +356,10 @@ public abstract class MeasurementView extends TableLayout {
|
|||||||
showEvaluatorRow(false);
|
showEvaluatorRow(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getIsDisabledByDependency() {
|
||||||
|
return isDisabledByDependency;
|
||||||
|
}
|
||||||
|
|
||||||
protected void setVisible(boolean isVisible) {
|
protected void setVisible(boolean isVisible) {
|
||||||
if (isVisible) {
|
if (isVisible) {
|
||||||
measurementRow.setVisibility(View.VISIBLE);
|
measurementRow.setVisibility(View.VISIBLE);
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
package com.health.openscale.gui.views;
|
package com.health.openscale.gui.views;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
|
||||||
@@ -29,8 +28,6 @@ public class MuscleMeasurementView extends FloatMeasurementView {
|
|||||||
public static final String KEY = "muscle";
|
public static final String KEY = "muscle";
|
||||||
private static final String[] DEPENDENCY = {WeightMeasurementView.KEY};
|
private static final String[] DEPENDENCY = {WeightMeasurementView.KEY};
|
||||||
|
|
||||||
private boolean percentageEnable;
|
|
||||||
|
|
||||||
public MuscleMeasurementView(Context context) {
|
public MuscleMeasurementView(Context context) {
|
||||||
super(context, context.getResources().getString(R.string.label_muscle), ContextCompat.getDrawable(context, R.drawable.ic_muscle));
|
super(context, context.getResources().getString(R.string.label_muscle), ContextCompat.getDrawable(context, R.drawable.ic_muscle));
|
||||||
}
|
}
|
||||||
@@ -46,14 +43,8 @@ public class MuscleMeasurementView extends FloatMeasurementView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updatePreferences(SharedPreferences preferences) {
|
protected boolean canConvertPercentageToAbsoluteWeight() {
|
||||||
super.updatePreferences(preferences);
|
return true;
|
||||||
percentageEnable = preferences.getBoolean("musclePercentageEnable", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean shouldConvertPercentageToAbsoluteWeight() {
|
|
||||||
return !percentageEnable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -68,11 +59,11 @@ public class MuscleMeasurementView extends FloatMeasurementView {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUnit() {
|
public String getUnit() {
|
||||||
if (percentageEnable) {
|
if (shouldConvertPercentageToAbsoluteWeight()) {
|
||||||
return "%";
|
return getScaleUser().getScaleUnit().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return getScaleUser().getScaleUnit().toString();
|
return "%";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
package com.health.openscale.gui.views;
|
package com.health.openscale.gui.views;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
|
||||||
@@ -29,9 +28,6 @@ public class WaterMeasurementView extends FloatMeasurementView {
|
|||||||
public static final String KEY = "water";
|
public static final String KEY = "water";
|
||||||
private static final String[] DEPENDENCY = {WeightMeasurementView.KEY};
|
private static final String[] DEPENDENCY = {WeightMeasurementView.KEY};
|
||||||
|
|
||||||
private boolean estimateWaterEnable;
|
|
||||||
private boolean percentageEnable;
|
|
||||||
|
|
||||||
public WaterMeasurementView(Context context) {
|
public WaterMeasurementView(Context context) {
|
||||||
super(context, context.getResources().getString(R.string.label_water), ContextCompat.getDrawable(context, R.drawable.ic_water));
|
super(context, context.getResources().getString(R.string.label_water), ContextCompat.getDrawable(context, R.drawable.ic_water));
|
||||||
}
|
}
|
||||||
@@ -47,15 +43,8 @@ public class WaterMeasurementView extends FloatMeasurementView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updatePreferences(SharedPreferences preferences) {
|
protected boolean canConvertPercentageToAbsoluteWeight() {
|
||||||
super.updatePreferences(preferences);
|
return true;
|
||||||
estimateWaterEnable = preferences.getBoolean("estimateWaterEnable", false);
|
|
||||||
percentageEnable = preferences.getBoolean("waterPercentageEnable", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean shouldConvertPercentageToAbsoluteWeight() {
|
|
||||||
return !percentageEnable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -70,11 +59,11 @@ public class WaterMeasurementView extends FloatMeasurementView {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUnit() {
|
public String getUnit() {
|
||||||
if (percentageEnable) {
|
if (shouldConvertPercentageToAbsoluteWeight()) {
|
||||||
return "%";
|
return getScaleUser().getScaleUnit().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return getScaleUser().getScaleUnit().toString();
|
return "%";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -88,9 +77,7 @@ public class WaterMeasurementView extends FloatMeasurementView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isEstimationEnabled() {
|
protected boolean isEstimationSupported() { return true; }
|
||||||
return estimateWaterEnable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected EvaluationResult evaluateSheet(EvaluationSheet evalSheet, float value) {
|
protected EvaluationResult evaluateSheet(EvaluationSheet evalSheet, float value) {
|
||||||
|
Reference in New Issue
Block a user