From 61441c3c8af1457d3735c081ab1a260f9e5eaa67 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 24 Oct 2018 09:09:07 +0000 Subject: [PATCH] Added multiple selection --- .../eu/faircode/email/AdapterMessage.java | 21 +++++++++-- .../eu/faircode/email/FragmentMessages.java | 22 ++++++++++++ .../email/ItemDetailsLookupMessage.java | 29 +++++++++++++++ .../eu/faircode/email/ItemDetailsMessage.java | 25 +++++++++++++ .../email/ItemKeyProviderMessage.java | 36 +++++++++++++++++++ .../email/SelectionPredicateMessage.java | 22 ++++++++++++ 6 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/ItemDetailsLookupMessage.java create mode 100644 app/src/main/java/eu/faircode/email/ItemDetailsMessage.java create mode 100644 app/src/main/java/eu/faircode/email/ItemKeyProviderMessage.java create mode 100644 app/src/main/java/eu/faircode/email/SelectionPredicateMessage.java diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 8dcc761b..00166661 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -94,6 +94,8 @@ import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.Observer; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.paging.PagedListAdapter; +import androidx.recyclerview.selection.ItemDetailsLookup; +import androidx.recyclerview.selection.SelectionTracker; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -109,6 +111,8 @@ public class AdapterMessage extends PagedListAdapter selectionTracker = null; + private DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, SimpleDateFormat.LONG); enum ViewType {UNIFIED, FOLDER, THREAD, SEARCH} @@ -159,6 +163,8 @@ public class AdapterMessage extends PagedListAdapter getItemDetails(@NonNull MotionEvent motionEvent) { + return itemDetails; + } } AdapterMessage(Context context, LifecycleOwner owner, ViewType viewType, IProperties properties) { @@ -1380,11 +1393,15 @@ public class AdapterMessage extends PagedListAdapter selectionTracker) { + this.selectionTracker = selectionTracker; + } + interface IProperties { void setExpanded(long id, boolean expand); diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 3efb712e..441cb75b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -61,6 +61,8 @@ import androidx.lifecycle.ViewModelProviders; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; +import androidx.recyclerview.selection.SelectionTracker; +import androidx.recyclerview.selection.StorageStrategy; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -95,6 +97,8 @@ public class FragmentMessages extends FragmentEx { private AdapterMessage.ViewType viewType; private LiveData> messages = null; + private SelectionTracker selectionTracker; + private int autoCount = 0; private boolean autoExpand = true; private List expanded = new ArrayList<>(); @@ -229,6 +233,22 @@ public class FragmentMessages extends FragmentEx { }); rvMessage.setAdapter(adapter); + selectionTracker = new SelectionTracker.Builder<>( + "messages-selection", + rvMessage, + new ItemKeyProviderMessage(rvMessage), + new ItemDetailsLookupMessage(rvMessage), + StorageStrategy.createLongStorage()) + .withSelectionPredicate(new SelectionPredicateMessage()) + .build(); + adapter.setSelectionTracker(selectionTracker); + + selectionTracker.addObserver(new SelectionTracker.SelectionObserver() { + @Override + public void onSelectionChanged() { + } + }); + new ItemTouchHelper(new ItemTouchHelper.Callback() { @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { @@ -515,6 +535,7 @@ public class FragmentMessages extends FragmentEx { outState.putLongArray("expanded", Helper.toLongArray(expanded)); outState.putLongArray("headers", Helper.toLongArray(headers)); outState.putLongArray("images", Helper.toLongArray(images)); + selectionTracker.onSaveInstanceState(outState); } @Override @@ -527,6 +548,7 @@ public class FragmentMessages extends FragmentEx { expanded = Helper.fromLongArray(savedInstanceState.getLongArray("expanded")); headers = Helper.fromLongArray(savedInstanceState.getLongArray("headers")); images = Helper.fromLongArray(savedInstanceState.getLongArray("images")); + selectionTracker.onRestoreInstanceState(savedInstanceState); } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); diff --git a/app/src/main/java/eu/faircode/email/ItemDetailsLookupMessage.java b/app/src/main/java/eu/faircode/email/ItemDetailsLookupMessage.java new file mode 100644 index 00000000..cb721740 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/ItemDetailsLookupMessage.java @@ -0,0 +1,29 @@ +package eu.faircode.email; + +import android.view.MotionEvent; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.selection.ItemDetailsLookup; +import androidx.recyclerview.widget.RecyclerView; + +public class ItemDetailsLookupMessage extends ItemDetailsLookup { + private RecyclerView recyclerView; + + ItemDetailsLookupMessage(RecyclerView recyclerView) { + this.recyclerView = recyclerView; + } + + @Nullable + @Override + public ItemDetails getItemDetails(@NonNull MotionEvent motionEvent) { + View view = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY()); + if (view != null) { + RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view); + if (viewHolder instanceof AdapterMessage.ViewHolder) + return ((AdapterMessage.ViewHolder) viewHolder).getItemDetails(motionEvent); + } + return null; + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/faircode/email/ItemDetailsMessage.java b/app/src/main/java/eu/faircode/email/ItemDetailsMessage.java new file mode 100644 index 00000000..b4a622e1 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/ItemDetailsMessage.java @@ -0,0 +1,25 @@ +package eu.faircode.email; + +import androidx.annotation.Nullable; +import androidx.recyclerview.selection.ItemDetailsLookup; + +public class ItemDetailsMessage extends ItemDetailsLookup.ItemDetails { + private int pos; + private Long key; + + ItemDetailsMessage(int pos, Long id) { + this.pos = pos; + this.key = id; + } + + @Override + public int getPosition() { + return pos; + } + + @Nullable + @Override + public Long getSelectionKey() { + return key; + } +} diff --git a/app/src/main/java/eu/faircode/email/ItemKeyProviderMessage.java b/app/src/main/java/eu/faircode/email/ItemKeyProviderMessage.java new file mode 100644 index 00000000..c2c80954 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/ItemKeyProviderMessage.java @@ -0,0 +1,36 @@ +package eu.faircode.email; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.paging.PagedList; +import androidx.recyclerview.selection.ItemKeyProvider; +import androidx.recyclerview.widget.RecyclerView; + +public class ItemKeyProviderMessage extends ItemKeyProvider { + private RecyclerView recyclerView; + + ItemKeyProviderMessage(RecyclerView recyclerView) { + super(ItemKeyProvider.SCOPE_CACHED); + this.recyclerView = recyclerView; + } + + @Nullable + @Override + public Long getKey(int pos) { + AdapterMessage adapter = (AdapterMessage) recyclerView.getAdapter(); + return adapter.getCurrentList().get(pos).id; + } + + @Override + public int getPosition(@NonNull Long key) { + AdapterMessage adapter = (AdapterMessage) recyclerView.getAdapter(); + PagedList messages = adapter.getCurrentList(); + if (messages != null) + for (int i = 0; i < messages.size(); i++) { + TupleMessageEx message = messages.get(i); + if (message != null && message.id.equals(key)) + return i; + } + return RecyclerView.NO_POSITION; + } +} diff --git a/app/src/main/java/eu/faircode/email/SelectionPredicateMessage.java b/app/src/main/java/eu/faircode/email/SelectionPredicateMessage.java new file mode 100644 index 00000000..987feeed --- /dev/null +++ b/app/src/main/java/eu/faircode/email/SelectionPredicateMessage.java @@ -0,0 +1,22 @@ +package eu.faircode.email; + +import androidx.annotation.NonNull; +import androidx.recyclerview.selection.SelectionTracker; + +public class SelectionPredicateMessage extends SelectionTracker.SelectionPredicate { + + @Override + public boolean canSetStateForKey(@NonNull Long key, boolean nextState) { + return true; + } + + @Override + public boolean canSetStateAtPosition(int position, boolean nextState) { + return true; + } + + @Override + public boolean canSelectMultiple() { + return true; + } +} \ No newline at end of file