From fcff5243b914a14dc61aef432c4fef767ccd10dc Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Tue, 20 Feb 2018 23:02:14 +0100 Subject: [PATCH 1/7] Add MeasurementView::getKey method Returns a stable, unique key for a measurement view. --- .../openscale/gui/views/BMIMeasurementView.java | 5 +++++ .../openscale/gui/views/BMRMeasurementView.java | 5 +++++ .../openscale/gui/views/BoneMeasurementView.java | 5 +++++ .../openscale/gui/views/CommentMeasurementView.java | 10 +++++++--- .../openscale/gui/views/DateMeasurementView.java | 11 ++++++++--- .../openscale/gui/views/FatMeasurementView.java | 5 +++++ .../openscale/gui/views/FloatMeasurementView.java | 4 ++-- .../openscale/gui/views/HipMeasurementView.java | 5 +++++ .../openscale/gui/views/LBWMeasurementView.java | 5 +++++ .../health/openscale/gui/views/MeasurementView.java | 2 ++ .../openscale/gui/views/MuscleMeasurementView.java | 5 +++++ .../openscale/gui/views/TimeMeasurementView.java | 10 +++++++--- .../openscale/gui/views/WHRMeasurementView.java | 5 +++++ .../openscale/gui/views/WHtRMeasurementView.java | 5 +++++ .../openscale/gui/views/WaistMeasurementView.java | 5 +++++ .../openscale/gui/views/WaterMeasurementView.java | 5 +++++ .../openscale/gui/views/WeightMeasurementView.java | 5 +++++ 17 files changed, 86 insertions(+), 11 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/BMIMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/BMIMeasurementView.java index d6b0e264..b37de99c 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/BMIMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/BMIMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "bmi"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("weightEnable", true)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/BMRMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/BMRMeasurementView.java index 9ca1ee3f..8845c832 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/BMRMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/BMRMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "bmr"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("weightEnable", true)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/BoneMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/BoneMeasurementView.java index ea3383a9..24d52744 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/BoneMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/BoneMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "bone"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("boneEnable", false)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/CommentMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/CommentMeasurementView.java index 57db1226..0c8cc47b 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/CommentMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/CommentMeasurementView.java @@ -27,12 +27,16 @@ import com.health.openscale.core.datatypes.ScaleMeasurement; public class CommentMeasurementView extends MeasurementView { private String comment; - private static String COMMENT_KEY = "comment"; public CommentMeasurementView(Context context) { 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) { if (!newComment.equals(comment)) { comment = newComment; @@ -52,12 +56,12 @@ public class CommentMeasurementView extends MeasurementView { @Override public void restoreState(Bundle state) { - setValue(state.getString(COMMENT_KEY), true); + setValue(state.getString(getKey()), true); } @Override public void saveState(Bundle state) { - state.putString(COMMENT_KEY, comment); + state.putString(getKey(), comment); } @Override diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/DateMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/DateMeasurementView.java index b5e3dbb1..977e9511 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/DateMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/DateMeasurementView.java @@ -34,12 +34,17 @@ import java.util.Date; public class DateMeasurementView extends MeasurementView { private static DateFormat dateFormat = DateFormat.getDateInstance(); private Date date; - private static String DATE_KEY = "date"; public DateMeasurementView(Context context) { 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) { if (!newDate.equals(date)) { date = newDate; @@ -70,12 +75,12 @@ public class DateMeasurementView extends MeasurementView { @Override public void restoreState(Bundle state) { - setValue(new Date(state.getLong(DATE_KEY)), true); + setValue(new Date(state.getLong(getKey())), true); } @Override public void saveState(Bundle state) { - state.putLong(DATE_KEY, date.getTime()); + state.putLong(getKey(), date.getTime()); } @Override diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/FatMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/FatMeasurementView.java index 696225c0..a7eca547 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/FatMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/FatMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "fat"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("fatEnable", true)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/FloatMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/FloatMeasurementView.java index b5b324db..8332aef6 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/FloatMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/FloatMeasurementView.java @@ -251,12 +251,12 @@ public abstract class FloatMeasurementView extends MeasurementView { @Override public void restoreState(Bundle state) { - setValue(state.getFloat(nameText), previousValue, true); + setValue(state.getFloat(getKey()), previousValue, true); } @Override public void saveState(Bundle state) { - state.putFloat(nameText, value); + state.putFloat(getKey(), value); } @Override diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/HipMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/HipMeasurementView.java index 87547b04..b2bf2263 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/HipMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/HipMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "hip"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("hipEnable", false)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/LBWMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/LBWMeasurementView.java index 8507cddd..25eeda0e 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/LBWMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/LBWMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "lbw"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("lbwEnable", false)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java index 66a038d1..39b6b600 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java @@ -200,6 +200,8 @@ public abstract class MeasurementView extends TableLayout { return updateViews; } + public abstract String getKey(); + public abstract void loadFrom(ScaleMeasurement measurement, ScaleMeasurement previousMeasurement); public abstract void saveTo(ScaleMeasurement measurement); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/MuscleMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/MuscleMeasurementView.java index bf0a0edd..286b60de 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/MuscleMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/MuscleMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "muscle"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("muscleEnable", true)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/TimeMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/TimeMeasurementView.java index 649bc45c..6bc635e9 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/TimeMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/TimeMeasurementView.java @@ -34,13 +34,17 @@ import java.util.Date; public class TimeMeasurementView extends MeasurementView { private DateFormat timeFormat; private Date time; - private static String TIME_KEY = "time"; public TimeMeasurementView(Context context) { super(context, context.getResources().getString(R.string.label_time), ContextCompat.getDrawable(context, R.drawable.ic_daysleft)); timeFormat = android.text.format.DateFormat.getTimeFormat(context); } + @Override + public String getKey() { + return "time"; + } + private void setValue(Date newTime, boolean callListener) { if (!newTime.equals(time)) { time = newTime; @@ -73,12 +77,12 @@ public class TimeMeasurementView extends MeasurementView { @Override public void restoreState(Bundle state) { - setValue(new Date(state.getLong(TIME_KEY)), true); + setValue(new Date(state.getLong(getKey())), true); } @Override public void saveState(Bundle state) { - state.putLong(TIME_KEY, time.getTime()); + state.putLong(getKey(), time.getTime()); } @Override diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/WHRMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/WHRMeasurementView.java index 7e01aee9..5cdcb904 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/WHRMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/WHRMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "whr"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("hipEnable", false) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/WHtRMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/WHtRMeasurementView.java index 8eed0e42..b60c05ac 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/WHtRMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/WHtRMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "whtr"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("waistEnable", false)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/WaistMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/WaistMeasurementView.java index c30bfb04..9a776fb0 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/WaistMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/WaistMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "waist"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("waistEnable", false)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/WaterMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/WaterMeasurementView.java index ca967067..57f0d5a9 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/WaterMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/WaterMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "water"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("waterEnable", true)); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/WeightMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/WeightMeasurementView.java index 0f710e2a..b70febcb 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/WeightMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/WeightMeasurementView.java @@ -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)); } + @Override + public String getKey() { + return "weight"; + } + @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("weightEnable", true)); From b2eb11380f92063880479b11077027e5529a9d2c Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Tue, 20 Feb 2018 23:35:35 +0100 Subject: [PATCH 2/7] Make it possible to change order of measurment views By long pressing a view in the data entry activity, the view can be dragged up or down to change the order. The order is then saved and reflected in overview, table and data entry fragments/activity. This fixes part of #81 (and #164). --- .../health/openscale/gui/MainActivity.java | 29 +++-- .../gui/activities/DataEntryActivity.java | 104 ++++++++++++++++++ .../openscale/gui/views/MeasurementView.java | 95 +++++++++++----- 3 files changed, 184 insertions(+), 44 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java index b3579b30..197c5c0e 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java @@ -59,6 +59,7 @@ import com.health.openscale.gui.fragments.OverviewFragment; import com.health.openscale.gui.fragments.StatisticsFragment; import com.health.openscale.gui.fragments.TableFragment; import com.health.openscale.gui.utils.PermissionHelper; +import com.health.openscale.gui.views.MeasurementView; import java.lang.reflect.Field; @@ -78,6 +79,8 @@ public class MainActivity extends AppCompatActivity private BottomNavigationView navBottomDrawer; private ActionBarDrawerToggle drawerToggle; + private boolean settingsActivityRunning = false; + @Override protected void onCreate(Bundle savedInstanceState) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); @@ -90,6 +93,9 @@ public class MainActivity extends AppCompatActivity super.onCreate(savedInstanceState); + PreferenceManager.getDefaultSharedPreferences(this) + .registerOnSharedPreferenceChangeListener(this); + CaocConfig.Builder.create() .trackActivities(true) .apply(); @@ -183,33 +189,24 @@ public class MainActivity extends AppCompatActivity } } - private void registerOnSharedPreferenceChangeListener() { - PreferenceManager.getDefaultSharedPreferences(this) - .registerOnSharedPreferenceChangeListener(this); - } - - private void unregisterOnSharedPreferenceChangeListener() { - PreferenceManager.getDefaultSharedPreferences(this) - .unregisterOnSharedPreferenceChangeListener(this); - } - @Override public void onResume() { super.onResume(); - // Stop listening when returning from settings - unregisterOnSharedPreferenceChangeListener(); + settingsActivityRunning = false; } @Override public void onDestroy() { - // Clean up when shutting down - unregisterOnSharedPreferenceChangeListener(); + PreferenceManager.getDefaultSharedPreferences(this) + .unregisterOnSharedPreferenceChangeListener(this); super.onDestroy(); } @Override public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { - recreate(); + if (settingsActivityRunning || key == MeasurementView.PREF_MEASUREMENT_ORDER) { + recreate(); + } } private void positiveFeedbackDialog() { @@ -307,10 +304,10 @@ public class MainActivity extends AppCompatActivity prefs.edit().putInt("lastFragmentId", menuItemId).commit(); break; case R.id.nav_settings: - registerOnSharedPreferenceChangeListener(); Intent settingsIntent = new Intent(this, SettingsActivity.class); settingsIntent.putExtra(SettingsActivity.EXTRA_TINT_COLOR, navDrawer.getItemTextColor().getDefaultColor()); startActivity(settingsIntent); + settingsActivityRunning = true; drawerLayout.closeDrawers(); return; case R.id.nav_help: diff --git a/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java index 521f37b2..8ceb9d62 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java @@ -20,15 +20,18 @@ import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; import android.graphics.Color; +import android.graphics.Point; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.view.DragEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; @@ -127,6 +130,16 @@ public class DataEntryActivity extends AppCompatActivity { : MeasurementView.MeasurementViewMode.VIEW; for (MeasurementView measurement : dataEntryMeasurements) { measurement.setEditMode(mode); + + // Date and time can not be reordered (as they can be both first and last) + if (measurement instanceof DateMeasurementView || measurement instanceof TimeMeasurementView) { + continue; + } + + onLongClickListener longClickListener = new onLongClickListener(); + measurement.setOnTouchListener(longClickListener); + measurement.setOnLongClickListener(longClickListener); + measurement.setOnDragListener(new onDragListener()); } updateOnView(); @@ -456,4 +469,95 @@ public class DataEntryActivity extends AppCompatActivity { } } } + + private class onLongClickListener implements View.OnTouchListener, View.OnLongClickListener { + float x = 0; + float y = 0; + + @Override + public boolean onTouch(View view, MotionEvent event) { + if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { + // Save x and y so that the drag shadow can have the touch point set to where + // the user did the touch (and not in the center of the view). + x = event.getX(); + y = event.getY(); + } + return false; + } + + @Override + public boolean onLongClick(View view) { + return view.startDrag(null, new dragShadowBuilder(view), view, 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(Math.round(x), Math.round(y)); + } + } + } + + private class onDragListener implements View.OnDragListener { + Drawable background = null; + // background may be set to null, thus the extra boolean + boolean hasBackground = false; + + @Override + public boolean onDrag(View view, DragEvent event) { + switch (event.getAction()) { + case DragEvent.ACTION_DRAG_STARTED: + if (view == event.getLocalState() && !hasBackground) { + background = view.getBackground(); + hasBackground = true; + view.setBackgroundColor(Color.GRAY); + } + break; + case DragEvent.ACTION_DRAG_LOCATION: + // Ignore + break; + case DragEvent.ACTION_DRAG_ENTERED: + if (view != event.getLocalState()) { + background = view.getBackground(); + hasBackground = true; + view.setBackgroundColor(Color.LTGRAY); + } + break; + case DragEvent.ACTION_DRAG_EXITED: + if (view != event.getLocalState() && hasBackground) { + view.setBackground(background); + background = null; + hasBackground = false; + } + break; + case DragEvent.ACTION_DROP: + View draggedView = (View) event.getLocalState(); + TableLayout table = (TableLayout) draggedView.getParent(); + final int draggedIndex = table.indexOfChild(draggedView); + final int targetIndex = table.indexOfChild(view); + if (draggedIndex != targetIndex) { + // A view that is moved down is placed after the target view, + // and a view that is moved up is placed before the target view. + table.removeView(draggedView); + table.addView(draggedView, targetIndex); + MeasurementView.saveMeasurementViewsOrder(table); + } + break; + case DragEvent.ACTION_DRAG_ENDED: + if (hasBackground) { + // Restore background + view.setBackground(background); + background = null; + hasBackground = false; + } + break; + } + return true; + } + } } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java index 39b6b600..e274b234 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java @@ -25,6 +25,7 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.content.ContextCompat; import android.text.SpannableStringBuilder; +import android.text.TextUtils; import android.util.TypedValue; import android.view.Gravity; import android.view.View; @@ -58,6 +59,8 @@ import static com.health.openscale.gui.views.MeasurementView.MeasurementViewMode public abstract class MeasurementView extends TableLayout { public enum MeasurementViewMode {VIEW, EDIT, ADD, STATISTIC} + public static String PREF_MEASUREMENT_ORDER = "measurementOrder"; + private TableRow measurementRow; private ImageView iconView; private TextView nameView; @@ -84,38 +87,76 @@ public abstract class MeasurementView extends TableLayout { public enum DateTimeOrder { FIRST, LAST, NONE } - public static final List getMeasurementList(Context context, DateTimeOrder order) { - final List measurementViews = new ArrayList<>(); - - if (order == DateTimeOrder.FIRST) { - measurementViews.add(new DateMeasurementView(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)); + public static final List getMeasurementList(Context context, DateTimeOrder dateTimeOrder) { + final List sorted = new ArrayList<>(); + if (dateTimeOrder == DateTimeOrder.FIRST) { + sorted.add(new DateMeasurementView(context)); + sorted.add(new TimeMeasurementView(context)); } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - for (MeasurementView measurement : measurementViews) { + { + final List 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); } - return measurementViews; + return sorted; + } + + public static void saveMeasurementViewsOrder(TableLayout tableLayout) { + ArrayList order = new ArrayList<>(); + for (int i = 0; i < tableLayout.getChildCount(); ++i) { + MeasurementView view = (MeasurementView) tableLayout.getChildAt(i); + if (view instanceof DateMeasurementView || view instanceof TimeMeasurementView) { + continue; + } + order.add(view.getKey()); + } + PreferenceManager.getDefaultSharedPreferences(tableLayout.getContext()) + .edit() + .putString(PREF_MEASUREMENT_ORDER, TextUtils.join(",", order)) + .commit(); } private void initView(Context context) { @@ -180,9 +221,7 @@ public abstract class MeasurementView extends TableLayout { evaluatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.99f)); spaceAfterEvaluatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.01f)); - onClickListenerEvaluation onClickListener = new onClickListenerEvaluation(); - measurementRow.setOnClickListener(onClickListener); - evaluatorRow.setOnClickListener(onClickListener); + setOnClickListener(new onClickListenerEvaluation()); } protected LinearLayout getIncDecLayout() { From 660b01774105d8bd836d07abc9379f13a83cea5b Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Wed, 21 Feb 2018 00:18:57 +0100 Subject: [PATCH 3/7] Sort List of views as well and pass that when saving order --- .../openscale/gui/activities/DataEntryActivity.java | 6 ++++-- .../health/openscale/gui/views/MeasurementView.java | 12 +++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java index 8ceb9d62..0bf3b0a3 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java @@ -536,7 +536,7 @@ public class DataEntryActivity extends AppCompatActivity { } break; case DragEvent.ACTION_DROP: - View draggedView = (View) event.getLocalState(); + MeasurementView draggedView = (MeasurementView) event.getLocalState(); TableLayout table = (TableLayout) draggedView.getParent(); final int draggedIndex = table.indexOfChild(draggedView); final int targetIndex = table.indexOfChild(view); @@ -545,7 +545,9 @@ public class DataEntryActivity extends AppCompatActivity { // and a view that is moved up is placed before the target view. table.removeView(draggedView); table.addView(draggedView, targetIndex); - MeasurementView.saveMeasurementViewsOrder(table); + dataEntryMeasurements.remove(draggedView); + dataEntryMeasurements.add(targetIndex, draggedView); + MeasurementView.saveMeasurementViewsOrder(getApplicationContext(), dataEntryMeasurements); } break; case DragEvent.ACTION_DRAG_ENDED: diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java index e274b234..3e297de4 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java @@ -144,17 +144,15 @@ public abstract class MeasurementView extends TableLayout { return sorted; } - public static void saveMeasurementViewsOrder(TableLayout tableLayout) { + public static void saveMeasurementViewsOrder(Context context, List measurementViews) { ArrayList order = new ArrayList<>(); - for (int i = 0; i < tableLayout.getChildCount(); ++i) { - MeasurementView view = (MeasurementView) tableLayout.getChildAt(i); - if (view instanceof DateMeasurementView || view instanceof TimeMeasurementView) { + for (MeasurementView measurement : measurementViews) { + if (measurement instanceof DateMeasurementView || measurement instanceof TimeMeasurementView) { continue; } - order.add(view.getKey()); + order.add(measurement.getKey()); } - PreferenceManager.getDefaultSharedPreferences(tableLayout.getContext()) - .edit() + PreferenceManager.getDefaultSharedPreferences(context).edit() .putString(PREF_MEASUREMENT_ORDER, TextUtils.join(",", order)) .commit(); } From c30c804c2c50191cfb0eac2f561d756b88528d56 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Thu, 22 Feb 2018 15:42:03 +0100 Subject: [PATCH 4/7] Add MeasurementView.getNameText --- .../java/com/health/openscale/gui/views/MeasurementView.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java index 3e297de4..9989d489 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java @@ -247,6 +247,7 @@ public abstract class MeasurementView extends TableLayout { public abstract void updatePreferences(SharedPreferences preferences); + public CharSequence getNameText() { return nameView.getText(); } public abstract String getValueAsString(); public void appendDiffValue(SpannableStringBuilder builder) { } public Drawable getIcon() { return iconView.getDrawable(); } @@ -378,8 +379,8 @@ public abstract class MeasurementView extends TableLayout { protected AlertDialog getInputDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(nameView.getText()); - builder.setIcon(iconView.getDrawable()); + builder.setTitle(getNameText()); + builder.setIcon(getIcon()); final EditText input = new EditText(getContext()); From f0940e7dee3dd707647274f6ed2e3666a6e5943b Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Thu, 22 Feb 2018 15:46:28 +0100 Subject: [PATCH 5/7] Move setting of measurement order to preferences --- .../health/openscale/gui/MainActivity.java | 3 +- .../gui/activities/DataEntryActivity.java | 106 --------- .../preferences/MeasurementPreferences.java | 203 ++++++++++++++++++ .../app/src/main/res/values/strings.xml | 3 + .../main/res/xml/measurement_preferences.xml | 1 + 5 files changed, 208 insertions(+), 108 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java index 197c5c0e..dcd57915 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java @@ -59,7 +59,6 @@ import com.health.openscale.gui.fragments.OverviewFragment; import com.health.openscale.gui.fragments.StatisticsFragment; import com.health.openscale.gui.fragments.TableFragment; import com.health.openscale.gui.utils.PermissionHelper; -import com.health.openscale.gui.views.MeasurementView; import java.lang.reflect.Field; @@ -204,7 +203,7 @@ public class MainActivity extends AppCompatActivity @Override public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { - if (settingsActivityRunning || key == MeasurementView.PREF_MEASUREMENT_ORDER) { + if (settingsActivityRunning) { recreate(); } } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java index 0bf3b0a3..521f37b2 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java @@ -20,18 +20,15 @@ import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; import android.graphics.Color; -import android.graphics.Point; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.view.DragEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; @@ -130,16 +127,6 @@ public class DataEntryActivity extends AppCompatActivity { : MeasurementView.MeasurementViewMode.VIEW; for (MeasurementView measurement : dataEntryMeasurements) { measurement.setEditMode(mode); - - // Date and time can not be reordered (as they can be both first and last) - if (measurement instanceof DateMeasurementView || measurement instanceof TimeMeasurementView) { - continue; - } - - onLongClickListener longClickListener = new onLongClickListener(); - measurement.setOnTouchListener(longClickListener); - measurement.setOnLongClickListener(longClickListener); - measurement.setOnDragListener(new onDragListener()); } updateOnView(); @@ -469,97 +456,4 @@ public class DataEntryActivity extends AppCompatActivity { } } } - - private class onLongClickListener implements View.OnTouchListener, View.OnLongClickListener { - float x = 0; - float y = 0; - - @Override - public boolean onTouch(View view, MotionEvent event) { - if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { - // Save x and y so that the drag shadow can have the touch point set to where - // the user did the touch (and not in the center of the view). - x = event.getX(); - y = event.getY(); - } - return false; - } - - @Override - public boolean onLongClick(View view) { - return view.startDrag(null, new dragShadowBuilder(view), view, 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(Math.round(x), Math.round(y)); - } - } - } - - private class onDragListener implements View.OnDragListener { - Drawable background = null; - // background may be set to null, thus the extra boolean - boolean hasBackground = false; - - @Override - public boolean onDrag(View view, DragEvent event) { - switch (event.getAction()) { - case DragEvent.ACTION_DRAG_STARTED: - if (view == event.getLocalState() && !hasBackground) { - background = view.getBackground(); - hasBackground = true; - view.setBackgroundColor(Color.GRAY); - } - break; - case DragEvent.ACTION_DRAG_LOCATION: - // Ignore - break; - case DragEvent.ACTION_DRAG_ENTERED: - if (view != event.getLocalState()) { - background = view.getBackground(); - hasBackground = true; - view.setBackgroundColor(Color.LTGRAY); - } - break; - case DragEvent.ACTION_DRAG_EXITED: - if (view != event.getLocalState() && hasBackground) { - view.setBackground(background); - background = null; - hasBackground = false; - } - break; - case DragEvent.ACTION_DROP: - MeasurementView draggedView = (MeasurementView) event.getLocalState(); - TableLayout table = (TableLayout) draggedView.getParent(); - final int draggedIndex = table.indexOfChild(draggedView); - final int targetIndex = table.indexOfChild(view); - if (draggedIndex != targetIndex) { - // A view that is moved down is placed after the target view, - // and a view that is moved up is placed before the target view. - table.removeView(draggedView); - table.addView(draggedView, targetIndex); - dataEntryMeasurements.remove(draggedView); - dataEntryMeasurements.add(targetIndex, draggedView); - MeasurementView.saveMeasurementViewsOrder(getApplicationContext(), dataEntryMeasurements); - } - break; - case DragEvent.ACTION_DRAG_ENDED: - if (hasBackground) { - // Restore background - view.setBackground(background); - background = null; - hasBackground = false; - } - break; - } - return true; - } - } } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java index a59e3254..44e65ecd 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java @@ -16,17 +16,27 @@ package com.health.openscale.gui.preferences; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; +import android.graphics.Color; +import android.graphics.Point; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.MultiSelectListPreference; import android.preference.Preference; +import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.preference.PreferenceGroup; +import android.preference.PreferenceManager; +import android.preference.PreferenceScreen; import android.preference.SwitchPreference; +import android.view.DragEvent; +import android.view.MotionEvent; +import android.view.View; import android.widget.Toast; 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.EstimatedLBWMetric; import com.health.openscale.core.bodymetric.EstimatedWaterMetric; +import com.health.openscale.gui.views.MeasurementView; import java.util.ArrayList; import java.util.List; @@ -56,6 +67,9 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared private Preference deleteAll; + private PreferenceScreen measurementOrderScreen; + private PreferenceCategory measurementOrderCategory; + private CheckBoxPreference fatEnable; private SwitchPreference fatPercentageEnable; private CheckBoxPreference waterEnable; @@ -79,6 +93,30 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared deleteAll = (Preference) findPreference(PREFERENCE_KEY_DELETE_ALL); 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); estimateWaterFormula = (ListPreference) findPreference(PREFERENCE_KEY_ESTIMATE_WATER_FORMULA); estimateLBWEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_ESTIMATE_LBW); @@ -100,6 +138,17 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared initSummary(getPreferenceScreen()); } + private void updateMeasurementOrderScreen(Context context, PreferenceCategory category) { + List 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() { ArrayList listEntries = new ArrayList(); ArrayList listEntryValues = new ArrayList(); @@ -167,6 +216,10 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { updatePrefSummary(findPreference(key)); + if (!key.equals(MeasurementView.PREF_MEASUREMENT_ORDER)) { + measurementOrderCategory.removeAll(); + updateMeasurementOrderScreen(getContext(), measurementOrderCategory); + } } private void updatePrefSummary(Preference p) { @@ -266,4 +319,154 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared 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.getNameText()); + } + + 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 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 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; + } + } + + } } diff --git a/android_app/app/src/main/res/values/strings.xml b/android_app/app/src/main/res/values/strings.xml index 9e514f8c..34f44fef 100644 --- a/android_app/app/src/main/res/values/strings.xml +++ b/android_app/app/src/main/res/values/strings.xml @@ -222,5 +222,8 @@ openScale requires permission to access the coarse location to search for Bluetooth devices Information Next + Measurement order + Press and hold to reorder + Set default order diff --git a/android_app/app/src/main/res/xml/measurement_preferences.xml b/android_app/app/src/main/res/xml/measurement_preferences.xml index aa23c650..b3e14152 100644 --- a/android_app/app/src/main/res/xml/measurement_preferences.xml +++ b/android_app/app/src/main/res/xml/measurement_preferences.xml @@ -1,6 +1,7 @@ + From f3c5d2fd759d8923908399c58d8c7e37be7693f8 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Thu, 22 Feb 2018 19:27:36 +0100 Subject: [PATCH 6/7] Merge getNameText() into getName() --- .../openscale/gui/preferences/MeasurementPreferences.java | 2 +- .../com/health/openscale/gui/views/FloatMeasurementView.java | 3 ++- .../java/com/health/openscale/gui/views/MeasurementView.java | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java index 44e65ecd..6b815249 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java @@ -330,7 +330,7 @@ public class MeasurementPreferences extends PreferenceFragment implements Shared parentGroup = parent; measurement = measurementView; setIcon(measurement.getIcon()); - setTitle(measurement.getNameText()); + setTitle(measurement.getName()); } public PreferenceGroup getParent() { diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/FloatMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/FloatMeasurementView.java index 8332aef6..6e0f2724 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/FloatMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/FloatMeasurementView.java @@ -271,7 +271,8 @@ public abstract class FloatMeasurementView extends MeasurementView { return value; } - public String getName() { + @Override + public CharSequence getName() { return nameText; } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java index 9989d489..133064e4 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java @@ -247,7 +247,7 @@ public abstract class MeasurementView extends TableLayout { public abstract void updatePreferences(SharedPreferences preferences); - public CharSequence getNameText() { return nameView.getText(); } + public CharSequence getName() { return nameView.getText(); } public abstract String getValueAsString(); public void appendDiffValue(SpannableStringBuilder builder) { } public Drawable getIcon() { return iconView.getDrawable(); } @@ -379,7 +379,7 @@ public abstract class MeasurementView extends TableLayout { protected AlertDialog getInputDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(getNameText()); + builder.setTitle(getName()); builder.setIcon(getIcon()); final EditText input = new EditText(getContext()); From ebe7e002640541b2e6c745167858f846346953f1 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Thu, 22 Feb 2018 19:31:37 +0100 Subject: [PATCH 7/7] Remove useless instanceof check --- .../java/com/health/openscale/gui/views/MeasurementView.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java index 133064e4..1c78743c 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java @@ -147,9 +147,6 @@ public abstract class MeasurementView extends TableLayout { public static void saveMeasurementViewsOrder(Context context, List measurementViews) { ArrayList order = new ArrayList<>(); for (MeasurementView measurement : measurementViews) { - if (measurement instanceof DateMeasurementView || measurement instanceof TimeMeasurementView) { - continue; - } order.add(measurement.getKey()); } PreferenceManager.getDefaultSharedPreferences(context).edit()