1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-27 18:27:43 +00:00

Refactoring

This commit is contained in:
M66B 2022-11-13 21:00:35 +01:00
parent 363675ab83
commit b71f3ccd32

View file

@ -107,10 +107,12 @@ public class LanguageTool {
static List<Suggestion> getSuggestions(Context context, CharSequence text) throws IOException, JSONException { static List<Suggestion> getSuggestions(Context context, CharSequence text) throws IOException, JSONException {
if (isPremium(context)) if (isPremium(context))
try { try {
List<Pair<Integer, Integer>> ranges = new ArrayList<>(); List<Pair<Integer, Integer>> paragraphs = new ArrayList<>();
// Skip links and email addresses
Pattern pattern = Pattern.compile("(" + Helper.EMAIL_ADDRESS + ")" + Pattern pattern = Pattern.compile("(" + Helper.EMAIL_ADDRESS + ")" +
"|(" + PatternsCompat.AUTOLINK_WEB_URL.pattern() + ")"); "|(" + PatternsCompat.AUTOLINK_WEB_URL.pattern() + ")");
Matcher matcher = pattern.matcher(text); Matcher matcher = pattern.matcher(text);
int index = 0; int index = 0;
boolean links = false; boolean links = false;
@ -118,19 +120,20 @@ public class LanguageTool {
links = true; links = true;
int start = matcher.start(); int start = matcher.start();
int end = matcher.end(); int end = matcher.end();
ranges.addAll(getRanges(index, start, text)); paragraphs.addAll(getParagraphs(index, start, text));
Log.i("LT skipping " + start + "..." + end + Log.i("LT skipping " + start + "..." + end +
" '" + text.subSequence(start, end).toString().replace('\n', '|') + "'"); " '" + text.subSequence(start, end).toString().replace('\n', '|') + "'");
index = end; index = end;
} }
ranges.addAll(getRanges(index, text.length(), text)); paragraphs.addAll(getParagraphs(index, text.length(), text));
for (Pair<Integer, Integer> range : ranges) // Get suggestions for paragraphs
Log.i("LT range " + range.first + "..." + range.second + for (Pair<Integer, Integer> paragraph : paragraphs)
" '" + text.subSequence(range.first, range.second).toString().replace('\n', '|') + "'"); Log.i("LT paragraph " + paragraph.first + "..." + paragraph.second +
if (ranges.size() <= LT_MAX_RANGES || links) { " '" + text.subSequence(paragraph.first, paragraph.second).toString().replace('\n', '|') + "'");
if (links || paragraphs.size() <= LT_MAX_RANGES) {
List<Suggestion> result = new ArrayList<>(); List<Suggestion> result = new ArrayList<>();
for (Pair<Integer, Integer> range : ranges) for (Pair<Integer, Integer> range : paragraphs)
result.addAll(getSuggestions(context, text, range.first, range.second)); result.addAll(getSuggestions(context, text, range.first, range.second));
return result; return result;
} }
@ -143,11 +146,11 @@ public class LanguageTool {
return getSuggestions(context, text, 0, text.length()); return getSuggestions(context, text, 0, text.length());
} }
private static List<Pair<Integer, Integer>> getRanges(int from, int to, CharSequence text) { private static List<Pair<Integer, Integer>> getParagraphs(int from, int to, CharSequence text) {
Log.i("LT ranges " + from + "..." + to + Log.i("LT paragraphs " + from + "..." + to +
" '" + text.subSequence(from, to).toString().replace('\n', '|') + "'"); " '" + text.subSequence(from, to).toString().replace('\n', '|') + "'");
List<Pair<Integer, Integer>> ranges = new ArrayList<>(); List<Pair<Integer, Integer>> paragraphs = new ArrayList<>();
int start = from; int start = from;
int end = start; int end = start;
@ -157,13 +160,13 @@ public class LanguageTool {
if (end > start) { if (end > start) {
String fragment = text.subSequence(start, end).toString(); String fragment = text.subSequence(start, end).toString();
if (!TextUtils.isEmpty(fragment.trim())) if (!TextUtils.isEmpty(fragment.trim()))
ranges.add(new Pair<>(start, end)); paragraphs.add(new Pair<>(start, end));
} }
start = end + 1; start = end + 1;
end = start; end = start;
} }
return ranges; return paragraphs;
} }
private static List<Suggestion> getSuggestions(Context context, CharSequence text, int start, int end) throws IOException, JSONException { private static List<Suggestion> getSuggestions(Context context, CharSequence text, int start, int end) throws IOException, JSONException {