2018-09-02 06:59:49 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2018-09-04 07:02:54 +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-09-04 07:02:54 +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-09-04 07:02:54 +00:00
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-09-04 07:02:54 +00:00
|
|
|
*/
|
|
|
|
|
2018-09-02 06:59:49 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
|
2018-09-02 08:55:06 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
2018-09-02 06:59:49 +00:00
|
|
|
|
|
|
|
import androidx.lifecycle.GenericLifecycleObserver;
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
import androidx.paging.PagedList;
|
|
|
|
|
|
|
|
public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMessageEx> {
|
2018-10-20 16:05:43 +00:00
|
|
|
private ViewModelBrowse model;
|
2018-09-23 04:28:13 +00:00
|
|
|
private Handler handler;
|
2018-09-02 06:59:49 +00:00
|
|
|
private IBoundaryCallbackMessages intf;
|
2018-10-21 06:39:38 +00:00
|
|
|
private boolean searching = false;
|
2018-10-20 17:56:09 +00:00
|
|
|
|
2018-11-26 17:04:44 +00:00
|
|
|
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
2018-09-02 06:59:49 +00:00
|
|
|
|
|
|
|
interface IBoundaryCallbackMessages {
|
|
|
|
void onLoading();
|
|
|
|
|
|
|
|
void onLoaded();
|
|
|
|
|
2018-11-06 07:30:41 +00:00
|
|
|
void onError(Throwable ex);
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-20 16:05:43 +00:00
|
|
|
BoundaryCallbackMessages(LifecycleOwner owner, ViewModelBrowse _model, IBoundaryCallbackMessages intf) {
|
|
|
|
this.model = _model;
|
2018-09-23 04:28:13 +00:00
|
|
|
this.handler = new Handler();
|
2018-09-02 06:59:49 +00:00
|
|
|
this.intf = intf;
|
|
|
|
|
|
|
|
owner.getLifecycle().addObserver(new GenericLifecycleObserver() {
|
|
|
|
@Override
|
|
|
|
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
|
2018-10-20 16:05:43 +00:00
|
|
|
if (event == Lifecycle.Event.ON_DESTROY)
|
2018-09-12 10:15:11 +00:00
|
|
|
executor.submit(new Runnable() {
|
2018-09-02 06:59:49 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-10-20 16:05:43 +00:00
|
|
|
model.clear();
|
2018-11-04 10:26:55 +00:00
|
|
|
model = null;
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
2018-09-12 10:15:11 +00:00
|
|
|
});
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
boolean isSearching() {
|
2018-10-21 06:39:38 +00:00
|
|
|
return searching;
|
2018-09-21 11:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onZeroItemsLoaded() {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("onZeroItemsLoaded");
|
2018-09-21 11:53:54 +00:00
|
|
|
load();
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemAtEndLoaded(final TupleMessageEx itemAtEnd) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("onItemAtEndLoaded");
|
2018-09-21 11:53:54 +00:00
|
|
|
load();
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
private void load() {
|
2018-11-04 10:26:55 +00:00
|
|
|
if (model != null)
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2018-12-26 09:14:53 +00:00
|
|
|
searching = model.isSearching();
|
2018-11-04 10:26:55 +00:00
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
intf.onLoading();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
model.load();
|
|
|
|
} catch (final Throwable ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e("Boundary", ex);
|
2018-11-04 10:26:55 +00:00
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-11-06 07:30:41 +00:00
|
|
|
intf.onError(ex);
|
2018-11-04 10:26:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
searching = false;
|
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
intf.onLoaded();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
2018-11-04 10:26:55 +00:00
|
|
|
});
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
}
|