1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-24 01:03:20 +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,9 +43,11 @@ 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;
if (getUpdateViews()) {
setValueView(dateFormat.format(date), callListener); setValueView(dateFormat.format(date), callListener);
} }
} }
}
@Override @Override
public void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement) { public void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement) {

View File

@@ -116,17 +116,14 @@ 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 setValue(float newValue, float newPreviousValue, boolean callListener) { private void setValueInner(float newValue, String suffix, boolean callListener) {
final String unit = getUnit();
final String suffix = unit.isEmpty() ? "" : " " + unit;
final boolean valueChanged = newValue != value;
final boolean previousValueChanged = newPreviousValue != previousValue;
if (valueChanged) {
value = newValue; value = newValue;
evaluationResult = null; evaluationResult = null;
if (!getUpdateViews()) {
return;
}
if (value == AUTO_VALUE) { if (value == AUTO_VALUE) {
setValueView(getContext().getString(R.string.label_automatic), false); setValueView(getContext().getString(R.string.label_automatic), false);
} }
@@ -141,8 +138,13 @@ public abstract class FloatMeasurementView extends MeasurementView {
setEvaluationView(evaluationResult); setEvaluationView(evaluationResult);
} }
if (valueChanged || previousValueChanged) { private void setPreviousValueInner(float newPreviousValue, String suffix) {
previousValue = newPreviousValue; previousValue = newPreviousValue;
if (!getUpdateViews()) {
return;
}
if (previousValue >= 0.0f) { if (previousValue >= 0.0f) {
final float diff = value - previousValue; final float diff = value - previousValue;
@@ -172,10 +174,26 @@ public abstract class FloatMeasurementView extends MeasurementView {
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setNameView(text); setNameView(text);
} else { }
else {
setNameView(nameText); setNameView(nameText);
} }
} }
private void setValue(float newValue, float newPreviousValue, boolean callListener) {
final String unit = getUnit();
final String suffix = unit.isEmpty() ? "" : " " + unit;
final boolean valueChanged = newValue != value;
final boolean previousValueChanged = newPreviousValue != previousValue;
if (valueChanged) {
setValueInner(newValue, suffix, callListener);
}
if (valueChanged || previousValueChanged) {
setPreviousValueInner(newPreviousValue, suffix);
}
} }
private void incValue() { private void incValue() {

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,15 +215,19 @@ public abstract class MeasurementView extends TableLayout {
} }
protected void setValueView(String text, boolean callListener) { protected void setValueView(String text, boolean callListener) {
if (updateViews) {
valueView.setText(text); 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) {
if (updateViews) {
nameView.setText(text); nameView.setText(text);
} }
}
protected void showEvaluatorRow(boolean show) { protected void showEvaluatorRow(boolean show) {
if (show) { if (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,9 +44,11 @@ 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;
if (getUpdateViews()) {
setValueView(timeFormat.format(time), callListener); setValueView(timeFormat.format(time), callListener);
} }
} }
}
@Override @Override
public void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement) { public void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement) {