Move discarded drafts to trash

pull/147/head
M66B 6 years ago
parent ec793d30f6
commit 1b295482fe

@ -1260,6 +1260,24 @@ public class FragmentCompose extends FragmentEx {
.show(); .show();
} }
private boolean isEmpty() {
if (!TextUtils.isEmpty(etExtra.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(etTo.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(etCc.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(etBcc.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(etSubject.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(Jsoup.parse(Html.toHtml(etBody.getText())).text().trim()))
return false;
if (rvAttachment.getAdapter().getItemCount() > 0)
return false;
return true;
}
private void onAction(int action) { private void onAction(int action) {
EntityIdentity identity = (EntityIdentity) spIdentity.getSelectedItem(); EntityIdentity identity = (EntityIdentity) spIdentity.getSelectedItem();
@ -1268,12 +1286,14 @@ public class FragmentCompose extends FragmentEx {
args.putInt("action", action); args.putInt("action", action);
args.putLong("account", identity == null ? -1 : identity.account); args.putLong("account", identity == null ? -1 : identity.account);
args.putLong("identity", identity == null ? -1 : identity.id); args.putLong("identity", identity == null ? -1 : identity.id);
args.putString("extra", etExtra.getText().toString()); args.putString("extra", etExtra.getText().toString().trim());
args.putString("to", etTo.getText().toString()); args.putString("to", etTo.getText().toString().trim());
args.putString("cc", etCc.getText().toString()); args.putString("cc", etCc.getText().toString().trim());
args.putString("bcc", etBcc.getText().toString()); args.putString("bcc", etBcc.getText().toString().trim());
args.putString("subject", etSubject.getText().toString()); args.putString("subject", etSubject.getText().toString().trim());
args.putInt("attachments", rvAttachment.getAdapter().getItemCount());
// Workaround underlines left by Android
Spannable spannable = etBody.getText(); Spannable spannable = etBody.getText();
UnderlineSpan[] uspans = spannable.getSpans(0, spannable.length(), UnderlineSpan.class); UnderlineSpan[] uspans = spannable.getSpans(0, spannable.length(), UnderlineSpan.class);
for (UnderlineSpan uspan : uspans) for (UnderlineSpan uspan : uspans)
@ -1281,7 +1301,6 @@ public class FragmentCompose extends FragmentEx {
args.putString("body", Html.toHtml(spannable)); args.putString("body", Html.toHtml(spannable));
args.putBoolean("empty", isEmpty());
args.putBoolean("dirty", dirty); args.putBoolean("dirty", dirty);
dirty = false; dirty = false;
@ -1289,24 +1308,6 @@ public class FragmentCompose extends FragmentEx {
actionLoader.execute(this, args, "compose:action:" + action); actionLoader.execute(this, args, "compose:action:" + action);
} }
private boolean isEmpty() {
if (!TextUtils.isEmpty(etExtra.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(etTo.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(etCc.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(etBcc.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(etSubject.getText().toString().trim()))
return false;
if (!TextUtils.isEmpty(Jsoup.parse(Html.toHtml(etBody.getText())).text().trim()))
return false;
if (rvAttachment.getAdapter().getItemCount() > 0)
return false;
return true;
}
private static EntityAttachment addAttachment(Context context, long id, Uri uri, private static EntityAttachment addAttachment(Context context, long id, Uri uri,
boolean image) throws IOException { boolean image) throws IOException {
if ("file".equals(uri.getScheme())) { if ("file".equals(uri.getScheme())) {
@ -1973,10 +1974,17 @@ public class FragmentCompose extends FragmentEx {
String cc = args.getString("cc"); String cc = args.getString("cc");
String bcc = args.getString("bcc"); String bcc = args.getString("bcc");
String subject = args.getString("subject"); String subject = args.getString("subject");
int acount = args.getInt("attachments");
String body = args.getString("body"); String body = args.getString("body");
boolean empty = args.getBoolean("empty");
boolean dirty = args.getBoolean("dirty"); boolean dirty = args.getBoolean("dirty");
boolean empty = TextUtils.isEmpty(extra) &&
TextUtils.isEmpty(to) &&
TextUtils.isEmpty(cc) &&
TextUtils.isEmpty(bcc) &&
TextUtils.isEmpty(subject) &&
acount == 0 &&
TextUtils.isEmpty(Jsoup.parse(body).text().trim());
EntityMessage draft; EntityMessage draft;
@ -1994,99 +2002,103 @@ public class FragmentCompose extends FragmentEx {
Log.i("Load action id=" + draft.id + " action=" + action); Log.i("Load action id=" + draft.id + " action=" + action);
if (action != R.id.action_delete) { // Move draft to new account
// Move draft to new account if (draft.account != aid && aid >= 0) {
if (draft.account != aid && aid >= 0) { Long uid = draft.uid;
Long uid = draft.uid; String msgid = draft.msgid;
String msgid = draft.msgid;
draft.uid = null;
draft.msgid = null;
db.message().updateMessage(draft);
draft.id = null;
draft.uid = uid;
draft.msgid = msgid;
draft.content = false;
draft.ui_hide = true;
draft.id = db.message().insertMessage(draft);
EntityOperation.queue(context, db, draft, EntityOperation.DELETE);
draft.id = id; draft.uid = null;
draft.account = aid; draft.msgid = null;
draft.folder = db.folder().getFolderByType(aid, EntityFolder.DRAFTS).id; db.message().updateMessage(draft);
draft.content = true;
draft.ui_hide = false;
EntityOperation.queue(context, db, draft, EntityOperation.ADD);
}
// Get data draft.id = null;
InternetAddress afrom[] = (identity == null ? null : new InternetAddress[]{new InternetAddress(identity.email, identity.name)}); draft.uid = uid;
draft.msgid = msgid;
draft.content = false;
draft.ui_hide = true;
draft.id = db.message().insertMessage(draft);
EntityOperation.queue(context, db, draft, EntityOperation.DELETE);
InternetAddress ato[] = null; draft.id = id;
InternetAddress acc[] = null; draft.account = aid;
InternetAddress abcc[] = null; draft.folder = db.folder().getFolderByType(aid, EntityFolder.DRAFTS).id;
draft.content = true;
draft.ui_hide = false;
EntityOperation.queue(context, db, draft, EntityOperation.ADD);
}
if (!TextUtils.isEmpty(to)) // Get data
try { InternetAddress afrom[] = (identity == null ? null : new InternetAddress[]{new InternetAddress(identity.email, identity.name)});
ato = InternetAddress.parse(to);
} catch (AddressException ignored) {
}
if (!TextUtils.isEmpty(cc)) InternetAddress ato[] = null;
try { InternetAddress acc[] = null;
acc = InternetAddress.parse(cc); InternetAddress abcc[] = null;
} catch (AddressException ignored) {
}
if (!TextUtils.isEmpty(bcc)) if (!TextUtils.isEmpty(to))
try { try {
abcc = InternetAddress.parse(bcc); ato = InternetAddress.parse(to);
} catch (AddressException ignored) { } catch (AddressException ignored) {
} }
if (TextUtils.isEmpty(extra)) if (!TextUtils.isEmpty(cc))
extra = null; try {
acc = InternetAddress.parse(cc);
Long ident = (identity == null ? null : identity.id); } catch (AddressException ignored) {
dirty = dirty || }
((draft.identity == null ? ident != null : !draft.identity.equals(ident)) ||
(draft.extra == null ? extra != null : !draft.extra.equals(extra)) ||
!MessageHelper.equal(draft.from, afrom) ||
!MessageHelper.equal(draft.to, ato) ||
!MessageHelper.equal(draft.cc, acc) ||
!MessageHelper.equal(draft.bcc, abcc) ||
(draft.subject == null ? subject != null : !draft.subject.equals(subject)));
// Update draft
draft.identity = ident;
draft.extra = extra;
draft.sender = MessageHelper.getSortKey(afrom);
draft.from = afrom;
draft.to = ato;
draft.cc = acc;
draft.bcc = abcc;
draft.subject = subject;
draft.received = new Date().getTime();
if (action == R.id.action_send)
if (draft.replying != null || draft.forwarding != null)
body += HtmlHelper.getQuote(context,
draft.replying == null ? draft.forwarding : draft.replying, false);
dirty = (dirty || !body.equals(draft.read(context)));
if (dirty) { if (!TextUtils.isEmpty(bcc))
db.message().updateMessage(draft); try {
draft.write(context, body); abcc = InternetAddress.parse(bcc);
db.message().setMessageContent( } catch (AddressException ignored) {
draft.id, true, HtmlHelper.getPreview(body));
} }
if (TextUtils.isEmpty(extra))
extra = null;
Long ident = (identity == null ? null : identity.id);
dirty = dirty ||
((draft.identity == null ? ident != null : !draft.identity.equals(ident)) ||
(draft.extra == null ? extra != null : !draft.extra.equals(extra)) ||
!MessageHelper.equal(draft.from, afrom) ||
!MessageHelper.equal(draft.to, ato) ||
!MessageHelper.equal(draft.cc, acc) ||
!MessageHelper.equal(draft.bcc, abcc) ||
(draft.subject == null ? subject != null : !draft.subject.equals(subject)));
// Update draft
draft.identity = ident;
draft.extra = extra;
draft.sender = MessageHelper.getSortKey(afrom);
draft.from = afrom;
draft.to = ato;
draft.cc = acc;
draft.bcc = abcc;
draft.subject = subject;
draft.received = new Date().getTime();
if (action == R.id.action_send)
if (draft.replying != null || draft.forwarding != null)
body += HtmlHelper.getQuote(context,
draft.replying == null ? draft.forwarding : draft.replying, false);
dirty = (dirty || !body.equals(draft.read(context)));
if (dirty) {
db.message().updateMessage(draft);
draft.write(context, body);
db.message().setMessageContent(
draft.id, true, HtmlHelper.getPreview(body));
} }
// Execute action // Execute action
if (action == R.id.action_delete) { if (action == R.id.action_delete) {
EntityOperation.queue(context, db, draft, EntityOperation.DELETE); EntityFolder trash = db.folder().getFolderByType(draft.account, EntityFolder.TRASH);
if (empty || trash == null)
EntityOperation.queue(context, db, draft, EntityOperation.DELETE);
else {
EntityOperation.queue(context, db, draft, EntityOperation.SEEN, true);
EntityOperation.queue(context, db, draft, EntityOperation.MOVE, trash.id);
}
if (!empty) { if (!empty) {
Handler handler = new Handler(context.getMainLooper()); Handler handler = new Handler(context.getMainLooper());

Loading…
Cancel
Save