Refactoring

This commit is contained in:
M66B 2022-10-01 09:51:16 +02:00
parent ec8cb9c656
commit ee8e26725e
2 changed files with 42 additions and 35 deletions

View File

@ -74,7 +74,6 @@ import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.ArrowKeyMovementMethod;
@ -85,7 +84,6 @@ import android.text.style.ImageSpan;
import android.text.style.ParagraphStyle;
import android.text.style.QuoteSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.SuggestionSpan;
import android.text.style.URLSpan;
import android.util.LogPrinter;
import android.util.Pair;
@ -2599,25 +2597,7 @@ public class FragmentCompose extends FragmentBase {
return;
}
Editable edit = etBody.getText();
if (edit == null)
return;
// https://developer.android.com/reference/android/text/style/SuggestionSpan
for (SuggestionSpanEx span : edit.getSpans(0, edit.length(), SuggestionSpanEx.class)) {
Log.i("LT removing=" + span);
edit.removeSpan(span);
}
for (LanguageTool.Suggestion suggestion : suggestions) {
Log.i("LT adding=" + suggestion);
SuggestionSpan span = new SuggestionSpanEx(getContext(),
suggestion.replacements.toArray(new String[0]),
SuggestionSpan.FLAG_MISSPELLED);
int start = suggestion.offset;
int end = start + suggestion.length;
edit.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
LanguageTool.applySuggestions(etBody, suggestions);
}
@Override
@ -2636,20 +2616,6 @@ public class FragmentCompose extends FragmentBase {
}.execute(this, args, "compose:lt");
}
private static class SuggestionSpanEx extends SuggestionSpan {
private final int textColorHighlight;
public SuggestionSpanEx(Context context, String[] suggestions, int flags) {
super(context, suggestions, flags);
textColorHighlight = Helper.resolveColor(context, android.R.attr.textColorHighlight);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.bgColor = textColorHighlight;
}
}
private boolean onActionStyle(int action, View anchor) {
Log.i("Style action=" + action);
return StyleHelper.apply(action, getViewLifecycleOwner(), anchor, etBody);

View File

@ -21,6 +21,11 @@ package eu.faircode.email;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.SuggestionSpan;
import android.widget.EditText;
import androidx.preference.PreferenceManager;
@ -146,6 +151,28 @@ public class LanguageTool {
}
}
static void applySuggestions(EditText etBody, List<Suggestion> suggestions) {
Editable edit = etBody.getText();
if (edit == null)
return;
// https://developer.android.com/reference/android/text/style/SuggestionSpan
for (SuggestionSpanEx span : edit.getSpans(0, edit.length(), SuggestionSpanEx.class)) {
Log.i("LT removing=" + span);
edit.removeSpan(span);
}
for (LanguageTool.Suggestion suggestion : suggestions) {
Log.i("LT adding=" + suggestion);
SuggestionSpan span = new SuggestionSpanEx(etBody.getContext(),
suggestion.replacements.toArray(new String[0]),
SuggestionSpan.FLAG_MISSPELLED);
int start = suggestion.offset;
int end = start + suggestion.length;
edit.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
static class Suggestion {
String title; // shortMessage
String description; // message
@ -158,4 +185,18 @@ public class LanguageTool {
return title;
}
}
private static class SuggestionSpanEx extends SuggestionSpan {
private final int textColorHighlight;
public SuggestionSpanEx(Context context, String[] suggestions, int flags) {
super(context, suggestions, flags);
textColorHighlight = Helper.resolveColor(context, android.R.attr.textColorHighlight);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.bgColor = textColorHighlight;
}
}
}