Use restartable two state child owner

pull/153/head
M66B 7 years ago
parent 0927dd7d8f
commit dd025b1ff3

@ -117,7 +117,6 @@ import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.paging.AsyncPagedListDiffer; import androidx.paging.AsyncPagedListDiffer;
@ -258,8 +257,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private AdapterAttachment adapterAttachment; private AdapterAttachment adapterAttachment;
private AdapterImage adapterImage; private AdapterImage adapterImage;
private LiveData<List<EntityAttachment>> liveAttachments = null; private TwoStateOwner cowner = new TwoStateOwner(owner);
private Observer<List<EntityAttachment>> observerAttachments = null;
private WebView printWebView = null; private WebView printWebView = null;
@ -890,14 +888,13 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
// Attachments // Attachments
bindAttachments(message, idAttachments.get(message.id)); bindAttachments(message, idAttachments.get(message.id));
observerAttachments = new Observer<List<EntityAttachment>>() { cowner.restart();
db.attachment().liveAttachments(message.id).observe(cowner, new Observer<List<EntityAttachment>>() {
@Override @Override
public void onChanged(@Nullable List<EntityAttachment> attachments) { public void onChanged(@Nullable List<EntityAttachment> attachments) {
bindAttachments(message, attachments); bindAttachments(message, attachments);
} }
}; });
liveAttachments = db.attachment().liveAttachments(message.id);
liveAttachments.observe(owner, observerAttachments);
// Setup actions // Setup actions
Bundle sargs = new Bundle(); Bundle sargs = new Bundle();
@ -1024,8 +1021,13 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
properties.setValue("inline", message.id, isChecked); properties.setValue("inline", message.id, isChecked);
liveAttachments.removeObserver(observerAttachments); cowner.restart();
liveAttachments.observe(owner, observerAttachments); DB.getInstance(context).attachment().liveAttachments(message.id).observe(cowner, new Observer<List<EntityAttachment>>() {
@Override
public void onChanged(@Nullable List<EntityAttachment> attachments) {
bindAttachments(message, attachments);
}
});
} }
}); });
@ -1044,13 +1046,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
} }
void unbind() {
if (liveAttachments != null) {
liveAttachments.removeObserver(observerAttachments);
liveAttachments = null;
}
}
private TupleMessageEx getMessage() { private TupleMessageEx getMessage() {
int pos = getAdapterPosition(); int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION) if (pos == RecyclerView.NO_POSITION)
@ -3062,7 +3057,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unbind();
holder.unwire(); holder.unwire();
TupleMessageEx message = differ.getItem(position); TupleMessageEx message = differ.getItem(position);
@ -3074,11 +3068,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
} }
@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
holder.unbind();
}
void setSelectionTracker(SelectionTracker<Long> selectionTracker) { void setSelectionTracker(SelectionTracker<Long> selectionTracker) {
this.selectionTracker = selectionTracker; this.selectionTracker = selectionTracker;
} }

@ -2,8 +2,10 @@ package eu.faircode.email;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry; import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.OnLifecycleEvent;
public class TwoStateOwner implements LifecycleOwner { public class TwoStateOwner implements LifecycleOwner {
private LifecycleRegistry registry; private LifecycleRegistry registry;
@ -12,6 +14,16 @@ public class TwoStateOwner implements LifecycleOwner {
registry = new LifecycleRegistry(this); registry = new LifecycleRegistry(this);
} }
TwoStateOwner(LifecycleOwner owner) {
this();
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
}
});
}
void start() { void start() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_START); registry.handleLifecycleEvent(Lifecycle.Event.ON_START);
} }
@ -20,6 +32,11 @@ public class TwoStateOwner implements LifecycleOwner {
registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
} }
void restart() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
start();
}
@NonNull @NonNull
@Override @Override
public Lifecycle getLifecycle() { public Lifecycle getLifecycle() {

Loading…
Cancel
Save