mirror of https://github.com/M66B/FairEmail.git
Dynamic previous/next navigation
This commit is contained in:
parent
0f9371f9ed
commit
5ef43f1ac6
|
@ -279,7 +279,22 @@ public class FragmentMessages extends FragmentBase {
|
|||
|
||||
rvMessage.setAdapter(adapter);
|
||||
|
||||
if (viewType != AdapterMessage.ViewType.THREAD) {
|
||||
if (viewType == AdapterMessage.ViewType.THREAD) {
|
||||
ViewModelMessages model = ViewModelProviders.of(getActivity()).get(ViewModelMessages.class);
|
||||
model.observePrevNext(getViewLifecycleOwner(), thread, new ViewModelMessages.IPrevNext() {
|
||||
@Override
|
||||
public void onPrevious(Long id) {
|
||||
bottom_navigation.getMenu().findItem(R.id.action_prev).setIntent(new Intent().putExtra("id", id));
|
||||
bottom_navigation.getMenu().findItem(R.id.action_prev).setEnabled(id != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Long id) {
|
||||
bottom_navigation.getMenu().findItem(R.id.action_next).setIntent(new Intent().putExtra("id", id));
|
||||
bottom_navigation.getMenu().findItem(R.id.action_next).setEnabled(id != null);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
final SelectionPredicateMessage predicate = new SelectionPredicateMessage(rvMessage);
|
||||
|
||||
selectionTracker = new SelectionTracker.Builder<>(
|
||||
|
@ -341,11 +356,11 @@ public class FragmentMessages extends FragmentBase {
|
|||
return true;
|
||||
|
||||
case R.id.action_prev:
|
||||
navigate(false);
|
||||
navigate(menuItem.getIntent().getLongExtra("id", -1));
|
||||
return true;
|
||||
|
||||
case R.id.action_next:
|
||||
navigate(true);
|
||||
navigate(menuItem.getIntent().getLongExtra("id", -1));
|
||||
return true;
|
||||
|
||||
default:
|
||||
|
@ -393,6 +408,8 @@ public class FragmentMessages extends FragmentBase {
|
|||
// Initialize
|
||||
swipeRefresh.setEnabled(pull);
|
||||
tvNoEmail.setVisibility(View.GONE);
|
||||
bottom_navigation.getMenu().findItem(R.id.action_prev).setEnabled(false);
|
||||
bottom_navigation.getMenu().findItem(R.id.action_next).setEnabled(false);
|
||||
bottom_navigation.setVisibility(View.GONE);
|
||||
grpReady.setVisibility(View.GONE);
|
||||
pbWait.setVisibility(View.VISIBLE);
|
||||
|
@ -2015,24 +2032,41 @@ public class FragmentMessages extends FragmentBase {
|
|||
private void handleAutoClose() {
|
||||
if (autoclose)
|
||||
finish();
|
||||
else if (autonext)
|
||||
navigate(true);
|
||||
else if (autonext) {
|
||||
Intent intent = bottom_navigation.getMenu().findItem(R.id.action_next).getIntent();
|
||||
Long id = (intent == null ? null : intent.getLongExtra("id", -1));
|
||||
if (id == null || id < 0)
|
||||
finish();
|
||||
else
|
||||
navigate(id);
|
||||
}
|
||||
}
|
||||
|
||||
private void navigate(boolean next) {
|
||||
ViewModelMessages model = ViewModelProviders.of(getActivity()).get(ViewModelMessages.class);
|
||||
ViewModelMessages.Target target = model.getPrevNext(thread)[next ? 1 : 0];
|
||||
private void navigate(long id) {
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", id);
|
||||
new SimpleTask<EntityMessage>() {
|
||||
@Override
|
||||
protected EntityMessage onExecute(Context context, Bundle args) {
|
||||
long id = args.getLong("id");
|
||||
return DB.getInstance(context).message().getMessage(id);
|
||||
}
|
||||
|
||||
if (target == null)
|
||||
finish();
|
||||
else {
|
||||
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
|
||||
lbm.sendBroadcast(
|
||||
new Intent(ActivityView.ACTION_VIEW_THREAD)
|
||||
.putExtra("account", target.account)
|
||||
.putExtra("thread", target.thread)
|
||||
.putExtra("id", target.id));
|
||||
}
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, EntityMessage message) {
|
||||
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
|
||||
lbm.sendBroadcast(
|
||||
new Intent(ActivityView.ACTION_VIEW_THREAD)
|
||||
.putExtra("account", message.account)
|
||||
.putExtra("thread", message.thread)
|
||||
.putExtra("id", message.id));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
|
||||
}
|
||||
}.execute(this, args, "messages:navigate");
|
||||
}
|
||||
|
||||
private void moveAsk(final ArrayList<MessageTarget> result) {
|
||||
|
|
|
@ -81,47 +81,49 @@ public class ViewModelMessages extends ViewModel {
|
|||
messages.clear();
|
||||
}
|
||||
|
||||
Target[] getPrevNext(String thread) {
|
||||
void observePrevNext(LifecycleOwner owner, final String thread, final IPrevNext intf) {
|
||||
LiveData<PagedList<TupleMessageEx>> list = messages.get(false);
|
||||
if (list == null || list.getValue() == null || list.getValue().size() == 0)
|
||||
return new Target[]{null, null};
|
||||
if (list == null)
|
||||
return;
|
||||
|
||||
boolean found = false;
|
||||
TupleMessageEx prev = null;
|
||||
TupleMessageEx next = null;
|
||||
for (int i = 0; i < list.getValue().size(); i++) {
|
||||
TupleMessageEx item = list.getValue().get(i);
|
||||
if (item == null)
|
||||
continue;
|
||||
if (found) {
|
||||
prev = item;
|
||||
break;
|
||||
list.observe(owner, new Observer<PagedList<TupleMessageEx>>() {
|
||||
@Override
|
||||
public void onChanged(PagedList<TupleMessageEx> list) {
|
||||
boolean load = false;
|
||||
for (int pos = 0; pos < list.size(); pos++) {
|
||||
TupleMessageEx item = list.get(pos);
|
||||
if (item != null && thread.equals(item.thread)) {
|
||||
if (pos - 1 >= 0) {
|
||||
TupleMessageEx next = list.get(pos - 1);
|
||||
if (next == null)
|
||||
load = true;
|
||||
else
|
||||
intf.onNext(next.id);
|
||||
} else
|
||||
intf.onNext(null);
|
||||
|
||||
if (pos + 1 <= list.size()) {
|
||||
TupleMessageEx prev = list.get(pos + 1);
|
||||
if (prev == null)
|
||||
load = true;
|
||||
else
|
||||
intf.onPrevious(prev.id);
|
||||
} else
|
||||
intf.onPrevious(null);
|
||||
|
||||
if (load)
|
||||
list.loadAround(pos);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (thread.equals(item.thread)) {
|
||||
found = true;
|
||||
list.getValue().loadAround(i);
|
||||
} else
|
||||
next = item;
|
||||
}
|
||||
if (!found)
|
||||
return new Target[]{null, null};
|
||||
|
||||
return new Target[]{
|
||||
prev == null ? null : new Target(prev.account, prev.thread, prev.id, prev.ui_found),
|
||||
next == null ? null : new Target(next.account, next.thread, next.id, next.ui_found)};
|
||||
});
|
||||
}
|
||||
|
||||
class Target {
|
||||
long account;
|
||||
String thread;
|
||||
long id;
|
||||
boolean found;
|
||||
interface IPrevNext {
|
||||
void onPrevious(Long id);
|
||||
|
||||
Target(long account, String thread, long id, boolean found) {
|
||||
this.account = account;
|
||||
this.thread = thread;
|
||||
this.id = id;
|
||||
this.found = found;
|
||||
}
|
||||
void onNext(Long id);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue