Refactoring

pull/214/head
M66B 6 months ago
parent 99464f7a8c
commit 9e352840a2

@ -6372,11 +6372,13 @@ public class FragmentCompose extends FragmentBase {
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(getContext());
Map<Long, EntityAttachment> map = new HashMap<>();
if (last_attachments != null)
for (EntityAttachment attachment : last_attachments)
map.put(attachment.id, attachment);
db.attachment().liveAttachments(data.draft.id).observe(getViewLifecycleOwner(), db.attachment().liveAttachments(data.draft.id).observe(getViewLifecycleOwner(),
new Observer<List<EntityAttachment>>() { new Observer<List<EntityAttachment>>() {
private Integer last_count = null;
private Integer last_available = null;
@Override @Override
public void onChanged(@Nullable List<EntityAttachment> attachments) { public void onChanged(@Nullable List<EntityAttachment> attachments) {
if (attachments == null) if (attachments == null)
@ -6416,46 +6418,35 @@ public class FragmentCompose extends FragmentBase {
ibRemoveAttachments.setVisibility(attachments.size() > 2 ? View.VISIBLE : View.GONE); ibRemoveAttachments.setVisibility(attachments.size() > 2 ? View.VISIBLE : View.GONE);
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE); grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
int available = 0;
boolean downloading = false; boolean downloading = false;
for (EntityAttachment attachment : attachments) { for (EntityAttachment attachment : attachments) {
if (attachment.isEncryption()) if (attachment.isEncryption())
continue; continue;
if (attachment.progress != null) if (attachment.progress != null)
downloading = true; downloading = true;
if (attachment.available)
available++;
} }
Log.i("Attachments=" + last_count + "/" + attachments.size() +
" downloading=" + downloading +
" available=" + last_available + "/" + available);
rvAttachment.setTag(downloading); rvAttachment.setTag(downloading);
checkInternet(); checkInternet();
if ((last_count != null && last_count > attachments.size()) || boolean updated = false;
(last_available != null && last_available < available)) { Editable edit = etBody.getEditableText();
boolean updated = false; ImageSpan[] spans = edit.getSpans(0, edit.length(), ImageSpan.class);
Editable edit = etBody.getEditableText(); if (spans == null)
spans = new ImageSpan[0];
ImageSpan[] spans = edit.getSpans(0, edit.length(), ImageSpan.class); for (EntityAttachment attachment : attachments) {
for (int i = 0; i < spans.length; i++) { EntityAttachment prev = map.get(attachment.id);
ImageSpan span = spans[i]; if (prev == null) // New attachment
String source = span.getSource(); continue;
if (source != null && source.startsWith("cid:")) { map.remove(attachment.id);
String cid = "<" + source.substring(4) + ">";
boolean found = false;
boolean downloaded = false;
for (EntityAttachment attachment : attachments)
if (cid.equals(attachment.cid)) {
found = true;
downloaded = attachment.available;
break;
}
if (found) { if (!prev.available && attachment.available) // Attachment downloaded
if (downloaded) { for (ImageSpan span : spans) {
String source = span.getSource();
if (source != null && source.startsWith("cid:")) {
String cid = "<" + source.substring(4) + ">";
if (cid.equals(attachment.cid)) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", working); args.putLong("id", working);
args.putString("source", source); args.putString("source", source);
@ -6475,8 +6466,7 @@ public class FragmentCompose extends FragmentBase {
String source = args.getString("source"); String source = args.getString("source");
Editable edit = etBody.getEditableText(); Editable edit = etBody.getEditableText();
ImageSpan[] spans = edit.getSpans(0, edit.length(), ImageSpan.class); ImageSpan[] spans = edit.getSpans(0, edit.length(), ImageSpan.class);
for (int i = 0; i < spans.length; i++) { for (ImageSpan span : spans)
ImageSpan span = spans[i];
if (source != null && source.equals(span.getSource())) { if (source != null && source.equals(span.getSource())) {
int start = edit.getSpanStart(span); int start = edit.getSpanStart(span);
int end = edit.getSpanEnd(span); int end = edit.getSpanEnd(span);
@ -6488,7 +6478,6 @@ public class FragmentCompose extends FragmentBase {
etBody.setText(edit); etBody.setText(edit);
break; break;
} }
}
} }
@Override @Override
@ -6496,23 +6485,35 @@ public class FragmentCompose extends FragmentBase {
// Ignored // Ignored
} }
}.execute(FragmentCompose.this, args, "attachment:downloaded"); }.execute(FragmentCompose.this, args, "attachment:downloaded");
break;
} }
} else { }
}
}
for (EntityAttachment removed : map.values())
for (ImageSpan span : spans) {
String source = span.getSource();
if (source != null && source.startsWith("cid:")) {
String cid = "<" + source.substring(4) + ">";
if (cid.equals(removed.cid)) {
updated = true; updated = true;
int start = edit.getSpanStart(span); int start = edit.getSpanStart(span);
int end = edit.getSpanEnd(span); int end = edit.getSpanEnd(span);
edit.removeSpan(span); edit.removeSpan(span);
edit.delete(start, end); edit.delete(start, end);
break;
} }
} }
} }
if (updated) if (updated)
etBody.setText(edit); etBody.setText(edit);
}
last_count = attachments.size(); map.clear();
last_available = available; for (EntityAttachment attachment : attachments)
map.put(attachment.id, attachment);
} }
}); });

Loading…
Cancel
Save