2018-10-20 14:42:03 +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
|
|
|
*/
|
|
|
|
|
|
2019-01-12 10:45:57 +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;
|
2018-10-20 14:42:03 +00:00
|
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
|
import androidx.paging.PagedList;
|
|
|
|
|
|
|
|
|
|
public class ViewModelMessages extends ViewModel {
|
2019-01-12 10:45:57 +00:00
|
|
|
private Map<Boolean, LiveData<PagedList<TupleMessageEx>>> messages = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
void setMessages(AdapterMessage.ViewType viewType, LiveData<PagedList<TupleMessageEx>> messages) {
|
|
|
|
|
boolean thread = (viewType == AdapterMessage.ViewType.THREAD);
|
|
|
|
|
this.messages.put(thread, messages);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void observe(AdapterMessage.ViewType viewType, LifecycleOwner owner, Observer<PagedList<TupleMessageEx>> observer) {
|
|
|
|
|
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
|
|
|
|
|
final boolean thread = (viewType == AdapterMessage.ViewType.THREAD);
|
|
|
|
|
messages.get(thread).observe(owner, observer);
|
|
|
|
|
|
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
|
public void onDestroyed() {
|
|
|
|
|
Log.i("Removed model thread=" + thread);
|
|
|
|
|
messages.remove(thread);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void removeObservers(AdapterMessage.ViewType viewType, LifecycleOwner owner) {
|
|
|
|
|
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
|
|
|
|
|
boolean thread = (viewType == AdapterMessage.ViewType.THREAD);
|
|
|
|
|
LiveData<PagedList<TupleMessageEx>> list = messages.get(thread);
|
|
|
|
|
if (list != null)
|
|
|
|
|
list.removeObservers(owner);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-20 14:42:03 +00:00
|
|
|
|
2019-01-12 10:45:57 +00:00
|
|
|
boolean isEmpty(AdapterMessage.ViewType viewType) {
|
|
|
|
|
boolean thread = (viewType == AdapterMessage.ViewType.THREAD);
|
|
|
|
|
LiveData<PagedList<TupleMessageEx>> list = messages.get(thread);
|
|
|
|
|
return (list == null || list.getValue() == null || list.getValue().size() == 0);
|
2018-10-20 14:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-21 14:07:45 +00:00
|
|
|
@Override
|
|
|
|
|
protected void onCleared() {
|
2019-01-12 10:45:57 +00:00
|
|
|
messages.clear();
|
2018-10-21 14:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-21 08:49:03 +00:00
|
|
|
Target[] getPrevNext(String thread) {
|
2019-01-12 10:45:57 +00:00
|
|
|
LiveData<PagedList<TupleMessageEx>> list = messages.get(false);
|
|
|
|
|
if (list == null || list.getValue() == null || list.getValue().size() == 0)
|
2018-10-21 08:49:03 +00:00
|
|
|
return new Target[]{null, null};
|
2018-10-20 14:42:03 +00:00
|
|
|
|
|
|
|
|
boolean found = false;
|
|
|
|
|
TupleMessageEx prev = null;
|
|
|
|
|
TupleMessageEx next = null;
|
2019-01-12 10:45:57 +00:00
|
|
|
for (int i = 0; i < list.getValue().size(); i++) {
|
|
|
|
|
TupleMessageEx item = list.getValue().get(i);
|
2018-10-20 14:42:03 +00:00
|
|
|
if (item == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (found) {
|
2018-10-21 08:49:03 +00:00
|
|
|
prev = item;
|
2019-01-12 10:45:57 +00:00
|
|
|
list.getValue().loadAround(i);
|
2018-10-20 14:42:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (thread.equals(item.thread))
|
|
|
|
|
found = true;
|
|
|
|
|
else
|
2018-10-21 08:49:03 +00:00
|
|
|
next = item;
|
2018-10-20 14:42:03 +00:00
|
|
|
}
|
2018-10-21 08:49:03 +00:00
|
|
|
return new Target[]{
|
2018-11-29 09:51:22 +01:00
|
|
|
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)};
|
2018-10-20 19:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-21 08:49:03 +00:00
|
|
|
class Target {
|
2018-10-20 19:04:05 +00:00
|
|
|
long account;
|
|
|
|
|
String thread;
|
2018-11-29 09:51:22 +01:00
|
|
|
long id;
|
2018-11-05 18:56:57 +00:00
|
|
|
boolean found;
|
2018-10-20 19:04:05 +00:00
|
|
|
|
2018-11-29 09:51:22 +01:00
|
|
|
Target(long account, String thread, long id, boolean found) {
|
2018-10-20 19:04:05 +00:00
|
|
|
this.account = account;
|
|
|
|
|
this.thread = thread;
|
2018-11-29 09:51:22 +01:00
|
|
|
this.id = id;
|
2018-11-05 18:56:57 +00:00
|
|
|
this.found = found;
|
2018-10-20 19:04:05 +00:00
|
|
|
}
|
2018-10-20 14:42:03 +00:00
|
|
|
}
|
|
|
|
|
}
|