From 13de85a3d3687f1deb42a9a2c2cc2fa49f8607ce Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 4 Sep 2018 13:38:47 +0000 Subject: [PATCH] Show swipe action icons Fixes #59 --- .idea/caches/build_file_checksums.ser | Bin 535 -> 535 bytes .../eu/faircode/email/FragmentMessages.java | 136 +++++++++++------- .../main/res/drawable/baseline_inbox_24.xml | 10 ++ .../drawable/baseline_move_to_inbox_24.xml | 10 ++ 4 files changed, 107 insertions(+), 49 deletions(-) create mode 100644 app/src/main/res/drawable/baseline_inbox_24.xml create mode 100644 app/src/main/res/drawable/baseline_move_to_inbox_24.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 983ae46dd6ca7643806a957379bc8cc9fe7dfed1..516668b371b07dba39dcfe4c3d8ec15866e9060b 100644 GIT binary patch delta 32 ocmbQvGM#0@Oy+%U(>Bh@VifH4Se1VEu-ZMZM*C@sWh*Ny0Obn~Q2+n{ delta 32 ocmbQvGM#0@OlE7I0~_aLF$%IhQ`I_Hmr;?E61HjA>bCp}0LDiRHvj+t diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 2c9d8b48..f406e1a0 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -22,6 +22,8 @@ package eu.faircode.email; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.TextUtils; @@ -160,64 +162,100 @@ public class FragmentMessages extends FragmentEx { } @Override - public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { + public void onChildDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { int pos = viewHolder.getAdapterPosition(); - if (pos != RecyclerView.NO_POSITION) { - TupleMessageEx message = ((AdapterMessage) rvMessage.getAdapter()).getCurrentList().get(pos); - Log.i(Helper.TAG, "Swiped dir=" + direction + " message=" + message.id); - - Bundle args = new Bundle(); - args.putLong("id", message.id); - args.putInt("direction", direction); - new SimpleTask() { - @Override - protected String onLoad(Context context, Bundle args) { - long id = args.getLong("id"); - int direction = args.getInt("direction"); - EntityFolder target = null; - - DB db = DB.getInstance(context); - try { - db.beginTransaction(); - EntityMessage message = db.message().getMessage(id); - EntityFolder folder = db.folder().getFolder(message.folder); - - if (EntityFolder.ARCHIVE.equals(folder.type) || EntityFolder.TRASH.equals(folder.type)) - target = db.folder().getFolderByType(message.account, EntityFolder.INBOX); - else { - if (direction == ItemTouchHelper.RIGHT) - target = db.folder().getFolderByType(message.account, EntityFolder.ARCHIVE); - if (direction == ItemTouchHelper.LEFT || target == null) - target = db.folder().getFolderByType(message.account, EntityFolder.TRASH); - } + if (pos == RecyclerView.NO_POSITION) + return; + + TupleMessageEx message = ((AdapterMessage) rvMessage.getAdapter()).getCurrentList().get(pos); + boolean inbox = (EntityFolder.ARCHIVE.equals(message.folderType) || EntityFolder.TRASH.equals(message.folderType)); + + View itemView = viewHolder.itemView; + int margin = Math.round(12 * (getResources().getDisplayMetrics().density)); + + if (dX > margin) { + // Right swipe + Drawable d = getResources().getDrawable(inbox ? R.drawable.baseline_inbox_24 : R.drawable.baseline_archive_24, getContext().getTheme()); + d.setBounds( + itemView.getLeft() + margin, + itemView.getTop() + d.getIntrinsicHeight() / 2, + itemView.getLeft() + margin + d.getIntrinsicWidth(), + itemView.getTop() + (itemView.getHeight() - d.getIntrinsicHeight() / 2)); + d.draw(canvas); + } else if (dX < -margin) { + // Left swipe + Drawable d = getResources().getDrawable(inbox ? R.drawable.baseline_inbox_24 : R.drawable.baseline_delete_24, getContext().getTheme()); + d.setBounds( + itemView.getLeft() + itemView.getWidth() - d.getIntrinsicWidth() - margin, + itemView.getTop() + d.getIntrinsicHeight() / 2, + itemView.getLeft() + itemView.getWidth() - margin, + itemView.getTop() + (itemView.getHeight() - d.getIntrinsicHeight() / 2)); + d.draw(canvas); + } - db.message().setMessageUiHide(message.id, true); - EntityOperation.queue(db, message, EntityOperation.MOVE, target.id); + super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); + } + + @Override + public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { + int pos = viewHolder.getAdapterPosition(); + if (pos == RecyclerView.NO_POSITION) + return; - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); + TupleMessageEx message = ((AdapterMessage) rvMessage.getAdapter()).getCurrentList().get(pos); + Log.i(Helper.TAG, "Swiped dir=" + direction + " message=" + message.id); + + Bundle args = new Bundle(); + args.putLong("id", message.id); + args.putInt("direction", direction); + new SimpleTask() { + @Override + protected String onLoad(Context context, Bundle args) { + long id = args.getLong("id"); + int direction = args.getInt("direction"); + EntityFolder target = null; + + DB db = DB.getInstance(context); + try { + db.beginTransaction(); + EntityMessage message = db.message().getMessage(id); + EntityFolder folder = db.folder().getFolder(message.folder); + + if (EntityFolder.ARCHIVE.equals(folder.type) || EntityFolder.TRASH.equals(folder.type)) + target = db.folder().getFolderByType(message.account, EntityFolder.INBOX); + else { + if (direction == ItemTouchHelper.RIGHT) + target = db.folder().getFolderByType(message.account, EntityFolder.ARCHIVE); + if (direction == ItemTouchHelper.LEFT || target == null) + target = db.folder().getFolderByType(message.account, EntityFolder.TRASH); } - EntityOperation.process(context); + db.message().setMessageUiHide(message.id, true); + EntityOperation.queue(db, message, EntityOperation.MOVE, target.id); - return target.name; + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); } - @Override - protected void onLoaded(Bundle args, String folder) { - Snackbar.make( - view, - getString(R.string.title_moving, Helper.localizeFolderName(getContext(), folder)), - Snackbar.LENGTH_SHORT).show(); - } + EntityOperation.process(context); - @Override - protected void onException(Bundle args, Throwable ex) { - Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); - } - }.load(FragmentMessages.this, args); - } + return target.name; + } + + @Override + protected void onLoaded(Bundle args, String folder) { + Snackbar.make( + view, + getString(R.string.title_moving, Helper.localizeFolderName(getContext(), folder)), + Snackbar.LENGTH_SHORT).show(); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); + } + }.load(FragmentMessages.this, args); } }).attachToRecyclerView(rvMessage); diff --git a/app/src/main/res/drawable/baseline_inbox_24.xml b/app/src/main/res/drawable/baseline_inbox_24.xml new file mode 100644 index 00000000..eddf19ea --- /dev/null +++ b/app/src/main/res/drawable/baseline_inbox_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/baseline_move_to_inbox_24.xml b/app/src/main/res/drawable/baseline_move_to_inbox_24.xml new file mode 100644 index 00000000..21f2d1fe --- /dev/null +++ b/app/src/main/res/drawable/baseline_move_to_inbox_24.xml @@ -0,0 +1,10 @@ + + +