1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-31 12:01:54 +02:00

Merge pull request #198 from erijo/reorder

Make it possible to change order of measurment views
This commit is contained in:
OliE
2018-02-22 20:49:42 +01:00
committed by GitHub
21 changed files with 372 additions and 58 deletions

View File

@@ -78,6 +78,8 @@ public class MainActivity extends AppCompatActivity
private BottomNavigationView navBottomDrawer; private BottomNavigationView navBottomDrawer;
private ActionBarDrawerToggle drawerToggle; private ActionBarDrawerToggle drawerToggle;
private boolean settingsActivityRunning = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@@ -90,6 +92,9 @@ public class MainActivity extends AppCompatActivity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
CaocConfig.Builder.create() CaocConfig.Builder.create()
.trackActivities(true) .trackActivities(true)
.apply(); .apply();
@@ -183,33 +188,24 @@ public class MainActivity extends AppCompatActivity
} }
} }
private void registerOnSharedPreferenceChangeListener() {
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
}
private void unregisterOnSharedPreferenceChangeListener() {
PreferenceManager.getDefaultSharedPreferences(this)
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
// Stop listening when returning from settings settingsActivityRunning = false;
unregisterOnSharedPreferenceChangeListener();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
// Clean up when shutting down PreferenceManager.getDefaultSharedPreferences(this)
unregisterOnSharedPreferenceChangeListener(); .unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy(); super.onDestroy();
} }
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
recreate(); if (settingsActivityRunning) {
recreate();
}
} }
private void positiveFeedbackDialog() { private void positiveFeedbackDialog() {
@@ -307,10 +303,10 @@ public class MainActivity extends AppCompatActivity
prefs.edit().putInt("lastFragmentId", menuItemId).commit(); prefs.edit().putInt("lastFragmentId", menuItemId).commit();
break; break;
case R.id.nav_settings: case R.id.nav_settings:
registerOnSharedPreferenceChangeListener();
Intent settingsIntent = new Intent(this, SettingsActivity.class); Intent settingsIntent = new Intent(this, SettingsActivity.class);
settingsIntent.putExtra(SettingsActivity.EXTRA_TINT_COLOR, navDrawer.getItemTextColor().getDefaultColor()); settingsIntent.putExtra(SettingsActivity.EXTRA_TINT_COLOR, navDrawer.getItemTextColor().getDefaultColor());
startActivity(settingsIntent); startActivity(settingsIntent);
settingsActivityRunning = true;
drawerLayout.closeDrawers(); drawerLayout.closeDrawers();
return; return;
case R.id.nav_help: case R.id.nav_help:

View File

@@ -16,17 +16,27 @@
package com.health.openscale.gui.preferences; package com.health.openscale.gui.preferences;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.MultiSelectListPreference; import android.preference.MultiSelectListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup; import android.preference.PreferenceGroup;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference; import android.preference.SwitchPreference;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast; import android.widget.Toast;
import com.health.openscale.R; import com.health.openscale.R;
@@ -34,6 +44,7 @@ import com.health.openscale.core.OpenScale;
import com.health.openscale.core.bodymetric.EstimatedFatMetric; import com.health.openscale.core.bodymetric.EstimatedFatMetric;
import com.health.openscale.core.bodymetric.EstimatedLBWMetric; import com.health.openscale.core.bodymetric.EstimatedLBWMetric;
import com.health.openscale.core.bodymetric.EstimatedWaterMetric; import com.health.openscale.core.bodymetric.EstimatedWaterMetric;
import com.health.openscale.gui.views.MeasurementView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -56,6 +67,9 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared
private Preference deleteAll; private Preference deleteAll;
private PreferenceScreen measurementOrderScreen;
private PreferenceCategory measurementOrderCategory;
private CheckBoxPreference fatEnable; private CheckBoxPreference fatEnable;
private SwitchPreference fatPercentageEnable; private SwitchPreference fatPercentageEnable;
private CheckBoxPreference waterEnable; private CheckBoxPreference waterEnable;
@@ -79,6 +93,30 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared
deleteAll = (Preference) findPreference(PREFERENCE_KEY_DELETE_ALL); deleteAll = (Preference) findPreference(PREFERENCE_KEY_DELETE_ALL);
deleteAll.setOnPreferenceClickListener(new onClickListenerDeleteAll()); deleteAll.setOnPreferenceClickListener(new onClickListenerDeleteAll());
final Context context = getActivity().getApplicationContext();
measurementOrderScreen = (PreferenceScreen) findPreference(MeasurementView.PREF_MEASUREMENT_ORDER);
measurementOrderCategory = new PreferenceCategory(context);
measurementOrderCategory.setTitle(R.string.label_press_hold_reorder);
measurementOrderCategory.setOrderingAsAdded(true);
Preference resetOrder = new Preference(context);
resetOrder.setTitle(R.string.label_set_default_order);
resetOrder.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
PreferenceManager.getDefaultSharedPreferences(context).edit()
.remove(MeasurementView.PREF_MEASUREMENT_ORDER).commit();
measurementOrderCategory.removeAll();
updateMeasurementOrderScreen(context, measurementOrderCategory);
return true;
}
});
measurementOrderScreen.addPreference(resetOrder);
measurementOrderScreen.addPreference(measurementOrderCategory);
updateMeasurementOrderScreen(context, measurementOrderCategory);
estimateWaterEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_ESTIMATE_WATER); estimateWaterEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_ESTIMATE_WATER);
estimateWaterFormula = (ListPreference) findPreference(PREFERENCE_KEY_ESTIMATE_WATER_FORMULA); estimateWaterFormula = (ListPreference) findPreference(PREFERENCE_KEY_ESTIMATE_WATER_FORMULA);
estimateLBWEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_ESTIMATE_LBW); estimateLBWEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_ESTIMATE_LBW);
@@ -100,6 +138,17 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared
initSummary(getPreferenceScreen()); initSummary(getPreferenceScreen());
} }
private void updateMeasurementOrderScreen(Context context, PreferenceCategory category) {
List<MeasurementView> measurementViews = MeasurementView.getMeasurementList(
context, MeasurementView.DateTimeOrder.NONE);
for (MeasurementView measurement : measurementViews) {
Preference preference = new MeasurementOrderPreference(context, category, measurement);
preference.setShouldDisableView(true);
preference.setEnabled(measurement.isVisible());
category.addPreference(preference);
}
}
public void updateWaterListPreferences() { public void updateWaterListPreferences() {
ArrayList<String> listEntries = new ArrayList(); ArrayList<String> listEntries = new ArrayList();
ArrayList<String> listEntryValues = new ArrayList(); ArrayList<String> listEntryValues = new ArrayList();
@@ -167,6 +216,10 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePrefSummary(findPreference(key)); updatePrefSummary(findPreference(key));
if (!key.equals(MeasurementView.PREF_MEASUREMENT_ORDER)) {
measurementOrderCategory.removeAll();
updateMeasurementOrderScreen(getContext(), measurementOrderCategory);
}
} }
private void updatePrefSummary(Preference p) { private void updatePrefSummary(Preference p) {
@@ -266,4 +319,154 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared
return false; return false;
} }
} }
private class MeasurementOrderPreference extends Preference {
PreferenceGroup parentGroup;
MeasurementView measurement;
View boundView;
MeasurementOrderPreference(Context context, PreferenceGroup parent, MeasurementView measurementView) {
super(context);
parentGroup = parent;
measurement = measurementView;
setIcon(measurement.getIcon());
setTitle(measurement.getName());
}
public PreferenceGroup getParent() {
return parentGroup;
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
boundView = view;
onTouchClickListener touchClickListener = new onTouchClickListener(this);
view.setOnTouchListener(touchClickListener);
view.setOnLongClickListener(touchClickListener);
view.setOnDragListener(new onDragListener());
}
private class onTouchClickListener implements View.OnTouchListener, View.OnLongClickListener {
MeasurementOrderPreference preference;
int x = 0;
int y = 0;
onTouchClickListener(MeasurementOrderPreference pref) {
preference = pref;
}
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
x = Math.round(event.getX());
y = Math.round(event.getY());
}
return false;
}
@Override
public boolean onLongClick(View view) {
return view.startDrag(null, new dragShadowBuilder(view), preference, 0);
}
private class dragShadowBuilder extends View.DragShadowBuilder {
public dragShadowBuilder(View view) {
super(view);
}
@Override
public void onProvideShadowMetrics(Point outShadowSize, Point outShadowTouchPoint) {
super.onProvideShadowMetrics(outShadowSize, outShadowTouchPoint);
outShadowTouchPoint.set(x, y);
}
}
}
private class onDragListener implements View.OnDragListener {
Drawable background = null;
// background may be set to null, thus the extra boolean
boolean hasBackground = false;
private MeasurementOrderPreference castLocalState(DragEvent event) {
return (MeasurementOrderPreference) event.getLocalState();
}
private boolean isDraggedView(View view, DragEvent event) {
return castLocalState(event).boundView == view;
}
private void setTemporaryBackgroundColor(View view, int color) {
if (!hasBackground) {
background = view.getBackground();
hasBackground = true;
view.setBackgroundColor(color);
}
}
private void restoreBackground(View view) {
if (hasBackground) {
view.setBackground(background);
background = null;
hasBackground = false;
}
}
@Override
public boolean onDrag(View view, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
if (isDraggedView(view, event)) {
setTemporaryBackgroundColor(view, Color.GRAY);
}
break;
case DragEvent.ACTION_DRAG_ENTERED:
if (!isDraggedView(view, event)) {
setTemporaryBackgroundColor(view, Color.LTGRAY);
}
break;
case DragEvent.ACTION_DRAG_EXITED:
if (!isDraggedView(view, event)) {
restoreBackground(view);
}
break;
case DragEvent.ACTION_DROP:
MeasurementOrderPreference draggedPref = castLocalState(event);
PreferenceGroup group = draggedPref.getParent();
ArrayList<MeasurementOrderPreference> preferences = new ArrayList<>();
for (int i = 0; i < group.getPreferenceCount(); ++i) {
MeasurementOrderPreference pref = (MeasurementOrderPreference) group.getPreference(i);
// Add all preferences except the dragged one
if (pref != draggedPref) {
preferences.add(pref);
}
// When we find the view that is the drop target use add(index, ...).
// This will add the dragged preference before the drop if dragged upwards,
// and after if dragged downwards.
if (pref.boundView == view) {
preferences.add(i, draggedPref);
}
}
ArrayList<MeasurementView> measurementViews = new ArrayList<>();
// Re-add all preferences in the new order
group.removeAll();
for (MeasurementOrderPreference p : preferences) {
p.setOrder(DEFAULT_ORDER);
group.addPreference(p);
measurementViews.add(p.measurement);
}
MeasurementView.saveMeasurementViewsOrder(getContext(), measurementViews);
break;
case DragEvent.ACTION_DRAG_ENDED:
restoreBackground(view);
break;
}
return true;
}
}
}
} }

