Refactoring: font descriptor

pull/194/merge
M66B 3 years ago
parent ae80da7a86
commit 46e0b09e4d

@ -159,14 +159,14 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
swReceiptLegacy = view.findViewById(R.id.swReceiptLegacy);
swLookupMx = view.findViewById(R.id.swLookupMx);
List<Pair<String, String>> fonts = StyleHelper.getFonts(getContext());
List<StyleHelper.FontDescriptor> fonts = StyleHelper.getFonts(getContext());
List<CharSequence> fn = new ArrayList<>();
fn.add("-");
for (int i = 0; i < fonts.size(); i++) {
Pair<String, String> font = fonts.get(i);
SpannableStringBuilder ssb = new SpannableStringBuilderEx(font.second);
ssb.setSpan(new TypefaceSpan(font.first), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
StyleHelper.FontDescriptor font = fonts.get(i);
SpannableStringBuilder ssb = new SpannableStringBuilderEx(font.toString());
ssb.setSpan(new TypefaceSpan(font.type), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
fn.add(ssb);
}
@ -305,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 ? "" : fonts.get(position - 1).first);
String value = (position == 0 ? "" : fonts.get(position - 1).type);
boolean monospaced = prefs.getBoolean("monospaced", false);
if (value.equals(monospaced ? "monospace" : "sans-serif"))
prefs.edit().remove("compose_font").apply();
@ -575,10 +575,10 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
boolean monospaced = prefs.getBoolean("monospaced", false);
String compose_font = prefs.getString("compose_font", monospaced ? "monospace" : "sans-serif");
List<Pair<String, String>> fonts = StyleHelper.getFonts(getContext());
List<StyleHelper.FontDescriptor> fonts = StyleHelper.getFonts(getContext());
for (int pos = 0; pos < fonts.size(); pos++) {
Pair<String, String> font = fonts.get(pos);
if (font.first.equals(compose_font)) {
StyleHelper.FontDescriptor font = fonts.get(pos);
if (font.type.equals(compose_font)) {
spComposeFont.setSelection(pos + 1);
break;
}

@ -48,6 +48,7 @@ import android.view.SubMenu;
import android.view.View;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.content.res.ResourcesCompat;
import androidx.lifecycle.LifecycleOwner;
@ -167,15 +168,15 @@ public class StyleHelper {
}
}
List<Pair<String, String>> fonts = getFonts(anchor.getContext());
List<FontDescriptor> fonts = getFonts(anchor.getContext());
SubMenu smenu = popupMenu.getMenu().findItem(R.id.menu_style_font).getSubMenu();
for (int i = 0; i < fonts.size(); i++) {
Pair<String, String> 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);
FontDescriptor font = fonts.get(i);
SpannableStringBuilder ssb = new SpannableStringBuilderEx(font.toString());
ssb.setSpan(getTypefaceSpan(font.type, context), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
smenu.add(font.custom ? R.id.group_style_font_custom : R.id.group_style_font_standard, i, 0, ssb);
}
smenu.add(R.id.group_style_font, fonts.size(), 0, R.string.title_style_font_default);
smenu.add(R.id.group_style_font_standard, fonts.size(), 0, R.string.title_style_font_default);
int level = -1;
BulletSpan[] spans = edit.getSpans(start, end, BulletSpan.class);
@ -207,7 +208,8 @@ public class StyleHelper {
return setBackground(item);
} else if (itemId == R.id.menu_style_color) {
return setColor(item);
} else if (groupId == R.id.group_style_font) {
} else if (groupId == R.id.group_style_font_standard ||
groupId == R.id.group_style_font_custom) {
return setFont(item);
} else if (groupId == R.id.group_style_align) {
return setAlignment(item);
@ -503,8 +505,8 @@ public class StyleHelper {
Log.breadcrumb("style", "action", "font");
int id = item.getItemId();
List<Pair<String, String>> fonts = StyleHelper.getFonts(anchor.getContext());
String face = (id < fonts.size() ? fonts.get(id).first : null);
List<FontDescriptor> fonts = getFonts(anchor.getContext());
String face = (id < fonts.size() ? fonts.get(id).type : null);
return _setFont(face);
}
@ -951,15 +953,38 @@ public class StyleHelper {
return Typeface.DEFAULT;
}
public static List<Pair<String, String>> getFonts(Context context) {
List<Pair<String, String>> result = new ArrayList<>();
public static List<FontDescriptor> getFonts(Context context) {
List<FontDescriptor> 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]));
result.add(new Pair("comic sans", "OpenDyslexic"));
result.add(new FontDescriptor(fontNameValues[i], fontNameNames[i]));
result.add(new FontDescriptor("comic sans", "OpenDyslexic", true));
return result;
}
public static class FontDescriptor {
@NonNull
public String type;
@NonNull
public String name;
public boolean custom;
FontDescriptor(String type, String name) {
this(type, name, false);
}
FontDescriptor(String type, String name, boolean custom) {
this.type = type;
this.name = name;
this.custom = custom;
}
@Override
public String toString() {
return name;
}
}
//TextUtils.dumpSpans(text, new LogPrinter(android.util.Log.INFO, "FairEmail"), "afterTextChanged ");
}

@ -48,7 +48,8 @@
android:orderInCategory="4"
android:title="@string/title_style_font">
<menu>
<group android:id="@+id/group_style_font" />
<group android:id="@+id/group_style_font_standard" />
<group android:id="@+id/group_style_font_custom" />
</menu>
</item>

Loading…
Cancel
Save