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

Add method to control if measurment views should update internal views

The table view doesn't actually display the measurement views, it only
uses them for formatting the value that is later displayed. So to
speed up that view, avoid doing calculations that aren't used.
This commit is contained in:
Erik Johansson
2018-02-02 19:26:54 +01:00
parent 7e932b0ec1
commit 147550262b
5 changed files with 95 additions and 52 deletions

View File

@@ -124,6 +124,10 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
measurementsList.add(new BMRMeasurementView(tableView.getContext())); measurementsList.add(new BMRMeasurementView(tableView.getContext()));
measurementsList.add(new CommentMeasurementView(tableView.getContext())); measurementsList.add(new CommentMeasurementView(tableView.getContext()));
for (MeasurementView measurement : measurementsList) {
measurement.setUpdateViews(false);
}
prefs = PreferenceManager.getDefaultSharedPreferences(tableView.getContext()); prefs = PreferenceManager.getDefaultSharedPreferences(tableView.getContext());
if (savedInstanceState == null) { if (savedInstanceState == null) {

View File

@@ -43,7 +43,9 @@ public class DateMeasurementView extends MeasurementView {
private void setValue(Date newDate, boolean callListener) { private void setValue(Date newDate, boolean callListener) {
if (!newDate.equals(date)) { if (!newDate.equals(date)) {
date = newDate; date = newDate;
setValueView(dateFormat.format(date), callListener); if (getUpdateViews()) {
setValueView(dateFormat.format(date), callListener);
}
} }
} }

View File

@@ -116,6 +116,70 @@ 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 void setValueInner(float newValue, String suffix, boolean callListener) {
value = newValue;
evaluationResult = null;
if (!getUpdateViews()) {
return;
}
if (value == AUTO_VALUE) {
setValueView(getContext().getString(R.string.label_automatic), false);
}
else {
setValueView(formatValue(value) + suffix, callListener);
if (getMeasurementMode() != MeasurementViewMode.ADD) {
EvaluationSheet evalSheet = new EvaluationSheet(getScaleUser(), dateTime);
evaluationResult = evaluateSheet(evalSheet, value);
}
}
setEvaluationView(evaluationResult);
}
private void setPreviousValueInner(float newPreviousValue, String suffix) {
previousValue = newPreviousValue;
if (!getUpdateViews()) {
return;
}
if (previousValue >= 0.0f) {
final float diff = value - previousValue;
char symbol;
if (diff > 0.0) {
symbol = SYMBOL_UP;
} else if (diff < 0.0) {
symbol = SYMBOL_DOWN;
} else {
symbol = SYMBOL_NEUTRAL;
}
SpannableStringBuilder text = new SpannableStringBuilder(nameText);
text.append("\n");
int start = text.length();
text.append(symbol);
text.setSpan(new ForegroundColorSpan(Color.GRAY), start, text.length(),
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
start = text.length();
text.append(' ');
text.append(formatValue(diff));
text.append(suffix);
text.setSpan(new RelativeSizeSpan(0.8f), start, text.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setNameView(text);
}
else {
setNameView(nameText);
}
}
private void setValue(float newValue, float newPreviousValue, boolean callListener) { private void setValue(float newValue, float newPreviousValue, boolean callListener) {
final String unit = getUnit(); final String unit = getUnit();
final String suffix = unit.isEmpty() ? "" : " " + unit; final String suffix = unit.isEmpty() ? "" : " " + unit;
@@ -124,57 +188,11 @@ public abstract class FloatMeasurementView extends MeasurementView {
final boolean previousValueChanged = newPreviousValue != previousValue; final boolean previousValueChanged = newPreviousValue != previousValue;
if (valueChanged) { if (valueChanged) {
value = newValue; setValueInner(newValue, suffix, callListener);
evaluationResult = null;
if (value == AUTO_VALUE) {
setValueView(getContext().getString(R.string.label_automatic), false);
}
else {
setValueView(formatValue(value) + suffix, callListener);
if (getMeasurementMode() != MeasurementViewMode.ADD) {
EvaluationSheet evalSheet = new EvaluationSheet(getScaleUser(), dateTime);
evaluationResult = evaluateSheet(evalSheet, value);
}
}
setEvaluationView(evaluationResult);
} }
if (valueChanged || previousValueChanged) { if (valueChanged || previousValueChanged) {
previousValue = newPreviousValue; setPreviousValueInner(newPreviousValue, suffix);
if (previousValue >= 0.0f) {
final float diff = value - previousValue;
char symbol;
if (diff > 0.0) {
symbol = SYMBOL_UP;
} else if (diff < 0.0) {
symbol = SYMBOL_DOWN;
} else {
symbol = SYMBOL_NEUTRAL;
}
SpannableStringBuilder text = new SpannableStringBuilder(nameText);
text.append("\n");
int start = text.length();
text.append(symbol);
text.setSpan(new ForegroundColorSpan(Color.GRAY), start, text.length(),
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
start = text.length();
text.append(' ');
text.append(formatValue(diff));
text.append(suffix);
text.setSpan(new RelativeSizeSpan(0.8f), start, text.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setNameView(text);
} else {
setNameView(nameText);
}
} }
} }

View File

@@ -67,6 +67,8 @@ public abstract class MeasurementView extends TableLayout {
private MeasurementViewUpdateListener updateListener = null; private MeasurementViewUpdateListener updateListener = null;
private MeasurementViewMode measurementMode = VIEW; private MeasurementViewMode measurementMode = VIEW;
private boolean updateViews = true;
public MeasurementView(Context context, String text, Drawable icon) { public MeasurementView(Context context, String text, Drawable icon) {
super(context); super(context);
initView(context); initView(context);
@@ -150,6 +152,13 @@ public abstract class MeasurementView extends TableLayout {
updateListener = listener; updateListener = listener;
} }
public void setUpdateViews(boolean update) {
updateViews = update;
}
protected boolean getUpdateViews() {
return updateViews;
}
public abstract void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement); public abstract void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement);
public abstract void saveTo(ScaleMeasurement measurement); public abstract void saveTo(ScaleMeasurement measurement);
@@ -206,14 +215,18 @@ public abstract class MeasurementView extends TableLayout {
} }
protected void setValueView(String text, boolean callListener) { protected void setValueView(String text, boolean callListener) {
valueView.setText(text); if (updateViews) {
valueView.setText(text);
}
if (callListener && updateListener != null) { if (callListener && updateListener != null) {
updateListener.onMeasurementViewUpdate(this); updateListener.onMeasurementViewUpdate(this);
} }
} }
protected void setNameView(CharSequence text) { protected void setNameView(CharSequence text) {
nameView.setText(text); if (updateViews) {
nameView.setText(text);
}
} }
protected void showEvaluatorRow(boolean show) { protected void showEvaluatorRow(boolean show) {
@@ -246,6 +259,10 @@ public abstract class MeasurementView extends TableLayout {
} }
protected void setEvaluationView(EvaluationResult evalResult) { protected void setEvaluationView(EvaluationResult evalResult) {
if (!updateViews) {
return;
}
if (evalResult == null) { if (evalResult == null) {
evaluatorView.setLimits(-1.0f, -1.0f); evaluatorView.setLimits(-1.0f, -1.0f);
indicatorView.setBackgroundColor(Color.GRAY); indicatorView.setBackgroundColor(Color.GRAY);

View File

@@ -44,7 +44,9 @@ public class TimeMeasurementView extends MeasurementView {
private void setValue(Date newTime, boolean callListener) { private void setValue(Date newTime, boolean callListener) {
if (!newTime.equals(time)) { if (!newTime.equals(time)) {
time = newTime; time = newTime;
setValueView(timeFormat.format(time), callListener); if (getUpdateViews()) {
setValueView(timeFormat.format(time), callListener);
}
} }
} }