View File

@@ -31,6 +31,11 @@ public class BMIMeasurementView extends FloatMeasurementView {
super(context, context.getResources().getString(R.string.label_bmi), ContextCompat.getDrawable(context, R.drawable.ic_bmi)); super(context, context.getResources().getString(R.string.label_bmi), ContextCompat.getDrawable(context, R.drawable.ic_bmi));
} }
@Override
public String getKey() {
return "bmi";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("weightEnable", true)); setVisible(preferences.getBoolean("weightEnable", true));

View File

@@ -33,6 +33,11 @@ public class BMRMeasurementView extends FloatMeasurementView {
super(context, context.getResources().getString(R.string.label_bmr), ContextCompat.getDrawable(context, R.drawable.ic_bmr)); super(context, context.getResources().getString(R.string.label_bmr), ContextCompat.getDrawable(context, R.drawable.ic_bmr));
} }
@Override
public String getKey() {
return "bmr";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("weightEnable", true)); setVisible(preferences.getBoolean("weightEnable", true));

View File

@@ -31,6 +31,11 @@ public class BoneMeasurementView extends FloatMeasurementView {
super(context, context.getResources().getString(R.string.label_bone), ContextCompat.getDrawable(context, R.drawable.ic_bone)); super(context, context.getResources().getString(R.string.label_bone), ContextCompat.getDrawable(context, R.drawable.ic_bone));
} }
@Override
public String getKey() {
return "bone";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("boneEnable", false)); setVisible(preferences.getBoolean("boneEnable", false));

