Improved two state lifecycles

This commit is contained in:
M66B 2019-04-13 08:02:01 +02:00
parent 722012609b
commit f93eb61b04
3 changed files with 31 additions and 16 deletions

View File

@ -291,7 +291,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
tvError.setVisibility(folder.error != null ? View.VISIBLE : View.GONE); tvError.setVisibility(folder.error != null ? View.VISIBLE : View.GONE);
childs.setShowHidden(show_hidden); childs.setShowHidden(show_hidden);
cowner.restart(); cowner.recreate();
if (account > 0 && folder.childs > 0 && !folder.collapsed) { if (account > 0 && folder.childs > 0 && !folder.collapsed) {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
cowner.start(); cowner.start();
@ -737,6 +737,11 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
return new ViewHolder(inflater.inflate(R.layout.item_folder, parent, false)); return new ViewHolder(inflater.inflate(R.layout.item_folder, parent, false));
} }
@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
holder.cowner.stop();
}
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire(); holder.unwire();

View File

@ -654,6 +654,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
private void clearExpanded() { private void clearExpanded() {
cowner.stop();
grpHeaders.setVisibility(View.GONE); grpHeaders.setVisibility(View.GONE);
grpAttachments.setVisibility(View.GONE); grpAttachments.setVisibility(View.GONE);
grpExpanded.setVisibility(View.GONE); grpExpanded.setVisibility(View.GONE);
@ -848,7 +850,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
// Attachments // Attachments
bindAttachments(message, properties.getAttachments(message.id)); bindAttachments(message, properties.getAttachments(message.id));
cowner.restart(); cowner.recreate();
cowner.start();
db.attachment().liveAttachments(message.id).observe(cowner, new Observer<List<EntityAttachment>>() { 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) {
@ -3063,7 +3066,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
@Override @Override
public void onViewRecycled(@NonNull ViewHolder holder) { public void onViewRecycled(@NonNull ViewHolder holder) {
holder.clearExpanded(); holder.cowner.stop();
} }
void setSelectionTracker(SelectionTracker<Long> selectionTracker) { void setSelectionTracker(SelectionTracker<Long> selectionTracker) {

View File

@ -15,19 +15,7 @@ public class TwoStateOwner implements LifecycleOwner {
TwoStateOwner(String aname) { TwoStateOwner(String aname) {
name = aname; name = aname;
create();
// Initialize
registry = new LifecycleRegistry(this);
registry.markState(Lifecycle.State.CREATED);
// Logging
registry.addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
if (BuildConfig.DEBUG)
Log.i("LifeCycle " + name + " state=" + registry.getCurrentState());
}
});
} }
TwoStateOwner(LifecycleOwner owner, String aname) { TwoStateOwner(LifecycleOwner owner, String aname) {
@ -44,6 +32,20 @@ public class TwoStateOwner implements LifecycleOwner {
}); });
} }
private void create() {
// Initialize
registry = new LifecycleRegistry(this);
registry.addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
if (BuildConfig.DEBUG)
Log.i("LifeCycle " + name + " state=" + registry.getCurrentState() + " " + registry);
}
});
registry.markState(Lifecycle.State.CREATED);
}
void start() { void start() {
registry.markState(Lifecycle.State.STARTED); registry.markState(Lifecycle.State.STARTED);
} }
@ -57,6 +59,11 @@ public class TwoStateOwner implements LifecycleOwner {
start(); start();
} }
void recreate() {
destroy();
create();
}
void destroy() { void destroy() {
Lifecycle.State state = registry.getCurrentState(); Lifecycle.State state = registry.getCurrentState();
if (!state.equals(Lifecycle.State.CREATED)) if (!state.equals(Lifecycle.State.CREATED))