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

653 lines
28 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
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2018-10-29 08:09:56 +00:00
*/
2019-05-14 16:34:09 +00:00
import android.content.Context;
import android.content.SharedPreferences;
2019-07-28 08:09:00 +00:00
import android.os.Bundle;
2019-05-14 16:34:09 +00:00
import android.text.TextUtils;
2020-06-29 15:06:25 +00:00
import android.util.Pair;
2019-05-14 16:34:09 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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;
2019-05-14 16:34:09 +00:00
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;
2019-05-14 16:34:09 +00:00
import androidx.preference.PreferenceManager;
2019-07-28 08:09:00 +00:00
import androidx.room.paging.LimitOffsetDataSource;
2019-05-14 16:34:09 +00:00
import java.util.ArrayList;
import java.util.HashMap;
2019-07-28 08:09:00 +00:00
import java.util.List;
import java.util.Map;
2019-05-14 16:34:09 +00:00
import java.util.Objects;
import java.util.concurrent.ExecutorService;
public class ViewModelMessages extends ViewModel {
2019-05-14 16:34:09 +00:00
private AdapterMessage.ViewType last = AdapterMessage.ViewType.UNIFIED;
2020-04-09 19:19:44 +00:00
private Map<AdapterMessage.ViewType, Model> models = new HashMap<AdapterMessage.ViewType, Model>() {
@Nullable
@Override
public Model put(AdapterMessage.ViewType key, Model value) {
Model existing = this.get(key);
if (existing != null && existing.boundary != null)
2021-06-18 14:02:46 +00:00
existing.boundary.destroy(existing.boundary.getState());
2020-04-09 19:19:44 +00:00
return super.put(key, value);
}
@Nullable
@Override
public Model remove(@Nullable Object key) {
Model existing = this.get(key);
if (existing != null && existing.boundary != null)
2021-06-18 14:02:46 +00:00
existing.boundary.destroy(existing.boundary.getState());
2020-04-09 19:19:44 +00:00
return super.remove(key);
}
};
2019-05-14 16:34:09 +00:00
2020-05-01 18:26:34 +00:00
// AndroidX IO = 4 threads
private ExecutorService executor = Helper.getBackgroundExecutor(4, "model");
2019-05-14 16:34:09 +00:00
2020-04-26 14:30:41 +00:00
private static final int LOCAL_PAGE_SIZE = 50;
2020-06-01 17:23:34 +00:00
private static final int THREAD_PAGE_SIZE = 100;
2019-05-14 16:34:09 +00:00
private static final int REMOTE_PAGE_SIZE = 10;
2019-10-29 10:35:44 +00:00
private static final int SEARCH_PAGE_SIZE = 10;
2020-11-10 09:11:21 +00:00
private static final int MAX_CACHED_ITEMS = LOCAL_PAGE_SIZE * 50;
2021-11-22 10:08:24 +00:00
private static final int CHUNK_SIZE = 100;
2019-05-14 16:34:09 +00:00
2019-05-15 15:05:11 +00:00
Model getModel(
final Context context, final LifecycleOwner owner,
2019-05-14 16:34:09 +00:00
final AdapterMessage.ViewType viewType,
2019-07-19 06:27:44 +00:00
String type, long account, long folder,
String thread, long id,
boolean threading,
boolean filter_archive,
2020-04-09 15:50:29 +00:00
BoundaryCallbackMessages.SearchCriteria criteria, boolean server) {
2019-05-14 16:34:09 +00:00
Args args = new Args(context,
viewType, type, account, folder,
thread, id, threading,
filter_archive, criteria, server);
2021-08-30 06:25:07 +00:00
Log.i("Get model=" + viewType + " " + args);
2019-05-14 16:34:09 +00:00
dump();
Model model = models.get(viewType);
if (model == null || !model.args.equals(args)) {
2021-08-30 06:25:07 +00:00
Log.i("Creating model=" + viewType + " replace=" + (model != null));
2019-08-09 17:29:17 +00:00
if (model != null)
2019-06-20 05:25:38 +00:00
model.list.removeObservers(owner);
2019-01-14 07:21:48 +00:00
2019-05-14 16:34:09 +00:00
DB db = DB.getInstance(context);
BoundaryCallbackMessages boundary = null;
2019-10-02 19:13:10 +00:00
if (viewType == AdapterMessage.ViewType.FOLDER)
2019-05-14 16:34:09 +00:00
boundary = new BoundaryCallbackMessages(context,
2021-11-22 07:36:51 +00:00
viewType, args.account, args.folder, true, args.criteria, REMOTE_PAGE_SIZE);
2019-10-02 19:13:10 +00:00
else if (viewType == AdapterMessage.ViewType.SEARCH)
boundary = new BoundaryCallbackMessages(context,
2021-11-22 07:36:51 +00:00
viewType, args.account, args.folder, args.server, args.criteria,
2019-10-02 19:13:10 +00:00
args.server ? REMOTE_PAGE_SIZE : SEARCH_PAGE_SIZE);
2019-05-14 16:34:09 +00:00
LivePagedListBuilder<Integer, TupleMessageEx> builder = null;
switch (viewType) {
case UNIFIED:
2020-07-27 11:37:05 +00:00
PagedList.Config configUnified = new PagedList.Config.Builder()
.setPageSize(LOCAL_PAGE_SIZE)
2020-11-10 09:11:21 +00:00
.setMaxSize(MAX_CACHED_ITEMS)
2020-07-27 11:37:05 +00:00
.build();
2019-05-14 16:34:09 +00:00
builder = new LivePagedListBuilder<>(
2019-07-19 06:27:44 +00:00
db.message().pagedUnified(
args.type,
2019-05-14 16:34:09 +00:00
args.threading,
args.group_category,
args.sort, args.ascending,
args.filter_seen,
args.filter_unflagged,
args.filter_unknown,
args.filter_snoozed,
2021-12-26 20:37:54 +00:00
args.filter_deleted,
2020-03-27 09:47:49 +00:00
args.filter_language,
2019-05-14 16:34:09 +00:00
false,
args.debug),
2020-07-27 11:37:05 +00:00
configUnified);
2019-05-14 16:34:09 +00:00
break;
case FOLDER:
PagedList.Config configFolder = new PagedList.Config.Builder()
2019-06-07 07:53:29 +00:00
.setInitialLoadSizeHint(LOCAL_PAGE_SIZE)
2019-05-14 16:34:09 +00:00
.setPageSize(LOCAL_PAGE_SIZE)
.setPrefetchDistance(REMOTE_PAGE_SIZE)
2020-11-10 09:11:21 +00:00
.setMaxSize(MAX_CACHED_ITEMS)
2019-05-14 16:34:09 +00:00
.build();
builder = new LivePagedListBuilder<>(
db.message().pagedFolder(
args.folder, args.threading,
args.sort, args.ascending,
args.filter_seen,
args.filter_unflagged,
args.filter_unknown,
args.filter_snoozed,
2021-12-26 20:37:54 +00:00
args.filter_deleted,
2020-03-27 09:47:49 +00:00
args.filter_language,
2019-05-14 16:34:09 +00:00
false,
args.debug),
configFolder);
builder.setBoundaryCallback(boundary);
break;
case THREAD:
2020-07-27 11:37:05 +00:00
PagedList.Config configThread = new PagedList.Config.Builder()
.setPageSize(THREAD_PAGE_SIZE)
.build();
2019-05-14 16:34:09 +00:00
builder = new LivePagedListBuilder<>(
db.message().pagedThread(
args.account, args.thread,
args.threading ? null : args.id,
args.filter_archive && args.threading,
args.ascending,
2020-07-27 11:37:05 +00:00
args.debug),
configThread);
2019-05-14 16:34:09 +00:00
break;
case SEARCH:
PagedList.Config configSearch = new PagedList.Config.Builder()
.setPageSize(LOCAL_PAGE_SIZE)
.setPrefetchDistance(REMOTE_PAGE_SIZE)
2020-11-10 09:11:21 +00:00
.setMaxSize(MAX_CACHED_ITEMS)
2019-05-14 16:34:09 +00:00
.build();
if (args.folder < 0)
builder = new LivePagedListBuilder<>(
2019-07-19 06:27:44 +00:00
db.message().pagedUnified(
null,
args.threading, false,
"time", false,
2021-12-26 20:37:54 +00:00
false, false, false, false, false,
2020-03-27 09:47:49 +00:00
null,
2019-05-14 16:34:09 +00:00
true,
args.debug),
configSearch);
else
builder = new LivePagedListBuilder<>(
db.message().pagedFolder(
args.folder, args.threading,
"time", false,
2021-12-26 20:37:54 +00:00
false, false, false, false, false,
2020-03-27 09:47:49 +00:00
null,
2019-05-14 16:34:09 +00:00
true,
args.debug),
configSearch);
builder.setBoundaryCallback(boundary);
break;
}
2019-02-05 08:15:05 +00:00
2019-05-14 16:34:09 +00:00
builder.setFetchExecutor(executor);
model = new Model(args, builder.build(), boundary);
models.put(viewType, model);
2019-01-31 08:57:28 +00:00
}
2019-05-17 14:44:37 +00:00
owner.getLifecycle().addObserver(new LifecycleObserver() {
2022-04-26 06:05:41 +00:00
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPaused() {
Log.i("Paused model=" + viewType + " last=" + last);
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResumed() {
Log.i("Resumed model=" + viewType + " last=" + last);
}
2019-05-17 14:44:37 +00:00
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
2021-08-30 06:25:07 +00:00
Log.i("Destroy model=" + viewType);
2019-05-17 14:44:37 +00:00
Model model = models.get(viewType);
if (model != null) {
2021-08-30 06:25:07 +00:00
Log.i("Remove observer model=" + viewType);
2019-05-17 14:44:37 +00:00
model.list.removeObservers(owner);
}
2020-06-29 15:07:15 +00:00
if (viewType == AdapterMessage.ViewType.THREAD) {
2021-08-30 06:25:07 +00:00
Log.i("Remove model=" + viewType);
2019-08-09 17:29:17 +00:00
models.remove(viewType);
2019-05-17 14:44:37 +00:00
}
dump();
2021-09-28 08:18:20 +00:00
owner.getLifecycle().removeObserver(this);
2019-05-17 14:44:37 +00:00
}
});
2019-05-14 16:34:09 +00:00
if (viewType == AdapterMessage.ViewType.UNIFIED) {
2019-08-09 17:29:17 +00:00
models.remove(AdapterMessage.ViewType.FOLDER);
models.remove(AdapterMessage.ViewType.SEARCH);
2019-05-14 16:34:09 +00:00
} else if (viewType == AdapterMessage.ViewType.FOLDER)
2019-08-09 17:29:17 +00:00
models.remove(AdapterMessage.ViewType.SEARCH);
2019-05-16 09:24:58 +00:00
if (viewType != AdapterMessage.ViewType.THREAD) {
last = viewType;
2021-08-30 06:25:07 +00:00
Log.i("Last model=" + last);
2019-05-16 09:24:58 +00:00
}
2019-02-05 08:15:05 +00:00
2021-08-30 06:25:07 +00:00
Log.i("Returning model=" + viewType);
2019-05-14 16:34:09 +00:00
dump();
2019-05-15 15:05:11 +00:00
return model;
}
2020-03-24 12:20:27 +00:00
void retry(AdapterMessage.ViewType viewType) {
Model model = models.get(viewType);
if (model != null && model.boundary != null)
model.boundary.retry();
}
2018-10-21 14:07:45 +00:00
@Override
protected void onCleared() {
2019-05-14 16:34:09 +00:00
for (AdapterMessage.ViewType viewType : new ArrayList<>(models.keySet()))
models.remove(viewType);
}
2021-07-19 17:59:30 +00:00
void observePrevNext(Context context, LifecycleOwner owner, final long id, int lpos, final IPrevNext intf) {
Log.i("Observe prev/next model=" + last + " id=" + id + " lpos=" + lpos);
2019-02-05 08:15:05 +00:00
2020-06-29 15:06:25 +00:00
final Model model = models.get(last);
2019-05-14 16:34:09 +00:00
if (model == null) {
// When showing accounts or folders
intf.onPrevious(false, null);
intf.onNext(false, null);
intf.onFound(-1, 0);
2019-01-26 14:32:52 +00:00
return;
}
2019-01-26 11:56:23 +00:00
2021-02-13 11:50:23 +00:00
Log.i("Observe previous/next id=" + id);
2021-07-19 17:59:30 +00:00
//model.list.getValue().loadAround(lpos);
2019-05-14 16:34:09 +00:00
model.list.observe(owner, new Observer<PagedList<TupleMessageEx>>() {
2021-07-20 06:02:44 +00:00
private final Pair<Long, Integer>[] lastState = new Pair[3];
2020-06-29 15:06:25 +00:00
2019-01-26 14:32:52 +00:00
@Override
public void onChanged(PagedList<TupleMessageEx> messages) {
2021-07-20 06:02:44 +00:00
Log.i("Observe previous/next id=" + id +
" lpos=" + lpos +
" messages=" + messages.size());
2021-07-20 06:02:44 +00:00
Pair<Long, Integer>[] curState = new Pair[3];
for (int pos = 0; pos < messages.size(); pos++) {
TupleMessageEx item = messages.get(pos);
if (item != null && id == item.id) {
2021-07-20 06:02:44 +00:00
curState[1] = new Pair<>(id, pos);
2021-02-13 12:55:32 +00:00
Log.i("Observe previous/next found id=" + id + " pos=" + pos);
2020-06-30 16:36:33 +00:00
2019-01-26 14:32:52 +00:00
if (pos - 1 >= 0) {
TupleMessageEx next = messages.get(pos - 1);
2021-07-21 14:52:45 +00:00
curState[2] = new Pair<>(next == null ? null : next.id, pos - 1);
2021-07-20 06:02:44 +00:00
}
2019-01-26 14:32:52 +00:00
if (pos + 1 < messages.size()) {
TupleMessageEx prev = messages.get(pos + 1);
2021-07-21 14:52:45 +00:00
curState[0] = new Pair<>(prev == null ? null : prev.id, pos + 1);
2021-07-20 06:02:44 +00:00
}
2019-02-02 12:58:28 +00:00
2021-07-20 06:02:44 +00:00
break;
2019-01-26 14:32:52 +00:00
}
}
2021-08-05 05:45:48 +00:00
if (curState[1] == null) // first changed
curState[1] = new Pair<>(id, -1);
2021-07-20 06:02:44 +00:00
if (Objects.equals(lastState[0], curState[0]) &&
Objects.equals(lastState[1], curState[1]) &&
Objects.equals(lastState[2], curState[2])) {
Log.i("Observe previous/next unchanged");
2020-06-29 17:29:06 +00:00
return;
2021-07-20 06:02:44 +00:00
}
2020-06-29 17:29:06 +00:00
2021-08-02 05:03:54 +00:00
Log.i("Observe previous/next" +
" prev=" + (curState[0] == null ? null : curState[0].first + "/" + curState[0].second) +
" base=" + (curState[1] == null ? null : curState[1].first + "/" + curState[1].second) +
" next=" + (curState[2] == null ? null : curState[2].first + "/" + curState[2].second));
2021-08-05 05:45:48 +00:00
if (curState[1].second >= 0)
2021-07-20 06:02:44 +00:00
intf.onFound(curState[1].second, messages.size());
2021-08-05 05:45:48 +00:00
if (!(lastState[0] != null && curState[0] == null))
2021-08-02 05:03:54 +00:00
intf.onPrevious(curState[0] != null, curState[0] == null ? null : curState[0].first);
2021-08-05 05:45:48 +00:00
if (!(lastState[2] != null && curState[2] == null))
2021-08-02 05:03:54 +00:00
intf.onNext(curState[2] != null, curState[2] == null ? null : curState[2].first);
2021-07-20 06:02:44 +00:00
2021-08-05 05:45:48 +00:00
lastState[0] = curState[0];
lastState[1] = curState[1];
lastState[2] = curState[2];
if (curState[1].second >= 0 &&
2021-07-20 06:02:44 +00:00
(curState[0] == null || curState[0].first != null) &&
(curState[2] == null || curState[2].first != null))
return;
2020-06-29 17:29:06 +00:00
2020-06-29 15:06:25 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
2021-07-19 17:59:30 +00:00
args.putInt("lpos", lpos);
2020-06-29 15:06:25 +00:00
new SimpleTask<Pair<Long, Long>>() {
@Override
protected Pair<Long, Long> onExecute(Context context, Bundle args) {
long id = args.getLong("id");
2021-07-19 17:59:30 +00:00
int lpos = args.getInt("lpos");
2020-06-29 15:06:25 +00:00
2022-04-24 08:37:47 +00:00
if (!isAlive())
return null;
2020-06-29 15:06:25 +00:00
PagedList<TupleMessageEx> plist = model.list.getValue();
if (plist == null)
return null;
LimitOffsetDataSource<TupleMessageEx> ds = (LimitOffsetDataSource<TupleMessageEx>) plist.getDataSource();
int count = ds.countItems();
2021-07-19 17:59:30 +00:00
if (lpos >= 0) {
int from = Math.max(0, lpos - 10);
int load = Math.min(20, count - from);
Log.i("Observe previous/next load lpos=" + lpos +
" range=" + from + "/#" + load);
List<TupleMessageEx> messages = ds.loadRange(from, load);
for (int j = 0; j < messages.size(); j++)
if (messages.get(j).id == id)
return getPair(plist, ds, count, from + j);
}
2022-04-24 08:37:47 +00:00
for (int i = 0; i < count && isAlive(); i += CHUNK_SIZE) {
2021-07-19 17:59:30 +00:00
Log.i("Observe previous/next load" +
" range=" + i + "/#" + count);
2021-11-22 10:08:24 +00:00
List<TupleMessageEx> messages = ds.loadRange(i, Math.min(CHUNK_SIZE, count - i));
2020-06-29 15:06:25 +00:00
for (int j = 0; j < messages.size(); j++)
2021-07-19 17:59:30 +00:00
if (messages.get(j).id == id)
return getPair(plist, ds, count, i + j);
2021-11-22 07:00:41 +00:00
2021-11-22 10:08:24 +00:00
if (lpos < 0 && i == CHUNK_SIZE * 2 && count > CHUNK_SIZE * 4)
i = count - CHUNK_SIZE * 2;
2020-06-29 15:06:25 +00:00
}
2021-11-20 06:56:44 +00:00
Log.i("Observe previous/next message not found" +
2021-11-21 16:14:09 +00:00
" lpos=" + lpos + " count=" + count + " " + model.args);
2021-11-20 06:56:44 +00:00
2020-06-29 15:06:25 +00:00
return null;
}
2021-08-02 05:03:54 +00:00
@Override
protected void onExecuted(Bundle args, Pair<Long, Long> data) {
if (data == null) {
Log.i("Observe previous/next fallback=none");
return; // keep current
}
intf.onPrevious(data.first != null, data.first);
intf.onNext(data.second != null, data.second);
intf.onFound(-1, messages.size());
}
@Override
protected void onException(Bundle args, Throwable ex) {
// No nothing
}
2021-07-19 17:59:30 +00:00
private Pair<Long, Long> getPair(
PagedList<TupleMessageEx> plist,
LimitOffsetDataSource<TupleMessageEx> ds,
int count, int pos) {
if (pos < plist.size())
plist.loadAround(pos);
List<TupleMessageEx> lprev = null;
if (pos - 1 >= 0)
lprev = ds.loadRange(pos - 1, 1);
List<TupleMessageEx> lnext = null;
if (pos + 1 < count)
lnext = ds.loadRange(pos + 1, 1);
TupleMessageEx prev = (lprev != null && lprev.size() > 0 ? lprev.get(0) : null);
TupleMessageEx next = (lnext != null && lnext.size() > 0 ? lnext.get(0) : null);
Pair<Long, Long> result = new Pair<>(
prev == null ? null : prev.id,
next == null ? null : next.id);
Log.i("Observe previous/next fallback=" + result);
return result;
}
2022-04-24 08:37:47 +00:00
}.execute(context, owner, args, "model:fallback");
2019-01-26 14:32:52 +00:00
}
});
2018-10-20 19:04:05 +00:00
}
void getIds(Context context, LifecycleOwner owner, long from, long to, final Observer<List<Long>> observer) {
2019-07-28 08:09:00 +00:00
final Model model = models.get(last);
if (model == null) {
Log.w("Get IDs without model");
observer.onChanged(new ArrayList<Long>());
return;
}
new SimpleTask<List<Long>>() {
@Override
protected List<Long> onExecute(Context context, Bundle args) {
List<Long> ids = new ArrayList<>();
2022-04-24 08:37:47 +00:00
if (!isAlive())
return ids;
2019-08-11 08:18:23 +00:00
PagedList<TupleMessageEx> plist = model.list.getValue();
if (plist == null)
return ids;
2019-10-04 17:17:33 +00:00
2019-08-11 08:18:23 +00:00
LimitOffsetDataSource<TupleMessageEx> ds = (LimitOffsetDataSource<TupleMessageEx>) plist.getDataSource();
2019-07-28 08:09:00 +00:00
int count = ds.countItems();
2022-04-24 08:37:47 +00:00
for (int i = 0; i < count && isAlive(); i += 100)
2019-07-28 08:09:00 +00:00
for (TupleMessageEx message : ds.loadRange(i, Math.min(100, count - i)))
if ((message.received >= from && message.received < to) &&
((message.uid != null && !message.folderReadOnly) ||
message.accountProtocol != EntityAccount.TYPE_IMAP))
ids.add(message.id);
2019-07-28 08:09:00 +00:00
Log.i("Loaded messages #" + ids.size());
return ids;
}
@Override
protected void onExecuted(Bundle args, List<Long> ids) {
observer.onChanged(ids);
}
@Override
protected void onException(Bundle args, Throwable ex) {
observer.onChanged(new ArrayList<Long>());
}
}.execute(context, owner, new Bundle(), "model:ids");
}
2022-04-26 05:58:16 +00:00
void cleanup() {
dump();
for (AdapterMessage.ViewType viewType : new ArrayList<>(models.keySet())) {
if (viewType != last && !models.get(viewType).list.hasObservers()) {
Log.i("Cleanup model viewType=" + viewType);
models.remove(viewType);
}
}
}
2019-05-14 16:34:09 +00:00
private class Args {
private long account;
2019-07-19 06:27:44 +00:00
private String type;
2019-05-14 16:34:09 +00:00
private long folder;
private String thread;
private long id;
2020-04-09 15:50:29 +00:00
private BoundaryCallbackMessages.SearchCriteria criteria;
2019-05-14 16:34:09 +00:00
private boolean server;
private boolean threading;
private boolean group_category;
2019-05-14 16:34:09 +00:00
private String sort;
2019-09-03 08:33:52 +00:00
private boolean ascending;
2019-05-14 16:34:09 +00:00
private boolean filter_seen;
private boolean filter_unflagged;
private boolean filter_unknown;
2019-05-14 16:34:09 +00:00
private boolean filter_snoozed;
private boolean filter_archive;
2021-12-26 20:37:54 +00:00
private boolean filter_deleted;
2020-03-27 09:47:49 +00:00
private String filter_language;
2019-05-14 16:34:09 +00:00
private boolean debug;
Args(Context context,
2019-09-09 07:46:54 +00:00
AdapterMessage.ViewType viewType,
2019-07-19 06:27:44 +00:00
String type, long account, long folder,
String thread, long id, boolean threading,
boolean filter_archive,
2020-04-09 15:50:29 +00:00
BoundaryCallbackMessages.SearchCriteria criteria, boolean server) {
2019-05-14 16:34:09 +00:00
2019-07-19 06:27:44 +00:00
this.type = type;
2019-05-14 16:34:09 +00:00
this.account = account;
this.folder = folder;
this.thread = thread;
this.id = id;
this.threading = threading;
this.filter_archive = filter_archive;
2020-04-09 15:50:29 +00:00
this.criteria = criteria;
2019-05-14 16:34:09 +00:00
this.server = server;
2022-03-19 06:38:34 +00:00
boolean outbox = EntityFolder.OUTBOX.equals(type);
2019-05-14 16:34:09 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.group_category = prefs.getBoolean("group_category", false);
2021-11-22 07:36:51 +00:00
this.sort = prefs.getString(FragmentMessages.getSort(context, viewType, type), "time");
2022-03-19 06:38:34 +00:00
this.ascending = prefs.getBoolean(FragmentMessages.getSortOrder(context, viewType, type), outbox);
2021-11-22 07:36:51 +00:00
this.filter_seen = prefs.getBoolean(FragmentMessages.getFilter(context, "seen", viewType, type), false);
this.filter_unflagged = prefs.getBoolean(FragmentMessages.getFilter(context, "unflagged", viewType, type), false);
this.filter_unknown = prefs.getBoolean(FragmentMessages.getFilter(context, "unknown", viewType, type), false);
this.filter_snoozed = prefs.getBoolean(FragmentMessages.getFilter(context, "snoozed", viewType, type), true);
2021-12-26 20:37:54 +00:00
this.filter_deleted = prefs.getBoolean(FragmentMessages.getFilter(context, "deleted", viewType, type), false);
boolean language_detection = prefs.getBoolean("language_detection", false);
2021-01-19 08:04:48 +00:00
String filter_language = prefs.getString("filter_language", null);
this.filter_language = (language_detection ? filter_language : null);
2019-05-14 16:34:09 +00:00
this.debug = prefs.getBoolean("debug", false);
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof Args) {
Args other = (Args) obj;
2019-07-19 06:27:44 +00:00
return (Objects.equals(this.type, other.type) &&
this.account == other.account &&
2019-05-14 16:34:09 +00:00
this.folder == other.folder &&
Objects.equals(this.thread, other.thread) &&
this.id == other.id &&
2020-04-09 15:50:29 +00:00
Objects.equals(this.criteria, other.criteria) &&
2019-05-14 16:34:09 +00:00
this.server == other.server &&
this.threading == other.threading &&
this.group_category == other.group_category &&
2019-05-14 16:34:09 +00:00
Objects.equals(this.sort, other.sort) &&
2019-09-03 08:33:52 +00:00
this.ascending == other.ascending &&
2019-05-14 16:34:09 +00:00
this.filter_seen == other.filter_seen &&
this.filter_unflagged == other.filter_unflagged &&
this.filter_unknown == other.filter_unknown &&
2019-05-14 16:34:09 +00:00
this.filter_snoozed == other.filter_snoozed &&
this.filter_archive == other.filter_archive &&
2021-12-26 20:37:54 +00:00
this.filter_deleted == other.filter_deleted &&
2020-03-27 09:47:49 +00:00
Objects.equals(this.filter_language, other.filter_language) &&
2019-05-14 16:34:09 +00:00
this.debug == other.debug);
} else
return false;
}
@NonNull
@Override
public String toString() {
2019-07-19 06:27:44 +00:00
return "folder=" + type + ":" + account + ":" + folder +
" thread=" + thread + ":" + id +
2020-04-09 15:50:29 +00:00
" criteria=" + criteria + ":" + server + "" +
2019-05-14 16:34:09 +00:00
" threading=" + threading +
" category=" + group_category +
" sort=" + sort + ":" + ascending +
" filter seen=" + filter_seen +
" unflagged=" + filter_unflagged +
" unknown=" + filter_unknown +
" snoozed=" + filter_snoozed +
" archive=" + filter_archive +
2020-03-27 09:47:49 +00:00
" language=" + filter_language +
2019-05-14 16:34:09 +00:00
" debug=" + debug;
}
}
private void dump() {
2021-08-30 06:25:07 +00:00
Log.i("Current models=" + TextUtils.join(", ", models.keySet()));
2019-05-14 16:34:09 +00:00
}
2019-05-15 15:05:11 +00:00
class Model {
2019-05-14 16:34:09 +00:00
private Args args;
private LiveData<PagedList<TupleMessageEx>> list;
private BoundaryCallbackMessages boundary;
Model(Args args, LiveData<PagedList<TupleMessageEx>> list, BoundaryCallbackMessages boundary) {
this.args = args;
this.list = list;
this.boundary = boundary;
}
2019-08-09 17:29:17 +00:00
void setCallback(LifecycleOwner owner, BoundaryCallbackMessages.IBoundaryCallbackMessages callback) {
if (boundary != null) {
2021-06-18 14:02:46 +00:00
BoundaryCallbackMessages.State state = boundary.setCallback(callback);
PagedList<TupleMessageEx> plist = list.getValue();
2021-08-14 12:26:37 +00:00
if (plist != null && plist.size() > 0)
plist.loadAround(0);
2019-08-09 17:29:17 +00:00
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
2022-04-27 07:58:38 +00:00
if (boundary != null) {
boundary.destroy(state);
boundary = null;
}
2021-09-28 08:18:20 +00:00
owner.getLifecycle().removeObserver(this);
2019-08-09 17:29:17 +00:00
}
});
}
2019-05-15 15:05:11 +00:00
}
void setObserver(LifecycleOwner owner, @NonNull Observer<PagedList<TupleMessageEx>> observer) {
2019-05-16 09:24:58 +00:00
//list.removeObservers(owner);
2019-05-15 15:05:11 +00:00
list.observe(owner, observer);
}
2019-05-14 16:34:09 +00:00
}
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);
2019-02-02 12:58:28 +00:00
void onFound(int position, int size);
}
}