mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-24 15:11:03 +00:00
Use restartable two state child owner
This commit is contained in:
parent
0927dd7d8f
commit
dd025b1ff3
2 changed files with 28 additions and 22 deletions
|
@ -117,7 +117,6 @@ import androidx.appcompat.widget.PopupMenu;
|
|||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import androidx.constraintlayout.widget.Group;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
import androidx.paging.AsyncPagedListDiffer;
|
||||
|
@ -258,8 +257,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
|
||||
private AdapterAttachment adapterAttachment;
|
||||
private AdapterImage adapterImage;
|
||||
private LiveData<List<EntityAttachment>> liveAttachments = null;
|
||||
private Observer<List<EntityAttachment>> observerAttachments = null;
|
||||
private TwoStateOwner cowner = new TwoStateOwner(owner);
|
||||
|
||||
private WebView printWebView = null;
|
||||
|
||||
|
@ -890,14 +888,13 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
|
||||
// Attachments
|
||||
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
|
||||
public void onChanged(@Nullable List<EntityAttachment> attachments) {
|
||||
bindAttachments(message, attachments);
|
||||
}
|
||||
};
|
||||
liveAttachments = db.attachment().liveAttachments(message.id);
|
||||
liveAttachments.observe(owner, observerAttachments);
|
||||
});
|
||||
|
||||
// Setup actions
|
||||
Bundle sargs = new Bundle();
|
||||
|
@ -1024,8 +1021,13 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
properties.setValue("inline", message.id, isChecked);
|
||||
liveAttachments.removeObserver(observerAttachments);
|
||||
liveAttachments.observe(owner, observerAttachments);
|
||||
cowner.restart();
|
||||
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() {
|
||||
int pos = getAdapterPosition();
|
||||
if (pos == RecyclerView.NO_POSITION)
|
||||
|
@ -3062,7 +3057,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
holder.unbind();
|
||||
holder.unwire();
|
||||
|
||||
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) {
|
||||
this.selectionTracker = selectionTracker;
|
||||
}
|
||||
|
|
|
@ -2,8 +2,10 @@ package eu.faircode.email;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LifecycleRegistry;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
|
||||
public class TwoStateOwner implements LifecycleOwner {
|
||||
private LifecycleRegistry registry;
|
||||
|
@ -12,6 +14,16 @@ public class TwoStateOwner implements LifecycleOwner {
|
|||
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() {
|
||||
registry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
}
|
||||
|
@ -20,6 +32,11 @@ public class TwoStateOwner implements LifecycleOwner {
|
|||
registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
}
|
||||
|
||||
void restart() {
|
||||
registry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
|
||||
start();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Lifecycle getLifecycle() {
|
||||
|
|
Loading…
Reference in a new issue