View File

@@ -27,12 +27,16 @@ import com.health.openscale.core.datatypes.ScaleMeasurement;
public class CommentMeasurementView extends MeasurementView { public class CommentMeasurementView extends MeasurementView {
private String comment; private String comment;
private static String COMMENT_KEY = "comment";
public CommentMeasurementView(Context context) { public CommentMeasurementView(Context context) {
super(context, context.getResources().getString(R.string.label_comment), ContextCompat.getDrawable(context, R.drawable.ic_comment)); super(context, context.getResources().getString(R.string.label_comment), ContextCompat.getDrawable(context, R.drawable.ic_comment));
} }
@Override
public String getKey() {
return "comment";
}
private void setValue(String newComment, boolean callListener) { private void setValue(String newComment, boolean callListener) {
if (!newComment.equals(comment)) { if (!newComment.equals(comment)) {
comment = newComment; comment = newComment;
@@ -52,12 +56,12 @@ public class CommentMeasurementView extends MeasurementView {
@Override @Override
public void restoreState(Bundle state) { public void restoreState(Bundle state) {
setValue(state.getString(COMMENT_KEY), true); setValue(state.getString(getKey()), true);
} }
@Override @Override
public void saveState(Bundle state) { public void saveState(Bundle state) {
state.putString(COMMENT_KEY, comment); state.putString(getKey(), comment);
} }
@Override @Override

View File

@@ -34,12 +34,17 @@ import java.util.Date;
public class DateMeasurementView extends MeasurementView { public class DateMeasurementView extends MeasurementView {
private static DateFormat dateFormat = DateFormat.getDateInstance(); private static DateFormat dateFormat = DateFormat.getDateInstance();
private Date date; private Date date;
private static String DATE_KEY = "date";
public DateMeasurementView(Context context) { public DateMeasurementView(Context context) {
super(context, context.getResources().getString(R.string.label_date), ContextCompat.getDrawable(context, R.drawable.ic_lastmonth)); super(context, context.getResources().getString(R.string.label_date), ContextCompat.getDrawable(context, R.drawable.ic_lastmonth));
} }
@Override
public String getKey() {
return "date";
}
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;
@@ -70,12 +75,12 @@ public class DateMeasurementView extends MeasurementView {
@Override @Override
public void restoreState(Bundle state) { public void restoreState(Bundle state) {
setValue(new Date(state.getLong(DATE_KEY)), true); setValue(new Date(state.getLong(getKey())), true);
} }
@Override @Override
public void saveState(Bundle state) { public void saveState(Bundle state) {
state.putLong(DATE_KEY, date.getTime()); state.putLong(getKey(), date.getTime());
} }
@Override @Override

View File

@@ -34,6 +34,11 @@ public class FatMeasurementView extends FloatMeasurementView {
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));
} }
@Override
public String getKey() {
return "fat";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("fatEnable", true)); setVisible(preferences.getBoolean("fatEnable", true));

View File

@@ -251,12 +251,12 @@ public abstract class FloatMeasurementView extends MeasurementView {
@Override @Override
public void restoreState(Bundle state) { public void restoreState(Bundle state) {
setValue(state.getFloat(nameText), previousValue, true); setValue(state.getFloat(getKey()), previousValue, true);
} }
@Override @Override
public void saveState(Bundle state) { public void saveState(Bundle state) {
state.putFloat(nameText, value); state.putFloat(getKey(), value);
} }
@Override @Override
@@ -271,7 +271,8 @@ public abstract class FloatMeasurementView extends MeasurementView {
return value; return value;
} }
public String getName() { @Override
public CharSequence getName() {
return nameText; return nameText;
} }

View File

@@ -31,6 +31,11 @@ public class HipMeasurementView extends FloatMeasurementView {
super(context, context.getResources().getString(R.string.label_hip), ContextCompat.getDrawable(context, R.drawable.ic_hip)); super(context, context.getResources().getString(R.string.label_hip), ContextCompat.getDrawable(context, R.drawable.ic_hip));
} }
@Override
public String getKey() {
return "hip";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("hipEnable", false)); setVisible(preferences.getBoolean("hipEnable", false));

View File

@@ -33,6 +33,11 @@ public class LBWMeasurementView extends FloatMeasurementView {
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));
} }
@Override
public String getKey() {
return "lbw";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("lbwEnable", false)); setVisible(preferences.getBoolean("lbwEnable", false));

View File

@@ -25,6 +25,7 @@ import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
@@ -58,6 +59,8 @@ import static com.health.openscale.gui.views.MeasurementView.MeasurementViewMode
public abstract class MeasurementView extends TableLayout { public abstract class MeasurementView extends TableLayout {
public enum MeasurementViewMode {VIEW, EDIT, ADD, STATISTIC} public enum MeasurementViewMode {VIEW, EDIT, ADD, STATISTIC}
public static String PREF_MEASUREMENT_ORDER = "measurementOrder";
private TableRow measurementRow; private TableRow measurementRow;
private ImageView iconView; private ImageView iconView;
private TextView nameView; private TextView nameView;
@@ -84,38 +87,71 @@ 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 order) { public static final List<MeasurementView> getMeasurementList(Context context, DateTimeOrder dateTimeOrder) {
final List<MeasurementView> measurementViews = new ArrayList<>(); final List<MeasurementView> sorted = new ArrayList<>();
if (dateTimeOrder == DateTimeOrder.FIRST) {
if (order == DateTimeOrder.FIRST) { sorted.add(new DateMeasurementView(context));
measurementViews.add(new DateMeasurementView(context)); sorted.add(new TimeMeasurementView(context));
measurementViews.add(new TimeMeasurementView(context));
}
measurementViews.add(new WeightMeasurementView(context));
measurementViews.add(new BMIMeasurementView(context));
measurementViews.add(new WaterMeasurementView(context));
measurementViews.add(new MuscleMeasurementView(context));
measurementViews.add(new LBWMeasurementView(context));
measurementViews.add(new FatMeasurementView(context));
measurementViews.add(new BoneMeasurementView(context));
measurementViews.add(new WaistMeasurementView(context));
measurementViews.add(new WHtRMeasurementView(context));
measurementViews.add(new HipMeasurementView(context));
measurementViews.add(new WHRMeasurementView(context));
measurementViews.add(new BMRMeasurementView(context));
measurementViews.add(new CommentMeasurementView(context));
if (order == DateTimeOrder.LAST) {
measurementViews.add(new DateMeasurementView(context));
measurementViews.add(new TimeMeasurementView(context));
} }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
for (MeasurementView measurement : measurementViews) { {
final List<MeasurementView> unsorted = new ArrayList<>();
unsorted.add(new WeightMeasurementView(context));
unsorted.add(new BMIMeasurementView(context));
unsorted.add(new WaterMeasurementView(context));
unsorted.add(new MuscleMeasurementView(context));
unsorted.add(new LBWMeasurementView(context));
unsorted.add(new FatMeasurementView(context));
unsorted.add(new BoneMeasurementView(context));
unsorted.add(new WaistMeasurementView(context));
unsorted.add(new WHtRMeasurementView(context));
unsorted.add(new HipMeasurementView(context));
unsorted.add(new WHRMeasurementView(context));
unsorted.add(new BMRMeasurementView(context));
unsorted.add(new CommentMeasurementView(context));
// Get sort order
final String[] sortOrder = TextUtils.split(
prefs.getString(PREF_MEASUREMENT_ORDER, ""), ",");
// Move views from unsorted to sorted in the correct order
for (String key : sortOrder) {
for (MeasurementView measurement : unsorted) {
if (key.equals(measurement.getKey())) {
sorted.add(measurement);
unsorted.remove(measurement);
break;
}
}
}
// Any new views end up at the end
sorted.addAll(unsorted);
}
if (dateTimeOrder == DateTimeOrder.LAST) {
sorted.add(new DateMeasurementView(context));
sorted.add(new TimeMeasurementView(context));
}
for (MeasurementView measurement : sorted) {
measurement.updatePreferences(prefs); measurement.updatePreferences(prefs);
} }
return measurementViews; return sorted;
}
public static void saveMeasurementViewsOrder(Context context, List<MeasurementView> measurementViews) {
ArrayList<String> order = new ArrayList<>();
for (MeasurementView measurement : measurementViews) {
order.add(measurement.getKey());
}
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString(PREF_MEASUREMENT_ORDER, TextUtils.join(",", order))
.commit();
} }
private void initView(Context context) { private void initView(Context context) {
@@ -180,9 +216,7 @@ public abstract class MeasurementView extends TableLayout {
evaluatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.99f)); evaluatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.99f));
spaceAfterEvaluatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.01f)); spaceAfterEvaluatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.01f));
onClickListenerEvaluation onClickListener = new onClickListenerEvaluation(); setOnClickListener(new onClickListenerEvaluation());
measurementRow.setOnClickListener(onClickListener);
evaluatorRow.setOnClickListener(onClickListener);
} }
protected LinearLayout getIncDecLayout() { protected LinearLayout getIncDecLayout() {
@@ -200,6 +234,8 @@ public abstract class MeasurementView extends TableLayout {
return updateViews; return updateViews;
} }
public abstract String getKey();
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);
@@ -208,6 +244,7 @@ public abstract class MeasurementView extends TableLayout {
public abstract void updatePreferences(SharedPreferences preferences); public abstract void updatePreferences(SharedPreferences preferences);
public CharSequence getName() { return nameView.getText(); }
public abstract String getValueAsString(); public abstract String getValueAsString();
public void appendDiffValue(SpannableStringBuilder builder) { } public void appendDiffValue(SpannableStringBuilder builder) { }
public Drawable getIcon() { return iconView.getDrawable(); } public Drawable getIcon() { return iconView.getDrawable(); }
@@ -339,8 +376,8 @@ public abstract class MeasurementView extends TableLayout {
protected AlertDialog getInputDialog() { protected AlertDialog getInputDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(nameView.getText()); builder.setTitle(getName());
builder.setIcon(iconView.getDrawable()); builder.setIcon(getIcon());
final EditText input = new EditText(getContext()); final EditText input = new EditText(getContext());

View File

@@ -33,6 +33,11 @@ public class MuscleMeasurementView extends FloatMeasurementView {
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));
} }
@Override
public String getKey() {
return "muscle";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("muscleEnable", true)); setVisible(preferences.getBoolean("muscleEnable", true));

