From e0f0d2c7834045cdd6b845d2b33d467575fcee18 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 7 Feb 2022 12:28:19 +0100 Subject: [PATCH] Refactoring --- .../faircode/email/FragmentOptionsSend.java | 21 +++++++++------- .../java/eu/faircode/email/StyleHelper.java | 25 +++++++++++++------ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java b/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java index 54959bdb3e..0aea497738 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java @@ -25,6 +25,7 @@ import android.os.Bundle; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.TypefaceSpan; +import android.util.Pair; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -158,14 +159,14 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc swReceiptLegacy = view.findViewById(R.id.swReceiptLegacy); swLookupMx = view.findViewById(R.id.swLookupMx); - String[] fontNameNames = getResources().getStringArray(R.array.fontNameNames); - String[] fontNameValues = getResources().getStringArray(R.array.fontNameValues); + List> fonts = StyleHelper.getFonts(getContext()); List fn = new ArrayList<>(); fn.add("-"); - for (int i = 0; i < fontNameNames.length; i++) { - SpannableStringBuilder ssb = new SpannableStringBuilderEx(fontNameNames[i]); - ssb.setSpan(new TypefaceSpan(fontNameValues[i]), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + for (int i = 0; i < fonts.size(); i++) { + Pair font = fonts.get(i); + SpannableStringBuilder ssb = new SpannableStringBuilderEx(font.second); + ssb.setSpan(new TypefaceSpan(font.first), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); fn.add(ssb); } @@ -304,7 +305,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc spComposeFont.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView adapterView, View view, int position, long id) { - String value = (position == 0 ? "" : fontNameValues[position - 1]); + String value = (position == 0 ? "" : fonts.get(position - 1).first); boolean monospaced = prefs.getBoolean("monospaced", false); if (value.equals(monospaced ? "monospace" : "sans-serif")) prefs.edit().remove("compose_font").apply(); @@ -574,12 +575,14 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc boolean monospaced = prefs.getBoolean("monospaced", false); String compose_font = prefs.getString("compose_font", monospaced ? "monospace" : "sans-serif"); - String[] fontNameValues = getResources().getStringArray(R.array.fontNameValues); - for (int pos = 0; pos < fontNameValues.length; pos++) - if (fontNameValues[pos].equals(compose_font)) { + List> fonts = StyleHelper.getFonts(getContext()); + for (int pos = 0; pos < fonts.size(); pos++) { + Pair font = fonts.get(pos); + if (font.first.equals(compose_font)) { spComposeFont.setSelection(pos + 1); break; } + } swSeparateReply.setChecked(prefs.getBoolean("separate_reply", false)); swExtendedReply.setChecked(prefs.getBoolean("extended_reply", false)); diff --git a/app/src/main/java/eu/faircode/email/StyleHelper.java b/app/src/main/java/eu/faircode/email/StyleHelper.java index c1b4d24af2..357771c719 100644 --- a/app/src/main/java/eu/faircode/email/StyleHelper.java +++ b/app/src/main/java/eu/faircode/email/StyleHelper.java @@ -169,15 +169,15 @@ public class StyleHelper { } } - String[] fontNameNames = anchor.getResources().getStringArray(R.array.fontNameNames); - String[] fontNameValues = anchor.getResources().getStringArray(R.array.fontNameValues); + List> fonts = getFonts(anchor.getContext()); SubMenu smenu = popupMenu.getMenu().findItem(R.id.menu_style_font).getSubMenu(); - for (int i = 0; i < fontNameNames.length; i++) { - SpannableStringBuilder ssb = new SpannableStringBuilderEx(fontNameNames[i]); - ssb.setSpan(getTypefaceSpan(fontNameValues[i], context), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + for (int i = 0; i < fonts.size(); i++) { + Pair font = fonts.get(i); + SpannableStringBuilder ssb = new SpannableStringBuilderEx(font.second); + ssb.setSpan(getTypefaceSpan(font.first, context), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); smenu.add(R.id.group_style_font, i, 0, ssb); } - smenu.add(R.id.group_style_font, fontNameNames.length, 0, R.string.title_style_font_default); + smenu.add(R.id.group_style_font, fonts.size(), 0, R.string.title_style_font_default); int level = -1; BulletSpan[] spans = edit.getSpans(start, end, BulletSpan.class); @@ -505,8 +505,8 @@ public class StyleHelper { Log.breadcrumb("style", "action", "font"); int id = item.getItemId(); - String[] names = anchor.getResources().getStringArray(R.array.fontNameValues); - String face = (id < names.length ? names[id] : null); + List> fonts = StyleHelper.getFonts(anchor.getContext()); + String face = (id < fonts.size() ? fonts.get(id).first : null); return _setFont(face); } @@ -953,5 +953,14 @@ public class StyleHelper { return Typeface.DEFAULT; } + public static List> getFonts(Context context) { + List> result = new ArrayList<>(); + String[] fontNameNames = context.getResources().getStringArray(R.array.fontNameNames); + String[] fontNameValues = context.getResources().getStringArray(R.array.fontNameValues); + for (int i = 0; i < fontNameNames.length; i++) + result.add(new Pair(fontNameValues[i], fontNameNames[i])); + return result; + } + //TextUtils.dumpSpans(text, new LogPrinter(android.util.Log.INFO, "FairEmail"), "afterTextChanged "); }