Improved boundary destroy

pull/199/head
M66B 3 years ago
parent 48bcd11414
commit d788d75422

@ -111,9 +111,10 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
this.pageSize = pageSize;
}
void setCallback(IBoundaryCallbackMessages intf) {
State setCallback(IBoundaryCallbackMessages intf) {
this.intf = intf;
this.state = new State();
return this.state;
}
@Override
@ -160,8 +161,14 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
int found = 0;
try {
if (state.destroyed || state.error)
if (state.destroyed || state.error) {
Log.i("Boundary was destroyed");
return;
}
if (!Objects.equals(state, BoundaryCallbackMessages.this.state)) {
Log.i("Boundary changed state");
return;
}
if (intf != null)
ApplicationEx.getMainHandler().post(new Runnable() {
@ -631,16 +638,18 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
return imessages;
}
void destroy() {
final State old = this.state;
old.destroyed = true;
State getState() {
return this.state;
}
this.state = new State();
void destroy(State state) {
state.destroyed = true;
Log.i("Boundary destroy");
executor.submit(new Runnable() {
@Override
public void run() {
close(old, true);
close(state, true);
}
});
}
@ -666,7 +675,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
state.reset();
}
private static class State {
static class State {
int queued = 0;
boolean destroyed = false;
boolean error = false;

@ -54,7 +54,7 @@ public class ViewModelMessages extends ViewModel {
public Model put(AdapterMessage.ViewType key, Model value) {
Model existing = this.get(key);
if (existing != null && existing.boundary != null)
existing.boundary.destroy();
existing.boundary.destroy(existing.boundary.getState());
return super.put(key, value);
}
@ -63,7 +63,7 @@ public class ViewModelMessages extends ViewModel {
public Model remove(@Nullable Object key) {
Model existing = this.get(key);
if (existing != null && existing.boundary != null)
existing.boundary.destroy();
existing.boundary.destroy(existing.boundary.getState());
return super.remove(key);
}
};
@ -518,12 +518,12 @@ public class ViewModelMessages extends ViewModel {
void setCallback(LifecycleOwner owner, BoundaryCallbackMessages.IBoundaryCallbackMessages callback) {
if (boundary != null) {
boundary.setCallback(callback);
BoundaryCallbackMessages.State state = boundary.setCallback(callback);
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
boundary.destroy();
boundary.destroy(state);
}
});
}

Loading…
Cancel
Save