This commit is contained in:
Moez Bhatti
2016-05-15 17:53:03 -04:00
parent aa272bab66
commit 43a64bbbe8
6 changed files with 44 additions and 41 deletions
@@ -3,6 +3,7 @@ package com.moez.QKSMS.common;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.ui.ThemeManager; 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_DIALOG_BUTTON = 0x7;
public static final int TEXT_TYPE_TOOLBAR = 0x8; 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); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return Integer.parseInt(prefs.getString(SettingsFragment.FONT_FAMILY, "0")); return Integer.parseInt(prefs.getString(SettingsFragment.FONT_FAMILY, "0"));
} }
@@ -115,7 +116,7 @@ public class FontManager {
return ColorStateList.valueOf(ThemeManager.getTextOnBackgroundPrimary()); 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); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int fontWeight = Integer.parseInt(prefs.getString(SettingsFragment.FONT_WEIGHT, "0")); int fontWeight = Integer.parseInt(prefs.getString(SettingsFragment.FONT_WEIGHT, "0"));
int fontFamily = getFontFamily(context); int fontFamily = getFontFamily(context);
@@ -133,4 +134,16 @@ public class FontManager {
return TypefaceManager.TextWeight.BOLD; 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);
}
} }
@@ -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 * 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 * We don't need to manually unregister the views because we're using weak sets
@@ -9,7 +9,6 @@ import com.android.ex.chips.BaseRecipientAdapter;
import com.android.ex.chips.RecipientEditTextView; import com.android.ex.chips.RecipientEditTextView;
import com.moez.QKSMS.common.FontManager; import com.moez.QKSMS.common.FontManager;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.TypefaceManager;
import com.moez.QKSMS.enums.QKPreference; import com.moez.QKSMS.enums.QKPreference;
import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.base.QKActivity; import com.moez.QKSMS.ui.base.QKActivity;
@@ -41,11 +40,13 @@ public class AutoCompleteContactView extends RecipientEditTextView {
setAdapter(mAdapter); setAdapter(mAdapter);
setOnItemClickListener(this); setOnItemClickListener(this);
LiveViewManager.registerView(key -> { LiveViewManager.registerView(QKPreference.FONT_FAMILY, this, key -> {
int fontFamily = FontManager.getFontFamily(mContext); setTypeface(FontManager.getFont(mContext));
int fontWeight = FontManager.getFontWeight(mContext, false); });
setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight));
}, QKPreference.FONT_FAMILY, QKPreference.FONT_WEIGHT); LiveViewManager.registerView(QKPreference.FONT_WEIGHT, this, key -> {
setTypeface(FontManager.getFont(mContext));
});
LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> { LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> {
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY)); setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY));
@@ -10,7 +10,6 @@ import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import com.moez.QKSMS.common.FontManager; import com.moez.QKSMS.common.FontManager;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.TypefaceManager;
import com.moez.QKSMS.enums.QKPreference; import com.moez.QKSMS.enums.QKPreference;
import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.ThemeManager;
@@ -50,11 +49,13 @@ public class QKEditText extends android.widget.EditText {
private void init(Context context) { private void init(Context context) {
mContext = context; mContext = context;
LiveViewManager.registerView(key -> { LiveViewManager.registerView(QKPreference.FONT_FAMILY, this, key -> {
int fontFamily = FontManager.getFontFamily(mContext); setTypeface(FontManager.getFont(mContext));
int fontWeight = FontManager.getFontWeight(mContext, false); });
setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight));
}, QKPreference.FONT_FAMILY, QKPreference.FONT_WEIGHT); LiveViewManager.registerView(QKPreference.FONT_WEIGHT, this, key -> {
setTypeface(FontManager.getFont(mContext));
});
LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> { LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> {
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY)); setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY));
@@ -29,10 +29,15 @@ public class QKSwitch extends SwitchCompat {
private void init(Context context) { private void init(Context context) {
mRes = context.getResources(); mRes = context.getResources();
LiveViewManager.registerView(key -> { LiveViewManager.registerView(QKPreference.THEME, this, key -> {
DrawableCompat.setTintList(getThumbDrawable(), getSwitchThumbColorStateList()); DrawableCompat.setTintList(getThumbDrawable(), getSwitchThumbColorStateList());
DrawableCompat.setTintList(getTrackDrawable(), getSwitchTrackColorStateList()); 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() { private ColorStateList getSwitchThumbColorStateList() {
@@ -13,9 +13,8 @@ import android.widget.TextView;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.common.FontManager; import com.moez.QKSMS.common.FontManager;
import com.moez.QKSMS.common.LiveViewManager; 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.common.utils.TextUtils;
import com.moez.QKSMS.enums.QKPreference;
import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment; import com.moez.QKSMS.ui.settings.SettingsFragment;
@@ -75,11 +74,13 @@ public class QKTextView extends TextView {
setTextColor(FontManager.getTextColor(mContext, mType))); setTextColor(FontManager.getTextColor(mContext, mType)));
} }
LiveViewManager.registerView(key -> { LiveViewManager.registerView(QKPreference.FONT_FAMILY, this, key -> {
int fontFamily = FontManager.getFontFamily(mContext); setTypeface(FontManager.getFont(mContext, type));
int fontWeight = FontManager.getFontWeight(mContext, FontManager.getIsFontHeavy(mType)); });
setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight));
}, QKPreference.FONT_FAMILY, QKPreference.FONT_WEIGHT); LiveViewManager.registerView(QKPreference.FONT_WEIGHT, this, key -> {
setTypeface(FontManager.getFont(mContext, type));
});
LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> { LiveViewManager.registerView(QKPreference.FONT_SIZE, this, key -> {
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType)); setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType));