Added optional tasks

This commit is contained in:
M66B 2022-04-23 23:08:00 +02:00
parent 11c696e3b4
commit a45994bf7a
2 changed files with 47 additions and 24 deletions

View File

@ -49,6 +49,7 @@ import java.util.concurrent.Future;
public abstract class SimpleTask<T> implements LifecycleObserver { public abstract class SimpleTask<T> implements LifecycleObserver {
private boolean log = true; private boolean log = true;
private boolean count = true; private boolean count = true;
private boolean optional = false;
private String name; private String name;
private long started; private long started;
@ -78,6 +79,11 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
return this; return this;
} }
public SimpleTask<T> setOptional(boolean optional) {
this.optional = optional;
return this;
}
public SimpleTask<T> setExecutor(ExecutorService executor) { public SimpleTask<T> setExecutor(ExecutorService executor) {
this.localExecutor = executor; this.localExecutor = executor;
return this; return this;
@ -144,6 +150,15 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
} }
LifecycleObserver watcher = new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
EntityLog.log(context, EntityLog.Type.Debug, "Cancelling task=" + name);
cancel(context);
owner.getLifecycle().removeObserver(this);
}
};
future = getExecutor(context).submit(new Runnable() { future = getExecutor(context).submit(new Runnable() {
private Object data; private Object data;
private long elapsed; private long elapsed;
@ -181,30 +196,35 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
if (state.equals(Lifecycle.State.DESTROYED)) { if (state.equals(Lifecycle.State.DESTROYED)) {
// No delivery // No delivery
cleanup(context); cleanup(context);
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
// Inline delivery
Log.i("Deliver task " + name + " state=" + state + " elapse=" + elapsed + " ms");
deliver();
cleanup(context);
} else { } else {
Log.i("Deferring task " + name + " state=" + state); if (optional)
owner.getLifecycle().addObserver(new LifecycleObserver() { owner.getLifecycle().removeObserver(watcher);
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() { if (state.isAtLeast(Lifecycle.State.RESUMED)) {
state = owner.getLifecycle().getCurrentState(); // Inline delivery
if (state.equals(Lifecycle.State.DESTROYED)) { Log.i("Deliver task " + name + " state=" + state + " elapse=" + elapsed + " ms");
Log.i("Destroyed task " + name); deliver();
owner.getLifecycle().removeObserver(this); cleanup(context);
cleanup(context); } else {
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) { Log.i("Deferring task " + name + " state=" + state);
Log.i("Deferred delivery task " + name); owner.getLifecycle().addObserver(new LifecycleObserver() {
owner.getLifecycle().removeObserver(this); @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
deliver(); public void onAny() {
cleanup(context); state = owner.getLifecycle().getCurrentState();
} else if (state.equals(Lifecycle.State.DESTROYED)) {
Log.i("Deferring task " + name + " state=" + state); Log.i("Destroyed task " + name);
} owner.getLifecycle().removeObserver(this);
}); cleanup(context);
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
Log.i("Deferred delivery task " + name);
owner.getLifecycle().removeObserver(this);
deliver();
cleanup(context);
} else
Log.i("Deferring task " + name + " state=" + state);
}
});
}
} }
} }
@ -249,6 +269,9 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
}); });
if (optional)
owner.getLifecycle().addObserver(watcher);
updateTaskCount(context); updateTaskCount(context);
} }

View File

@ -431,7 +431,7 @@ public class ViewModelMessages extends ViewModel {
Log.i("Observe previous/next fallback=" + result); Log.i("Observe previous/next fallback=" + result);
return result; return result;
} }
}.execute(context, owner, args, "model:fallback"); }.setOptional(true).execute(context, owner, args, "model:fallback");
} }
}); });
} }