Refactoring

pull/162/head
M66B 6 years ago
parent 1ec286fe8b
commit e54a6ca90c

@ -19,8 +19,6 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B) Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/ */
import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
@ -54,43 +52,34 @@ public class EntityAnswer implements Serializable {
@NonNull @NonNull
public String text; public String text;
static String getAnswerText(Context context, long id, Address[] from) { String getText(Address[] address) {
DB db = DB.getInstance(context); return replacePlaceholders(text, address);
EntityAnswer answer = db.answer().getAnswer(id);
if (answer == null)
return null;
return getAnswerText(answer, from);
} }
static String getAnswerText(EntityAnswer answer, Address[] from) { static String replacePlaceholders(String text, Address[] address) {
String name = null; String fullName = null;
String email = null; String email = null;
if (from != null && from.length > 0) { if (address != null && address.length > 0) {
name = ((InternetAddress) from[0]).getPersonal(); fullName = ((InternetAddress) address[0]).getPersonal();
email = ((InternetAddress) from[0]).getAddress(); email = ((InternetAddress) address[0]).getAddress();
} }
return replacePlaceholders(answer.text, name, email); String first = fullName;
} String last = null;
static String replacePlaceholders(String text, String fullName, String email) {
String firstName = null;
String lastName = null;
if (fullName != null) { if (fullName != null) {
fullName = fullName.trim(); fullName = fullName.trim();
int c = fullName.lastIndexOf(","); int c = fullName.lastIndexOf(",");
if (c < 0) if (c < 0)
c = fullName.lastIndexOf(" "); c = fullName.lastIndexOf(" ");
if (c > 0) { if (c > 0) {
firstName = fullName.substring(0, c).trim(); first = fullName.substring(0, c).trim();
lastName = fullName.substring(c + 1).trim(); last = fullName.substring(c + 1).trim();
} }
} }
text = text.replace("$name$", fullName == null ? "" : fullName); text = text.replace("$name$", fullName == null ? "" : fullName);
text = text.replace("$firstname$", firstName == null ? "" : firstName); text = text.replace("$firstname$", first == null ? "" : first);
text = text.replace("$lastname$", lastName == null ? "" : lastName); text = text.replace("$lastname$", last == null ? "" : last);
text = text.replace("$email$", email == null ? "" : email); text = text.replace("$email$", email == null ? "" : email);
return text; return text;

@ -352,10 +352,11 @@ public class EntityRule {
if (identity == null) if (identity == null)
throw new IllegalArgumentException("Rule identity not found"); throw new IllegalArgumentException("Rule identity not found");
String body = EntityAnswer.getAnswerText(context, aid, message.from); EntityAnswer answer = db.answer().getAnswer(aid);
if (body == null) if (answer == null)
throw new IllegalArgumentException("Rule answer not found"); throw new IllegalArgumentException("Rule answer not found");
EntityMessage reply = new EntityMessage(); EntityMessage reply = new EntityMessage();
reply.account = message.account; reply.account = message.account;
reply.folder = db.folder().getOutbox().id; reply.folder = db.folder().getOutbox().id;
@ -376,6 +377,8 @@ public class EntityRule {
reply.avatar = (lookupUri == null ? null : lookupUri.toString()); reply.avatar = (lookupUri == null ? null : lookupUri.toString());
reply.id = db.message().insertMessage(reply); reply.id = db.message().insertMessage(reply);
String body = answer.getText(message.from);
Helper.writeText(reply.getFile(context), body); Helper.writeText(reply.getFile(context), body);
db.message().setMessageContent(reply.id, db.message().setMessageContent(reply.id,
true, true,

@ -1730,10 +1730,13 @@ public class FragmentCompose extends FragmentBase {
private void onAnswerSelected(Bundle args) { private void onAnswerSelected(Bundle args) {
String answer = args.getString("answer"); String answer = args.getString("answer");
EntityIdentity identity = (EntityIdentity) spIdentity.getSelectedItem(); InternetAddress[] to = null;
String name = (identity == null ? null : identity.name); try {
String email = (identity == null ? null : identity.email); to = InternetAddress.parse(etTo.getText().toString());
String text = EntityAnswer.replacePlaceholders(answer, name, email); } catch (AddressException ignored) {
}
String text = EntityAnswer.replacePlaceholders(answer, to);
Spanned spanned = HtmlHelper.fromHtml(text); Spanned spanned = HtmlHelper.fromHtml(text);
etBody.getText().insert(etBody.getSelectionStart(), spanned); etBody.getText().insert(etBody.getSelectionStart(), spanned);
@ -2068,7 +2071,7 @@ public class FragmentCompose extends FragmentBase {
EntityAnswer a = db.answer().getAnswer(answer); EntityAnswer a = db.answer().getAnswer(answer);
if (a != null) { if (a != null) {
data.draft.subject = a.name; data.draft.subject = a.name;
body = EntityAnswer.getAnswerText(a, null) + body; body = a.getText(null) + body;
} }
} }
} else { } else {
@ -2146,8 +2149,12 @@ public class FragmentCompose extends FragmentBase {
data.draft.subject = status + ": " + ref.subject; data.draft.subject = status + ": " + ref.subject;
data.draft.plain_only = ref.plain_only; data.draft.plain_only = ref.plain_only;
if (answer > 0)
body = EntityAnswer.getAnswerText(context, answer, data.draft.to) + body; if (answer > 0) {
EntityAnswer a = db.answer().getAnswer(answer);
if (a != null)
body = a.getText(data.draft.to) + body;
}
} }
if (plain_only) if (plain_only)

Loading…
Cancel
Save