Browse Source

Delay task result delivery

main
M66B 6 years ago
parent
commit
8a1d82c4bb
1 changed files with 33 additions and 8 deletions
  1. +33
    -8
      app/src/main/java/eu/faircode/email/SimpleTask.java

+ 33
- 8
app/src/main/java/eu/faircode/email/SimpleTask.java View File

@ -38,7 +38,9 @@ import androidx.lifecycle.OnLifecycleEvent;
//
public abstract class SimpleTask<T> implements LifecycleObserver {
private boolean alive = true;
private boolean paused = false;
private Bundle args = null;
private Result stored = null;
public void load(Context context, LifecycleOwner owner, Bundle args) {
run(context, owner, args);
@ -56,9 +58,25 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
run(fragment.getContext(), fragment.getViewLifecycleOwner(), args);
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
paused = false;
if (stored != null) {
deliver(args, stored);
stored = null;
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
paused = true;
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
alive = false;
paused = true;
args = null;
stored = null;
}
private void run(final Context context, LifecycleOwner owner, final Bundle args) {
@ -79,16 +97,23 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
@Override
protected void onPostExecute(Result result) {
if (alive) {
if (result.ex == null)
onLoaded(args, (T) result.data);
else
onException(args, result.ex);
}
deliver(args, result);
}
}.execute(args);
}
private void deliver(Bundle args, Result result) {
if (paused) {
this.args = args;
this.stored = result;
} else {
if (result.ex == null)
onLoaded(args, (T) result.data);
else
onException(args, result.ex);
}
}
protected T onLoad(Context context, Bundle args) throws Throwable {
// Be careful not to access members in outer scopes
return null;


Loading…
Cancel
Save