Skip to content

Commit

Permalink
Only load font when it is used
Browse files Browse the repository at this point in the history
To use just as much memory as required.
  • Loading branch information
markusfisch committed Jul 24, 2023
1 parent 3db0239 commit 6617ad1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ public void onActivityResult(int requestCode, int resultCode,
if (requestCode == PICK_FILE_RESULT_CODE &&
resultCode == Activity.RESULT_OK && resultData != null) {
Context context = getContext();
String message = DatabaseImporter.importDatabase(context,
resultData.getData());
if (context == null) {
return;
}
String message = DatabaseImporter.importDatabase(
context, resultData.getData());
Toast.makeText(context, message,
Toast.LENGTH_LONG).show();
}
Expand Down Expand Up @@ -87,7 +90,7 @@ public void onSharedPreferenceChanged(
return;
}

ShaderEditorApp.preferences.update();
ShaderEditorApp.preferences.update(getContext());
setSummary(preference);

if (Preferences.SAVE_BATTERY.equals(key) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public class Preferences {
private static final int RUN_MANUALLY = 2;
private static final int RUN_MANUALLY_EXTRA = 3;
private static final int RUN_MANUALLY_EXTRA_NEW = 4;
private static final Map<String, Integer> fontNameToResId = new HashMap<>();

static {
fontNameToResId.put("fira_code", R.font.fira_code);
fontNameToResId.put("ibm_plex_mono", R.font.ibm_plex_mono);
fontNameToResId.put("jetbrains_mono", R.font.jetbrains_mono);
fontNameToResId.put("roboto_mono", R.font.roboto_mono);
fontNameToResId.put("source_code_pro", R.font.source_code_pro);
}

private SharedPreferences preferences;
private long wallpaperShaderId = 1;
Expand All @@ -64,7 +73,6 @@ public class Preferences {
private boolean disableHighlighting = false;
private boolean autoSave = true;
private boolean showLineNumbers = true;
private final Map<String, Typeface> fonts = new HashMap<>();
private String defaultFont;

public void init(Context context) {
Expand All @@ -80,17 +88,16 @@ public void init(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(
context);

loadFonts(context);
defaultFont = context.getString(R.string.default_font_value);

update();
update(context);
}

public SharedPreferences getSharedPreferences() {
return preferences;
}

public void update() {
public void update(Context context) {
wallpaperShaderId = parseLong(
preferences.getString(WALLPAPER_SHADER, null),
wallpaperShaderId);
Expand All @@ -109,8 +116,10 @@ public void update() {
textSize = parseInt(
preferences.getString(TEXT_SIZE, null),
textSize);
font = getLoadedFont(preferences.getString(FONT, defaultFont));
useLigatures = preferences.getBoolean(USE_LIGATURES, true);
font = loadFont(context,
preferences.getString(FONT, defaultFont));
useLigatures = preferences.getBoolean(
USE_LIGATURES, true);
tabWidth = parseInt(
preferences.getString(TAB_WIDTH, null),
tabWidth);
Expand All @@ -135,23 +144,24 @@ public void update() {
defaultNewShaderId = parseLong(
preferences.getString(DEFAULT_NEW_SHADER, null),
defaultNewShaderId);
showLineNumbers = preferences.getBoolean(SHOW_LINE_NUMBERS, showLineNumbers);
}

private void loadFonts(Context context) {
fonts.put("fira_code", ResourcesCompat.getFont(context, R.font.fira_code));
fonts.put("ibm_plex_mono", ResourcesCompat.getFont(context, R.font.ibm_plex_mono));
fonts.put("jetbrains_mono", ResourcesCompat.getFont(context, R.font.jetbrains_mono));
fonts.put("roboto_mono", ResourcesCompat.getFont(context, R.font.roboto_mono));
fonts.put("source_code_pro", ResourcesCompat.getFont(context, R.font.source_code_pro));
showLineNumbers = preferences.getBoolean(
SHOW_LINE_NUMBERS,
showLineNumbers);
}

private @NonNull Typeface getLoadedFont(@NonNull String fontName) {
Typeface tf = fonts.get(fontName);
if (tf == null) {
private @NonNull Typeface loadFont(
@NonNull Context context,
@NonNull String fontName) {
Integer resId = fontNameToResId.get(fontName);
if (resId == null) {
throw new IllegalArgumentException(
"font \"" + fontName + "\" not found!");
}
Typeface tf = ResourcesCompat.getFont(context, resId);
if (tf == null) {
throw new IllegalArgumentException(
"font \"" + fontName + "\" could not be loaded!");
}
return tf;
}

Expand Down

0 comments on commit 6617ad1

Please sign in to comment.