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

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

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

Loading…
Cancel
Save