diff --git a/QKSMS/src/main/java/com/moez/QKSMS/common/FontManager.java b/QKSMS/src/main/java/com/moez/QKSMS/common/FontManager.java index e3b8e12e..4a112c56 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/common/FontManager.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/common/FontManager.java @@ -3,6 +3,7 @@ package com.moez.QKSMS.common; import android.content.Context; import android.content.SharedPreferences; import android.content.res.ColorStateList; +import android.graphics.Typeface; import android.preference.PreferenceManager; import com.moez.QKSMS.R; import com.moez.QKSMS.ui.ThemeManager; @@ -26,7 +27,7 @@ public class FontManager { public static final int TEXT_TYPE_DIALOG_BUTTON = 0x7; public static final int TEXT_TYPE_TOOLBAR = 0x8; - public static int getFontFamily(Context context) { + private static int getFontFamily(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); return Integer.parseInt(prefs.getString(SettingsFragment.FONT_FAMILY, "0")); } @@ -115,7 +116,7 @@ public class FontManager { return ColorStateList.valueOf(ThemeManager.getTextOnBackgroundPrimary()); } - public static int getFontWeight(Context context, boolean heavy) { + private static int getFontWeight(Context context, boolean heavy) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int fontWeight = Integer.parseInt(prefs.getString(SettingsFragment.FONT_WEIGHT, "0")); int fontFamily = getFontFamily(context); @@ -133,4 +134,16 @@ public class FontManager { return TypefaceManager.TextWeight.BOLD; } } + + public static Typeface getFont(Context context, int type) { + int fontFamily = getFontFamily(context); + int fontWeight = getFontWeight(context, getIsFontHeavy(type)); + return TypefaceManager.obtainTypeface(context, fontFamily, fontWeight); + } + + public static Typeface getFont(Context context) { + int fontFamily = getFontFamily(context); + int fontWeight = getFontWeight(context, false); + return TypefaceManager.obtainTypeface(context, fontFamily, fontWeight); + } } diff --git a/QKSMS/src/main/java/com/moez/QKSMS/common/LiveViewManager.java b/QKSMS/src/main/java/com/moez/QKSMS/common/LiveViewManager.java index b7d55897..97121b8b 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/common/LiveViewManager.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/common/LiveViewManager.java @@ -57,24 +57,6 @@ public abstract class LiveViewManager { }); } - /** - * Convenience method for #registerView(QKPreference, ThemedView) to allow registering a single - * ThemedView to listen for multiple preferences - * - * @param view The LiveView - * @param parent The object to tie the lifecycle of the LiveView to. If we only reference - * a LiveView anonymous inner class, then it'll be quickly garbage collected - * and removed from the WeakHashMap. Instead, we should reference the parent - * object (ie. The Activity, Fragment, View, etc...) that this LiveView is - * concerned with. In most cases, it's acceptable to just pass in `this` - * @param preferences The preferences to listen for - */ - public static void registerView(LiveView view, Object parent, QKPreference... preferences) { - for (QKPreference preference : preferences) { - registerView(preference, parent, view); - } - } - /** * Register a view to be updated when a QKPreference is changed * We don't need to manually unregister the views because we're using weak sets diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/AutoCompleteContactView.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/AutoCompleteContactView.java index b80a4203..83995f3b 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/AutoCompleteContactView.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/AutoCompleteContactView.java @@ -9,7 +9,6 @@ import com.android.ex.chips.BaseRecipientAdapter; import com.android.ex.chips.RecipientEditTextView; import com.moez.QKSMS.common.FontManager; import com.moez.QKSMS.common.LiveViewManager; -import com.moez.QKSMS.common.TypefaceManager; import com.moez.QKSMS.enums.QKPreference; import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.base.QKActivity; @@ -41,11 +40,13 @@ public class AutoCompleteContactView extends RecipientEditTextView { setAdapter(mAdapter); setOnItemClickListener(this); - LiveViewManager.registerView(key -> { - int fontFamily = FontManager.getFontFamily(mContext); - int fontWeight = FontManager.getFontWeight(mContext, false); - setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight)); - }, QKPreference.FONT_FAMILY, QKPreference.FONT_WEIGHT); + LiveViewManager.registerView(QKPreference.FONT_FAMILY, this, key -> { + setTypeface(FontManager.getFont(mContext)); + }); + + LiveViewManager.registerView(QKPreference.FONT_WEIGHT, this, key -> { + setTypeface(FontManager.getFont(mContext)); + }); LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> { setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY)); diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKEditText.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKEditText.java index e8191566..aed937c4 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKEditText.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKEditText.java @@ -10,7 +10,6 @@ import android.util.AttributeSet; import android.util.TypedValue; import com.moez.QKSMS.common.FontManager; import com.moez.QKSMS.common.LiveViewManager; -import com.moez.QKSMS.common.TypefaceManager; import com.moez.QKSMS.enums.QKPreference; import com.moez.QKSMS.ui.ThemeManager; @@ -50,11 +49,13 @@ public class QKEditText extends android.widget.EditText { private void init(Context context) { mContext = context; - LiveViewManager.registerView(key -> { - int fontFamily = FontManager.getFontFamily(mContext); - int fontWeight = FontManager.getFontWeight(mContext, false); - setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight)); - }, QKPreference.FONT_FAMILY, QKPreference.FONT_WEIGHT); + LiveViewManager.registerView(QKPreference.FONT_FAMILY, this, key -> { + setTypeface(FontManager.getFont(mContext)); + }); + + LiveViewManager.registerView(QKPreference.FONT_WEIGHT, this, key -> { + setTypeface(FontManager.getFont(mContext)); + }); LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> { setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY)); diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKSwitch.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKSwitch.java index 7249333a..fb17cfdb 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKSwitch.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKSwitch.java @@ -29,10 +29,15 @@ public class QKSwitch extends SwitchCompat { private void init(Context context) { mRes = context.getResources(); - LiveViewManager.registerView(key -> { + LiveViewManager.registerView(QKPreference.THEME, this, key -> { DrawableCompat.setTintList(getThumbDrawable(), getSwitchThumbColorStateList()); DrawableCompat.setTintList(getTrackDrawable(), getSwitchTrackColorStateList()); - }, QKPreference.THEME, QKPreference.BACKGROUND); + }); + + LiveViewManager.registerView(QKPreference.BACKGROUND, this, key -> { + DrawableCompat.setTintList(getThumbDrawable(), getSwitchThumbColorStateList()); + DrawableCompat.setTintList(getTrackDrawable(), getSwitchTrackColorStateList()); + }); } private ColorStateList getSwitchThumbColorStateList() { diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKTextView.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKTextView.java index 2822fc7d..ad0559a1 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKTextView.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/view/QKTextView.java @@ -13,9 +13,8 @@ import android.widget.TextView; import com.moez.QKSMS.R; import com.moez.QKSMS.common.FontManager; import com.moez.QKSMS.common.LiveViewManager; -import com.moez.QKSMS.common.TypefaceManager; -import com.moez.QKSMS.enums.QKPreference; import com.moez.QKSMS.common.utils.TextUtils; +import com.moez.QKSMS.enums.QKPreference; import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.settings.SettingsFragment; @@ -75,11 +74,13 @@ public class QKTextView extends TextView { setTextColor(FontManager.getTextColor(mContext, mType))); } - LiveViewManager.registerView(key -> { - int fontFamily = FontManager.getFontFamily(mContext); - int fontWeight = FontManager.getFontWeight(mContext, FontManager.getIsFontHeavy(mType)); - setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight)); - }, QKPreference.FONT_FAMILY, QKPreference.FONT_WEIGHT); + LiveViewManager.registerView(QKPreference.FONT_FAMILY, this, key -> { + setTypeface(FontManager.getFont(mContext, type)); + }); + + LiveViewManager.registerView(QKPreference.FONT_WEIGHT, this, key -> { + setTypeface(FontManager.getFont(mContext, type)); + }); LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> { setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType));