From 3c324e3de9d080242a03625fc1362df3b5245ff3 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 6 Jan 2020 13:26:53 +0100 Subject: [PATCH] Scroll instead of fling --- .../eu/faircode/email/FragmentMessages.java | 29 ++++++++++- .../java/eu/faircode/email/SwipeListener.java | 50 ++++++++++++++----- .../baseline_keyboard_arrow_left_24.xml | 10 ++++ .../baseline_keyboard_arrow_right_24.xml | 10 ++++ app/src/main/res/layout/fragment_messages.xml | 24 +++++++++ 5 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 app/src/main/res/drawable/baseline_keyboard_arrow_left_24.xml create mode 100644 app/src/main/res/drawable/baseline_keyboard_arrow_right_24.xml diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 788bde777c..15e24b03df 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -76,6 +76,7 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageButton; +import android.widget.ImageView; import android.widget.SeekBar; import android.widget.Spinner; import android.widget.TextView; @@ -192,6 +193,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. private FixedRecyclerView rvMessage; private View vwAnchor; private SeekBar sbThread; + private ImageView ivLeft; + private ImageView ivRight; private ImageButton ibDown; private ImageButton ibUp; private ImageButton ibSeen; @@ -381,6 +384,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. rvMessage = view.findViewById(R.id.rvMessage); vwAnchor = view.findViewById(R.id.vwAnchor); sbThread = view.findViewById(R.id.sbThread); + ivLeft = view.findViewById(R.id.ivLeft); + ivRight = view.findViewById(R.id.ivRight); ibDown = view.findViewById(R.id.ibDown); ibUp = view.findViewById(R.id.ibUp); ibSeen = view.findViewById(R.id.ibSeen); @@ -982,6 +987,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. tvNoEmail.setVisibility(View.GONE); tvNoEmailHint.setVisibility(View.GONE); sbThread.setVisibility(View.GONE); + ivLeft.setVisibility(View.GONE); + ivRight.setVisibility(View.GONE); ibDown.setVisibility(View.GONE); ibUp.setVisibility(View.GONE); ibSeen.setVisibility(View.GONE); @@ -1052,7 +1059,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. final SwipeListener swipeListener = new SwipeListener(getContext(), new SwipeListener.ISwipeListener() { @Override - public boolean onSwipeRight() { + public boolean onSwipedRight() { if (prev == null) { Animation bounce = AnimationUtils.loadAnimation(getContext(), R.anim.bounce_right); view.startAnimation(bounce); @@ -1063,7 +1070,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. } @Override - public boolean onSwipeLeft() { + public boolean onSwipedLeft() { if (next == null) { Animation bounce = AnimationUtils.loadAnimation(getContext(), R.anim.bounce_left); view.startAnimation(bounce); @@ -1072,6 +1079,24 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. return (next != null); } + + @Override + public void onSwipingLeft() { + ivLeft.setVisibility(View.VISIBLE); + ivRight.setVisibility(View.GONE); + } + + @Override + public void onSwipingRight() { + ivLeft.setVisibility(View.GONE); + ivRight.setVisibility(View.VISIBLE); + } + + @Override + public void onCancel() { + ivLeft.setVisibility(View.GONE); + ivRight.setVisibility(View.GONE); + } }); rvMessage.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { diff --git a/app/src/main/java/eu/faircode/email/SwipeListener.java b/app/src/main/java/eu/faircode/email/SwipeListener.java index e19d6373e1..dafc07e654 100644 --- a/app/src/main/java/eu/faircode/email/SwipeListener.java +++ b/app/src/main/java/eu/faircode/email/SwipeListener.java @@ -31,12 +31,12 @@ public class SwipeListener implements View.OnTouchListener { SwipeListener(final Context context, final ISwipeListener listener) { final int width = context.getResources().getDisplayMetrics().widthPixels; - final int MOVE_THRESHOLD = width / 3; - final int SPEED_THRESHOLD = width / 2; + final int MOVE_THRESHOLD = width / 10; + final int SWIPE_THRESHOLD = width / 3; gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override - public boolean onFling(MotionEvent me1, MotionEvent me2, float vx, float vy) { + public boolean onScroll(MotionEvent me1, MotionEvent me2, float distanceX, float distanceY) { if (me1 == null || me2 == null) return false; if (me1.getPointerCount() > 1 || me2.getPointerCount() > 1) @@ -45,21 +45,39 @@ public class SwipeListener implements View.OnTouchListener { boolean consumed = false; int dx = Math.round(me2.getX() - me1.getX()); int dy = Math.round(me2.getY() - me1.getY()); + if (Math.abs(dx) > Math.abs(dy)) { - Log.i("Swipe dx=" + dx + "/" + MOVE_THRESHOLD + " vx=" + vx + "/" + SPEED_THRESHOLD); - if (Math.abs(dx) > MOVE_THRESHOLD && Math.abs(vx) > SPEED_THRESHOLD) - try { + Log.i("Swipe dx=" + dx + "/" + SWIPE_THRESHOLD); + try { + if (Math.abs(dx) > SWIPE_THRESHOLD) { if (dx > 0) - consumed = listener.onSwipeRight(); + consumed = listener.onSwipedRight(); else - consumed = listener.onSwipeLeft(); - } catch (Throwable ex) { - Log.e(ex); + consumed = listener.onSwipedLeft(); + } else if (Math.abs(dx) > MOVE_THRESHOLD) { + if (dx > 0) + listener.onSwipingRight(); + else + listener.onSwipingLeft(); } + } catch (Throwable ex) { + Log.e(ex); + } } return consumed; } - }); + }) { + @Override + public boolean onTouchEvent(MotionEvent ev) { + if (ev.getAction() == MotionEvent.ACTION_UP) + try { + listener.onCancel(); + } catch (Throwable ex) { + Log.e(ex); + } + return super.onTouchEvent(ev); + } + }; } public boolean onTouch(@NonNull View view, @NonNull MotionEvent event) { @@ -67,8 +85,14 @@ public class SwipeListener implements View.OnTouchListener { } interface ISwipeListener { - boolean onSwipeRight(); + boolean onSwipedRight(); - boolean onSwipeLeft(); + boolean onSwipedLeft(); + + void onSwipingLeft(); + + void onSwipingRight(); + + void onCancel(); } } \ No newline at end of file diff --git a/app/src/main/res/drawable/baseline_keyboard_arrow_left_24.xml b/app/src/main/res/drawable/baseline_keyboard_arrow_left_24.xml new file mode 100644 index 0000000000..836fcbff41 --- /dev/null +++ b/app/src/main/res/drawable/baseline_keyboard_arrow_left_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/baseline_keyboard_arrow_right_24.xml b/app/src/main/res/drawable/baseline_keyboard_arrow_right_24.xml new file mode 100644 index 0000000000..07ea9e8169 --- /dev/null +++ b/app/src/main/res/drawable/baseline_keyboard_arrow_right_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_messages.xml b/app/src/main/res/layout/fragment_messages.xml index 71b9b1e8e1..79b5c64227 100644 --- a/app/src/main/res/layout/fragment_messages.xml +++ b/app/src/main/res/layout/fragment_messages.xml @@ -200,6 +200,30 @@ app:layout_constraintEnd_toEndOf="parent" app:srcCompat="@drawable/baseline_reply_24" /> + + + +