From 6c5dd6fb1e118bd8502ac9f1d12247cfc07b045a Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 4 Feb 2019 12:17:42 +0000 Subject: [PATCH] Bind cached attachments, refactoring --- .../eu/faircode/email/AdapterMessage.java | 134 +++++++++--------- 1 file changed, 68 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 4dfa30e83c..421e3e80b3 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -443,7 +443,6 @@ public class AdapterMessage extends RecyclerView.Adapter 0 ? View.VISIBLE : View.GONE); @@ -782,69 +786,11 @@ public class AdapterMessage extends RecyclerView.Adapter attachments = idAttachments.get(message.id); - if (attachments != null) - adapterAttachment.set(attachments); - - // Observe attachments + bindAttachments(message, idAttachments.get(message.id)); observerAttachments = new Observer>() { @Override public void onChanged(@Nullable List attachments) { - if (attachments == null) - attachments = new ArrayList<>(); - idAttachments.put(message.id, attachments); - - boolean show_inline = properties.getValue("inline", message.id); - Log.i("Show inline=" + show_inline); - - boolean inline = false; - boolean download = false; - boolean save = (attachments.size() > 1); - boolean downloading = false; - List a = new ArrayList<>(); - for (EntityAttachment attachment : attachments) { - if (attachment.isInline()) - inline = true; - if (attachment.progress == null && !attachment.available) - download = true; - if (!attachment.available) - save = false; - if (attachment.progress != null) - downloading = true; - if (show_inline || !attachment.isInline()) - a.add(attachment); - } - adapterAttachment.set(a); - - cbInline.setOnCheckedChangeListener(null); - cbInline.setChecked(show_inline); - cbInline.setVisibility(inline ? View.VISIBLE : View.GONE); - btnDownloadAttachments.setVisibility(download && internet ? View.VISIBLE : View.GONE); - btnSaveAttachments.setVisibility(save ? View.VISIBLE : View.GONE); - tvNoInternetAttachments.setVisibility(downloading && !internet ? View.VISIBLE : View.GONE); - - cbInline.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - properties.setValue("inline", message.id, isChecked); - liveAttachments.removeObserver(observerAttachments); - liveAttachments.observe(owner, observerAttachments); - } - }); - - List images = new ArrayList<>(); - for (EntityAttachment attachment : attachments) - if (attachment.type.startsWith("image/") && !attachment.isInline()) - images.add(attachment); - adapterImage.set(images); - rvImage.setVisibility(images.size() > 0 ? View.VISIBLE : View.GONE); - - boolean show_html = properties.getValue("html", message.id); - if (message.content && !show_html) { - Bundle args = new Bundle(); - args.putSerializable("message", message); - bodyTask.execute(context, owner, args, "message:body"); - } + bindAttachments(message, attachments); } }; liveAttachments = db.attachment().liveAttachments(message.id); @@ -918,10 +864,8 @@ public class AdapterMessage extends RecyclerView.Adapter attachments) { + if (attachments == null) + attachments = new ArrayList<>(); + idAttachments.put(message.id, attachments); + + boolean show_inline = properties.getValue("inline", message.id); + Log.i("Show inline=" + show_inline); + + boolean inline = false; + boolean download = false; + boolean save = (attachments.size() > 1); + boolean downloading = false; + List a = new ArrayList<>(); + for (EntityAttachment attachment : attachments) { + if (attachment.isInline()) + inline = true; + if (attachment.progress == null && !attachment.available) + download = true; + if (!attachment.available) + save = false; + if (attachment.progress != null) + downloading = true; + if (show_inline || !attachment.isInline()) + a.add(attachment); + } + adapterAttachment.set(a); + + cbInline.setOnCheckedChangeListener(null); + cbInline.setChecked(show_inline); + cbInline.setVisibility(inline ? View.VISIBLE : View.GONE); + btnDownloadAttachments.setVisibility(download && internet ? View.VISIBLE : View.GONE); + btnSaveAttachments.setVisibility(save ? View.VISIBLE : View.GONE); + tvNoInternetAttachments.setVisibility(downloading && !internet ? View.VISIBLE : View.GONE); + + cbInline.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + properties.setValue("inline", message.id, isChecked); + liveAttachments.removeObserver(observerAttachments); + liveAttachments.observe(owner, observerAttachments); + } + }); + + List images = new ArrayList<>(); + for (EntityAttachment attachment : attachments) + if (attachment.type.startsWith("image/") && !attachment.isInline()) + images.add(attachment); + adapterImage.set(images); + rvImage.setVisibility(images.size() > 0 ? View.VISIBLE : View.GONE); + + boolean show_html = properties.getValue("html", message.id); + if (message.content && !show_html) { + Bundle args = new Bundle(); + args.putSerializable("message", message); + bodyTask.execute(context, owner, args, "message:body"); + } + } + void unbind() { if (liveAttachments != null) { liveAttachments.removeObserver(observerAttachments);