Added reply count option

pull/194/merge
M66B 3 years ago
parent ba140e543e
commit 4bf9e9cc85

@ -320,29 +320,45 @@ public class EntityMessage implements Serializable {
return MessageHelper.equalDomain(context, reply, from);
}
private static String SUBJECT_COUNT = "((\\[\\d+\\])|(\\(\\d+\\)))?";
static int getReplies(Context context, String language, String subject) {
boolean found = false;
List<String> res = new ArrayList<>();
res.addAll(Arrays.asList(Helper.getStrings(context, language, R.string.title_subject_reply, "")));
res.addAll(Arrays.asList(Helper.getStrings(context, language, R.string.title_subject_reply_alt, "")));
for (String re : res) {
Matcher m = getPattern(re.trim()).matcher(subject);
if (m.matches()) {
found = true;
if (re.trim().endsWith(":"))
try {
String n = m.group(2);
if (n != null)
return Integer.parseInt(n.substring(1, n.length() - 1));
} catch (NumberFormatException ex) {
Log.e(ex);
}
}
}
return (found ? 1 : 0);
}
static String collapsePrefixes(Context context, String language, String subject, boolean forward) {
List<Pair<String, Boolean>> prefixes = new ArrayList<>();
for (String re : Helper.getStrings(context, language, R.string.title_subject_reply, ""))
prefixes.add(new Pair<>(re.trim().toLowerCase(), false));
prefixes.add(new Pair<>(re, false));
for (String re : Helper.getStrings(context, language, R.string.title_subject_reply_alt, ""))
prefixes.add(new Pair<>(re.trim().toLowerCase(), false));
prefixes.add(new Pair<>(re, false));
for (String fwd : Helper.getStrings(context, language, R.string.title_subject_forward, ""))
prefixes.add(new Pair<>(fwd.trim().toLowerCase(), true));
prefixes.add(new Pair<>(fwd, true));
for (String fwd : Helper.getStrings(context, language, R.string.title_subject_forward_alt, ""))
prefixes.add(new Pair<>(fwd.trim().toLowerCase(), true));
prefixes.add(new Pair<>(fwd, true));
List<Boolean> scanned = new ArrayList<>();
subject = subject.trim();
while (true) {
boolean found = false;
for (Pair<String, Boolean> prefix : prefixes) {
String pre = prefix.first.endsWith(":")
? "(^" + Pattern.quote(prefix.first.substring(0, prefix.first.length() - 1)) + SUBJECT_COUNT + ":)"
: "(^" + Pattern.quote(prefix.first) + ")";
Pattern p = Pattern.compile(pre + "(\\s*)(.*)", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(subject);
Matcher m = getPattern(prefix.first.trim()).matcher(subject);
if (m.matches()) {
found = true;
int count = scanned.size();
@ -364,6 +380,13 @@ public class EntityMessage implements Serializable {
return result.toString();
}
private static Pattern getPattern(String prefix) {
String pre = prefix.endsWith(":")
? "(^" + Pattern.quote(prefix.substring(0, prefix.length() - 1)) + ")" + "((\\[\\d+\\])|(\\(\\d+\\)))?" + ":"
: "(^" + Pattern.quote(prefix) + ")";
return Pattern.compile(pre + "(\\s*)(.*)", Pattern.CASE_INSENSITIVE);
}
Element getReplyHeader(Context context, Document document, boolean separate, boolean extended) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean language_detection = prefs.getBoolean("language_detection", false);

@ -274,6 +274,7 @@ public class FragmentCompose extends FragmentBase {
private AdapterAttachment adapter;
private boolean prefix_once = false;
private boolean prefix_count = false;
private boolean alt_re = false;
private boolean alt_fwd = false;
private boolean monospaced = false;
@ -335,6 +336,7 @@ public class FragmentCompose extends FragmentBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefix_once = prefs.getBoolean("prefix_once", true);
prefix_count = prefs.getBoolean("prefix_count", false);
alt_re = prefs.getBoolean("alt_re", false);
alt_fwd = prefs.getBoolean("alt_fwd", false);
monospaced = prefs.getBoolean("monospaced", false);
@ -4578,12 +4580,24 @@ public class FragmentCompose extends FragmentBase {
// Subject
String subject = (ref.subject == null ? "" : ref.subject);
if ("reply".equals(action) || "reply_all".equals(action)) {
if (prefix_once)
int replies = 0;
if (prefix_once) {
if (prefix_count)
replies = EntityMessage.getReplies(context, ref.language, subject);
subject = EntityMessage.collapsePrefixes(context, ref.language, subject, false);
data.draft.subject = Helper.getString(context,
}
String re = Helper.getString(context,
ref.language,
alt_re ? R.string.title_subject_reply_alt : R.string.title_subject_reply,
"").trim();
String s = Helper.getString(context,
ref.language,
alt_re ? R.string.title_subject_reply_alt : R.string.title_subject_reply,
subject);
if (replies > 0 && re.endsWith(":") && s.startsWith(re))
s = re.substring(0, re.length() - 1) + "[" + (replies + 1) + "]:" +
s.substring(re.length());
data.draft.subject = s;
if (external_text != null) {
Element div = document.createElement("div");

@ -59,6 +59,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
private SwitchCompat swSuggestFrequently;
private Button btnLocalContacts;
private SwitchCompat swPrefixOnce;
private SwitchCompat swPrefixCount;
private RadioGroup rgRe;
private RadioGroup rgFwd;
private SwitchCompat swSendReminders;
@ -97,7 +98,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
"alt_re", "alt_fwd",
"send_reminders", "send_delayed",
"attach_new", "reply_all", "send_pending",
"compose_font", "prefix_once", "separate_reply", "extended_reply", "write_below", "quote_reply", "quote_limit", "resize_reply",
"compose_font", "prefix_once", "prefix_count", "separate_reply", "extended_reply", "write_below", "quote_reply", "quote_limit", "resize_reply",
"signature_location", "signature_new", "signature_reply", "signature_forward",
"discard_delete", "reply_move",
"auto_link", "plain_only", "format_flowed", "usenet_signature", "remove_signatures",
@ -122,6 +123,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
swSuggestFrequently = view.findViewById(R.id.swSuggestFrequently);
btnLocalContacts = view.findViewById(R.id.btnLocalContacts);
swPrefixOnce = view.findViewById(R.id.swPrefixOnce);
swPrefixCount = view.findViewById(R.id.swPrefixCount);
rgRe = view.findViewById(R.id.rgRe);
rgFwd = view.findViewById(R.id.rgFwd);
swSendReminders = view.findViewById(R.id.swSendReminders);
@ -231,6 +233,14 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("prefix_once", checked).apply();
swPrefixOnce.setEnabled(checked);
}
});
swPrefixCount.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("prefix_count", checked).apply();
}
});
@ -534,6 +544,8 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
swSuggestFrequently.setEnabled(swSuggestSent.isChecked() || swSuggestReceived.isChecked());
swPrefixOnce.setChecked(prefs.getBoolean("prefix_once", true));
swPrefixCount.setChecked(prefs.getBoolean("prefix_count", false));
swPrefixCount.setEnabled(swPrefixOnce.isChecked());
rgRe.check(prefs.getBoolean("alt_re", false) ? R.id.rbRe2 : R.id.rbRe1);
rgFwd.check(prefs.getBoolean("alt_fwd", false) ? R.id.rbFwd2 : R.id.rbFwd1);

@ -195,6 +195,18 @@
app:layout_constraintTop_toBottomOf="@id/btnLocalContacts"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swPrefixCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_prefix_count"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swPrefixOnce"
app:switchPadding="12dp" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvAltReFws"
android:layout_width="wrap_content"
@ -204,7 +216,7 @@
android:text="@string/title_advanced_alt_re_fwd"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swPrefixOnce" />
app:layout_constraintTop_toBottomOf="@id/swPrefixCount" />
<RadioGroup
android:id="@+id/rgRe"

@ -379,6 +379,7 @@
<string name="title_advanced_compose_font">Default font</string>
<string name="title_advanced_prefix_once">Prefix subject only once on replying or forwarding</string>
<string name="title_advanced_prefix_count">Add count to reply prefix</string>
<string name="title_advanced_separate_reply">Insert a horizontal line before a reply/forward header</string>
<string name="title_advanced_extended_reply">Use extended reply/forward header</string>
<string name="title_advanced_write_above">Write above the sender\'s text</string>

Loading…
Cancel
Save