Skip to content

Commit

Permalink
chore: Font Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
asandikci committed Dec 6, 2024
1 parent 77028cc commit fc95329
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 49 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
49 changes: 0 additions & 49 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ protected void onCreate(final Bundle savedInstanceState) {
UpdateSettingsFragment.askForConsentToUpdateChecks(this);
}

// Apply the preferred font globally
final String preferredFont = getPreferredFont(this);
setUpFont(preferredFont);

if (!preferredFont.equals(getString(R.string.default_font_key))) {
Fonty.setFonts(this);
}
Expand All @@ -199,51 +195,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();
Expand Down
63 changes: 63 additions & 0 deletions app/src/main/java/org/schabi/newpipe/util/FontManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
import com.marcinorlowski.fonty.Fonty;


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));
}

// 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;
}

}
}

0 comments on commit fc95329

Please sign in to comment.