FairEmail/app/src/main/java/eu/faircode/email/ViewModelMessages.java

140 lines
5.2 KiB
Java
Raw Normal View History

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
*/
import java.util.HashMap;
import java.util.Map;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ViewModel;
import androidx.paging.PagedList;
public class ViewModelMessages extends ViewModel {
private Map<Boolean, LiveData<PagedList<TupleMessageEx>>> messages = new HashMap<>();
2019-01-14 07:21:48 +00:00
void setMessages(AdapterMessage.ViewType viewType, LifecycleOwner owner, final LiveData<PagedList<TupleMessageEx>> messages) {
final boolean thread = (viewType == AdapterMessage.ViewType.THREAD);
this.messages.put(thread, messages);
2019-01-14 07:21:48 +00:00
// Keep list up-to-date for previous/next navigation
messages.observe(owner, new Observer<PagedList<TupleMessageEx>>() {
@Override
public void onChanged(PagedList<TupleMessageEx> messages) {
}
});
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
Log.i("Removed model thread=" + thread);
ViewModelMessages.this.messages.remove(thread);
}
});
}
void observe(AdapterMessage.ViewType viewType, LifecycleOwner owner, Observer<PagedList<TupleMessageEx>> observer) {
2019-01-20 15:22:21 +00:00
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.INITIALIZED)) {
final boolean thread = (viewType == AdapterMessage.ViewType.THREAD);
messages.get(thread).observe(owner, observer);
}
}
void removeObservers(AdapterMessage.ViewType viewType, LifecycleOwner owner) {
2019-01-20 15:22:21 +00:00
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.INITIALIZED)) {
boolean thread = (viewType == AdapterMessage.ViewType.THREAD);
LiveData<PagedList<TupleMessageEx>> list = messages.get(thread);
if (list != null)
list.removeObservers(owner);
}
}
2018-10-21 14:07:45 +00:00
@Override
protected void onCleared() {
messages.clear();
2018-10-21 14:07:45 +00:00
}
2019-01-26 14:32:52 +00:00
void observePrevNext(LifecycleOwner owner, final String thread, final IPrevNext intf) {
LiveData<PagedList<TupleMessageEx>> list = messages.get(false);
if (list == null) {
Log.w("Observe previous/next without list");
2019-01-26 14:32:52 +00:00
return;
}
2019-01-26 11:56:23 +00:00
Log.i("Observe previous/next thread=" + thread);
2019-01-26 14:32:52 +00:00
list.observe(owner, new Observer<PagedList<TupleMessageEx>>() {
@Override
public void onChanged(PagedList<TupleMessageEx> messages) {
Log.i("Observe previous/next thread=" + thread + " messages=" + messages.size());
for (int pos = 0; pos < messages.size(); pos++) {
TupleMessageEx item = messages.get(pos);
2019-01-26 14:32:52 +00:00
if (item != null && thread.equals(item.thread)) {
boolean load = false;
2019-01-26 14:32:52 +00:00
if (pos - 1 >= 0) {
TupleMessageEx next = messages.get(pos - 1);
2019-01-26 14:32:52 +00:00
if (next == null)
load = true;
reportNext(intf, true, next == null ? null : next.id);
2019-01-26 14:32:52 +00:00
} else
reportNext(intf, false, null);
2019-01-26 14:32:52 +00:00
if (pos + 1 < messages.size()) {
TupleMessageEx prev = messages.get(pos + 1);
2019-01-26 14:32:52 +00:00
if (prev == null)
load = true;
reportPrevious(intf, true, prev == null ? null : prev.id);
2019-01-26 14:32:52 +00:00
} else
reportPrevious(intf, false, null);
2019-01-26 14:32:52 +00:00
if (load)
messages.loadAround(pos);
2019-01-26 14:32:52 +00:00
return;
2019-01-26 14:32:52 +00:00
}
}
Log.w("Observe previous/next gone thread=" + thread);
2019-01-26 14:32:52 +00:00
}
});
2018-10-20 19:04:05 +00:00
}
private void reportPrevious(IPrevNext intf, boolean exists, Long id) {
Log.i("Previous exists=" + exists + " id=" + id);
intf.onPrevious(exists, id);
}
private void reportNext(IPrevNext intf, boolean exists, Long id) {
Log.i("Next exists=" + exists + " id=" + id);
intf.onNext(exists, id);
}
2019-01-26 14:32:52 +00:00
interface IPrevNext {
2019-01-29 10:08:22 +00:00
void onPrevious(boolean exists, Long id);
2019-01-26 14:32:52 +00:00
2019-01-29 10:08:22 +00:00
void onNext(boolean exists, Long id);
}
}