View File

@@ -34,13 +34,17 @@ import java.util.Date;
public class TimeMeasurementView extends MeasurementView { public class TimeMeasurementView extends MeasurementView {
private DateFormat timeFormat; private DateFormat timeFormat;
private Date time; private Date time;
private static String TIME_KEY = "time";
public TimeMeasurementView(Context context) { public TimeMeasurementView(Context context) {
super(context, context.getResources().getString(R.string.label_time), ContextCompat.getDrawable(context, R.drawable.ic_daysleft)); super(context, context.getResources().getString(R.string.label_time), ContextCompat.getDrawable(context, R.drawable.ic_daysleft));
timeFormat = android.text.format.DateFormat.getTimeFormat(context); timeFormat = android.text.format.DateFormat.getTimeFormat(context);
} }
@Override
public String getKey() {
return "time";
}
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;
@@ -73,12 +77,12 @@ public class TimeMeasurementView extends MeasurementView {
@Override @Override
public void restoreState(Bundle state) { public void restoreState(Bundle state) {
setValue(new Date(state.getLong(TIME_KEY)), true); setValue(new Date(state.getLong(getKey())), true);
} }
@Override @Override
public void saveState(Bundle state) { public void saveState(Bundle state) {
state.putLong(TIME_KEY, time.getTime()); state.putLong(getKey(), time.getTime());
} }
@Override @Override

View File

@@ -31,6 +31,11 @@ public class WHRMeasurementView extends FloatMeasurementView {
super(context, context.getResources().getString(R.string.label_whr), ContextCompat.getDrawable(context, R.drawable.ic_whr)); super(context, context.getResources().getString(R.string.label_whr), ContextCompat.getDrawable(context, R.drawable.ic_whr));
} }
@Override
public String getKey() {
return "whr";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("hipEnable", false) setVisible(preferences.getBoolean("hipEnable", false)

View File

@@ -31,6 +31,11 @@ public class WHtRMeasurementView extends FloatMeasurementView {
super(context, context.getResources().getString(R.string.label_whtr), ContextCompat.getDrawable(context, R.drawable.ic_whtr)); super(context, context.getResources().getString(R.string.label_whtr), ContextCompat.getDrawable(context, R.drawable.ic_whtr));
} }
@Override
public String getKey() {
return "whtr";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("waistEnable", false)); setVisible(preferences.getBoolean("waistEnable", false));

View File

@@ -31,6 +31,11 @@ public class WaistMeasurementView extends FloatMeasurementView {
super(context, context.getResources().getString(R.string.label_waist), ContextCompat.getDrawable(context, R.drawable.ic_waist)); super(context, context.getResources().getString(R.string.label_waist), ContextCompat.getDrawable(context, R.drawable.ic_waist));
} }
@Override
public String getKey() {
return "waist";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("waistEnable", false)); setVisible(preferences.getBoolean("waistEnable", false));

View File

@@ -34,6 +34,11 @@ public class WaterMeasurementView extends FloatMeasurementView {
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));
} }
@Override
public String getKey() {
return "water";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("waterEnable", true)); setVisible(preferences.getBoolean("waterEnable", true));

