Added fail-safe

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

@ -6429,91 +6429,95 @@ public class FragmentCompose extends FragmentBase {
rvAttachment.setTag(downloading); rvAttachment.setTag(downloading);
checkInternet(); checkInternet();
boolean updated = false; try {
Editable edit = etBody.getEditableText(); boolean updated = false;
ImageSpan[] spans = edit.getSpans(0, edit.length(), ImageSpan.class); Editable edit = etBody.getEditableText();
if (spans == null) ImageSpan[] spans = edit.getSpans(0, edit.length(), ImageSpan.class);
spans = new ImageSpan[0]; if (spans == null)
spans = new ImageSpan[0];
for (EntityAttachment attachment : attachments) {
EntityAttachment prev = map.get(attachment.id); for (EntityAttachment attachment : attachments) {
if (prev == null) // New attachment EntityAttachment prev = map.get(attachment.id);
continue; if (prev == null) // New attachment
map.remove(attachment.id); continue;
map.remove(attachment.id);
if (!prev.available && attachment.available) // Attachment 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();
args.putLong("id", working);
args.putString("source", source);
args.putInt("zoom", zoom);
new SimpleTask<Drawable>() {
@Override
protected Drawable onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String source = args.getString("source");
int zoom = args.getInt("zoom");
return ImageHelper.decodeImage(context, id, source, true, zoom, 1.0f, etBody);
}
@Override
protected void onExecuted(Bundle args, Drawable d) {
String source = args.getString("source");
Editable edit = etBody.getEditableText();
ImageSpan[] spans = edit.getSpans(0, edit.length(), ImageSpan.class);
for (ImageSpan span : spans)
if (source != null && source.equals(span.getSource())) {
int start = edit.getSpanStart(span);
int end = edit.getSpanEnd(span);
edit.removeSpan(span);
if (d == null)
edit.delete(start, end);
else
edit.setSpan(new ImageSpan(d, source), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(edit);
break;
}
}
@Override
protected void onException(Bundle args, Throwable ex) {
// Ignored
}
}.execute(FragmentCompose.this, args, "attachment:downloaded");
break;
}
}
}
}
if (!prev.available && attachment.available) // Attachment downloaded for (EntityAttachment removed : map.values())
for (ImageSpan span : spans) { for (ImageSpan span : spans) {
String source = span.getSource(); String source = span.getSource();
if (source != null && source.startsWith("cid:")) { if (source != null && source.startsWith("cid:")) {
String cid = "<" + source.substring(4) + ">"; String cid = "<" + source.substring(4) + ">";
if (cid.equals(attachment.cid)) { if (cid.equals(removed.cid)) {
Bundle args = new Bundle(); updated = true;
args.putLong("id", working); int start = edit.getSpanStart(span);
args.putString("source", source); int end = edit.getSpanEnd(span);
args.putInt("zoom", zoom); edit.removeSpan(span);
edit.delete(start, end);
new SimpleTask<Drawable>() {
@Override
protected Drawable onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String source = args.getString("source");
int zoom = args.getInt("zoom");
return ImageHelper.decodeImage(context, id, source, true, zoom, 1.0f, etBody);
}
@Override
protected void onExecuted(Bundle args, Drawable d) {
String source = args.getString("source");
Editable edit = etBody.getEditableText();
ImageSpan[] spans = edit.getSpans(0, edit.length(), ImageSpan.class);
for (ImageSpan span : spans)
if (source != null && source.equals(span.getSource())) {
int start = edit.getSpanStart(span);
int end = edit.getSpanEnd(span);
edit.removeSpan(span);
if (d == null)
edit.delete(start, end);
else
edit.setSpan(new ImageSpan(d, source), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
etBody.setText(edit);
break;
}
}
@Override
protected void onException(Bundle args, Throwable ex) {
// Ignored
}
}.execute(FragmentCompose.this, args, "attachment:downloaded");
break; break;
} }
} }
} }
}
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;
int start = edit.getSpanStart(span);
int end = edit.getSpanEnd(span);
edit.removeSpan(span);
edit.delete(start, end);
break;
}
}
}
if (updated) if (updated)
etBody.setText(edit); etBody.setText(edit);
map.clear(); map.clear();
for (EntityAttachment attachment : attachments) for (EntityAttachment attachment : attachments)
map.put(attachment.id, attachment); map.put(attachment.id, attachment);
} catch (Throwable ex) {
Log.e(ex);
}
} }
}); });

Loading…
Cancel
Save