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

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

Loading…
Cancel
Save