View File

@@ -32,6 +32,11 @@ public class WeightMeasurementView extends FloatMeasurementView {
super(context, context.getResources().getString(R.string.label_weight), ContextCompat.getDrawable(context, R.drawable.ic_weight)); super(context, context.getResources().getString(R.string.label_weight), ContextCompat.getDrawable(context, R.drawable.ic_weight));
} }
@Override
public String getKey() {
return "weight";
}
@Override @Override
public void updatePreferences(SharedPreferences preferences) { public void updatePreferences(SharedPreferences preferences) {
setVisible(preferences.getBoolean("weightEnable", true)); setVisible(preferences.getBoolean("weightEnable", true));

View File

@@ -222,5 +222,8 @@
<string name="permission_bluetooth_info">openScale requires permission to access the coarse location to search for Bluetooth devices</string> <string name="permission_bluetooth_info">openScale requires permission to access the coarse location to search for Bluetooth devices</string>
<string name="permission_bluetooth_info_title">Information</string> <string name="permission_bluetooth_info_title">Information</string>
<string name="label_next">Next</string> <string name="label_next">Next</string>
<string name="label_measurement_order">Measurement order</string>
<string name="label_press_hold_reorder">Press and hold to reorder</string>
<string name="label_set_default_order">Set default order</string>
</resources> </resources>

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/label_category_display"> <PreferenceCategory android:title="@string/label_category_display">
<PreferenceScreen android:title="@string/label_measurement_order" android:key="measurementOrder" />
<CheckBoxPreference android:title="@string/label_fat" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="fatEnable" android:defaultValue="true"/> <CheckBoxPreference android:title="@string/label_fat" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="fatEnable" android:defaultValue="true"/>
<SwitchPreference android:title="@string/label_fat_percentage" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key ="fatPercentageEnable" android:defaultValue="true"/> <SwitchPreference android:title="@string/label_fat_percentage" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key ="fatPercentageEnable" android:defaultValue="true"/>
<CheckBoxPreference android:title="@string/label_water" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="waterEnable" android:defaultValue="true"/> <CheckBoxPreference android:title="@string/label_water" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="waterEnable" android:defaultValue="true"/>