2018-10-24 09:09:07 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
2018-10-29 08:09:56 +00:00
|
|
|
/*
|
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-10-29 08:09:56 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-10-29 08:09:56 +00:00
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-10-29 08:09:56 +00:00
|
|
|
*/
|
|
|
|
|
|
2018-10-24 09:09:07 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.recyclerview.selection.SelectionTracker;
|
2018-10-24 15:18:01 +00:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2018-10-24 09:09:07 +00:00
|
|
|
|
|
|
|
|
public class SelectionPredicateMessage extends SelectionTracker.SelectionPredicate<Long> {
|
2019-02-04 14:57:55 +00:00
|
|
|
private boolean enabled;
|
2018-10-24 15:18:01 +00:00
|
|
|
private RecyclerView recyclerView;
|
|
|
|
|
|
|
|
|
|
SelectionPredicateMessage(RecyclerView recyclerView) {
|
2019-02-04 14:57:55 +00:00
|
|
|
this.enabled = true;
|
2018-10-24 15:18:01 +00:00
|
|
|
this.recyclerView = recyclerView;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 14:57:55 +00:00
|
|
|
void setEnabled(boolean enabled) {
|
|
|
|
|
this.enabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 09:09:07 +00:00
|
|
|
@Override
|
|
|
|
|
public boolean canSetStateForKey(@NonNull Long key, boolean nextState) {
|
2019-02-04 14:57:55 +00:00
|
|
|
if (!enabled)
|
|
|
|
|
return false;
|
|
|
|
|
|
2018-10-24 15:18:01 +00:00
|
|
|
AdapterMessage adapter = (AdapterMessage) recyclerView.getAdapter();
|
2018-12-03 13:18:23 +01:00
|
|
|
TupleMessageEx message = adapter.getItemForKey(key);
|
2019-04-11 19:37:34 +02:00
|
|
|
if (message == null) // happens when restoring state
|
|
|
|
|
return true;
|
2018-12-03 13:18:23 +01:00
|
|
|
|
2019-11-23 13:48:59 +01:00
|
|
|
if (message.accountProtocol != EntityAccount.TYPE_IMAP)
|
2019-11-11 14:44:55 +01:00
|
|
|
return true;
|
|
|
|
|
|
2019-07-07 09:25:52 +02:00
|
|
|
if (message.uid != null && !message.folderReadOnly)
|
2018-12-03 13:18:23 +01:00
|
|
|
return true;
|
|
|
|
|
|
2018-10-24 15:18:01 +00:00
|
|
|
return false;
|
2018-10-24 09:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean canSetStateAtPosition(int position, boolean nextState) {
|
2019-02-04 14:57:55 +00:00
|
|
|
if (!enabled)
|
|
|
|
|
return false;
|
|
|
|
|
|
2018-10-24 15:18:01 +00:00
|
|
|
AdapterMessage adapter = (AdapterMessage) recyclerView.getAdapter();
|
2018-12-03 13:18:23 +01:00
|
|
|
TupleMessageEx message = adapter.getItemAtPosition(position);
|
2019-04-11 19:37:34 +02:00
|
|
|
if (message == null) // happens when restoring state
|
|
|
|
|
return true;
|
2018-12-03 13:18:23 +01:00
|
|
|
|
2019-11-23 13:48:59 +01:00
|
|
|
if (message.accountProtocol != EntityAccount.TYPE_IMAP)
|
2019-11-11 14:44:55 +01:00
|
|
|
return true;
|
|
|
|
|
|
2019-07-07 09:25:52 +02:00
|
|
|
if (message.uid != null && !message.folderReadOnly)
|
2018-12-03 13:18:23 +01:00
|
|
|
return true;
|
|
|
|
|
|
2018-11-23 08:53:18 +01:00
|
|
|
return false;
|
2018-10-24 09:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean canSelectMultiple() {
|
2019-02-05 07:35:55 +00:00
|
|
|
return true;
|
2018-10-24 09:09:07 +00:00
|
|
|
}
|
|
|
|
|
}
|