From 381c346c08cecdb86d7f6a5195d35c38044a1315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aliberk=20Sand=C4=B1k=C3=A7=C4=B1?= Date: Fri, 6 Dec 2024 20:43:19 +0300 Subject: [PATCH] chore: Font Manager --- app/src/main/java/org/schabi/newpipe/App.java | 2 + .../java/org/schabi/newpipe/MainActivity.java | 47 +------------- .../org/schabi/newpipe/util/FontManager.java | 61 +++++++++++++++++++ 3 files changed, 64 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/FontManager.java diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index 80dc68b3c..8fbb7451b 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -20,6 +20,7 @@ import org.schabi.newpipe.ktx.ExceptionUtils; import org.schabi.newpipe.settings.NewPipeSettings; import org.schabi.newpipe.util.BridgeStateSaverInitializer; +import org.schabi.newpipe.util.FontManager; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.StateSaver; @@ -102,6 +103,7 @@ public void onCreate() { Localization.getPreferredContentCountry(this)); Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext())); + FontManager.init(this); BridgeStateSaverInitializer.init(this); StateSaver.init(this); initNotificationChannels(); diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 216b3c7ce..ea476f5a8 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -57,7 +57,7 @@ import androidx.preference.PreferenceManager; import com.google.android.material.bottomsheet.BottomSheetBehavior; -import com.marcinorlowski.fonty.Fonty; +import com.marcinorlowski.fonty.Fonty; import org.schabi.newpipe.databinding.ActivityMainBinding; import org.schabi.newpipe.databinding.DrawerHeaderBinding; @@ -199,51 +199,6 @@ protected void onPostCreate(final Bundle savedInstanceState) { NewVersionWorker.enqueueNewVersionCheckingWork(app, false); } } - public String getPreferredFont(final Context context) { - final SharedPreferences preferences = PreferenceManager - .getDefaultSharedPreferences(context); - return preferences.getString("preferred_font", getString(R.string.default_font_key)); - } - - // build the relevant font TypeFace - public void setUpFont(final String preferredFont) { - switch (preferredFont) { - case "Arial": - Fonty.context(this) - .normalTypeface("arial.ttf") - .build(); - break; - case "Broadway": - Fonty.context(this) - .normalTypeface("BROADW.TTF") - .build(); - break; - case "Algerian": - Fonty.context(this) - .normalTypeface("Algerian.TTF") - .build(); - break; - case "Bell MT": - Fonty.context(this) - .normalTypeface("BELL.TTF") - .build(); - break; - case "Calibri": - Fonty.context(this) - .normalTypeface("calibrii.ttf") - .build(); - break; - case "Time New Roman": - Fonty.context(this) - .normalTypeface("times.ttf") - .build(); - break; - default: - // do nothing - break; - } - - } private void setupDrawer() throws ExtractionException { addDrawerMenuForCurrentService(); diff --git a/app/src/main/java/org/schabi/newpipe/util/FontManager.java b/app/src/main/java/org/schabi/newpipe/util/FontManager.java new file mode 100644 index 000000000..58faaabc2 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/FontManager.java @@ -0,0 +1,61 @@ +import com.marcinorlowski.fonty.Fonty; +import androidx.preference.PreferenceManager; + +public final class FontManager { + private FontManager() { + } + + public void init(final Context context) { + // Apply the preferred font globally + final String preferredFont = getPreferredFont(this); + + setUpFont(preferredFont); + } + + public String getPreferredFont(final Context context) { + final SharedPreferences preferences = PreferenceManager + .getDefaultSharedPreferences(context); + return preferences.getString("preferred_font", getString(R.string.default_font_key)); + } + +@Override + // build the relevant font TypeFace + public void setUpFont(final String preferredFont) { + switch (preferredFont) { + case "Arial": + Fonty.context(this) + .normalTypeface("arial.ttf") + .build(); + break; + case "Broadway": + Fonty.context(this) + .normalTypeface("BROADW.TTF") + .build(); + break; + case "Algerian": + Fonty.context(this) + .normalTypeface("Algerian.TTF") + .build(); + break; + case "Bell MT": + Fonty.context(this) + .normalTypeface("BELL.TTF") + .build(); + break; + case "Calibri": + Fonty.context(this) + .normalTypeface("calibrii.ttf") + .build(); + break; + case "Time New Roman": + Fonty.context(this) + .normalTypeface("times.ttf") + .build(); + break; + default: + // do nothing + break; + } + + } +}