Rewrote LiveViewManager

Essentially now you’ll be registering blocks of code, rather than
views. Significant performance and code quality improvements
This commit is contained in:
Moez Bhatti
2016-01-02 01:43:14 -05:00
parent f8c9844b00
commit 12a385b8aa
22 changed files with 242 additions and 447 deletions
@@ -3,166 +3,126 @@ package com.moez.QKSMS.common;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.interfaces.LiveView; import com.moez.QKSMS.interfaces.LiveView;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashMap;
import java.util.Set; import java.util.Set;
import java.util.WeakHashMap; import java.util.WeakHashMap;
/** /**
* Allows LiveViews to register for updates on relevant preferences. * Allows views to register for updates on preferences
*
* Example: A button may need to know when the theme changes, so it that
* it can change colors accordingly
*
* In order to do this, you can use this class as following:
* LiveViewManager.registerView(QKPreference.THEME, key -> {
* // Change button color
* }
*
* You won't need to initialize the button color in addition to registering it
* in the LiveViewManager, because registering it will trigger a refresh automatically,
* which will initialize it
*/ */
public class LiveViewManager implements SharedPreferences.OnSharedPreferenceChangeListener { public class LiveViewManager implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "ThemedViewManager";
private static final String TAG = "LiveViewManager";
private static final boolean LOCAL_LOGV = false;
private static LiveViewManager sInstance; private static LiveViewManager sInstance;
private static final HashMap<String, Set<LiveView>> sViews = new HashMap<>();
private static final Set<LiveView> sSet = Collections.newSetFromMap(new WeakHashMap<LiveView, Boolean>());
private static final WeakHashMap<LiveView, Set<String>> sPrefsMap = new WeakHashMap<>();
/** /**
* Private constructor. * Private constructor
*/ */
private LiveViewManager() {} private LiveViewManager() {
}
/**
* Initialize a static instance so that we can listen for views.
*/
static { static {
sInstance = new LiveViewManager(); sInstance = new LiveViewManager();
} }
/**
* Initialize preferences and register a listener for changes
*
* @param context Context
*/
public static void init(Context context) { public static void init(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(sInstance); prefs.registerOnSharedPreferenceChangeListener(sInstance);
} }
/**
* Listen for preference changes from the SharedPreferences
*
* @param prefs SharedPreferences instance
* @param key Key of preference that was changed
*/
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
LiveViewManager.refreshViews(key); LiveViewManager.refreshViews(key);
} }
/** /**
* Registers a LiveView for global updates. To get updates for specific preferences, use * Convenience method for #registerView(QKPreference, ThemedView) to allow registering a single
* registerPreference. * ThemedView to listen for multiple preferences
* @param v
*/
public static void registerView(LiveView v) {
synchronized (sSet) {
sSet.add(v);
}
synchronized(sPrefsMap) {
// Don't add the view to the prefs map more than once, in case the LiveView has already
// been initalized.
if (!sPrefsMap.containsKey(v)) {
sPrefsMap.put(v, new HashSet<String>());
}
}
}
/**
* Unregisters a LiveView for any updates: global, or preference-specific.
* @param v
*/
public static void unregisterView(LiveView v) {
synchronized (sSet) {
sSet.remove(v);
}
synchronized (sPrefsMap) {
sPrefsMap.remove(v);
}
}
/**
* Register a LiveView to be notified when this preference is updated.
* Note that you must first register the view, otherwise #refresh() will
* not be called on it
* *
* @param v * @param view The ThemedView
* @param pref * @param preferences The preferences to listen for
*/ */
public static void registerPreference(LiveView v, String pref) { public static void registerView(LiveView view, QKPreference... preferences) {
synchronized (sPrefsMap) { for (QKPreference preference : preferences) {
Set<String> prefs = sPrefsMap.get(v); registerView(preference, view);
// WeakHashSet: the value might have been removed.
if (prefs != null) {
prefs.add(pref);
}
} }
} }
/** /**
* Register a LiveView to be notified when this preference is updated. * 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
* *
* @param v * @param preference The preference to listen for
* @param pref * @param view The view
*/ */
public static void unregisterPreference(LiveView v, String pref) { public static void registerView(QKPreference preference, LiveView view) {
synchronized (sPrefsMap) { synchronized (sViews) {
Set<String> prefs = sPrefsMap.get(v); if (sViews.containsKey(preference.getKey())) {
// WeakHashSet: the value might have been removed. Set<LiveView> views = sViews.get(preference.getKey());
if (prefs != null) { views.add(view);
prefs.remove(pref); } else {
Set<LiveView> set = Collections.newSetFromMap(new WeakHashMap<>());
set.add(view);
sViews.put(preference.getKey(), set);
} }
} }
// Fire it off once registered
view.refresh(preference.getKey());
} }
/** /**
* Refresh all views. * Refresh all views that are registered to listen for updates to the given preference
* Convenience method for #refreshViews(String key)
*
* @param preference The preference
*/ */
public static void refreshViews() { public static void refreshViews(QKPreference preference) {
synchronized (sSet) { refreshViews(preference.getKey());
for (LiveView view : sSet) {
view.refresh();
}
}
} }
/** /**
* Refreshes only the views that are listening for any of the given preferences. * Refresh all views that are registered to listen for updates to the given preference
* @param query * Convenience method for #refreshViews(String key)
*
* @param key The preference key
*/ */
public static void refreshViews(String... query) { private static void refreshViews(String key) {
Set<LiveView> toRefresh = getViews(query); Set<LiveView> toRefresh = sViews.get(key);
// Refresh those views. // Refresh those views.
for (LiveView view : toRefresh) { if (toRefresh != null) {
view.refresh(); for (LiveView view : toRefresh) {
} view.refresh(key);
}
/**
* Returns the set of LiveViews which subscribe to at least one of the given preferences. Can
* be used to build a quick cache of LiveViews for rapid refreshing, i.e. animations.
*
* @param query
* @return
*/
public static Set<LiveView> getViews(String... query) {
// Build a set of the given preferences.
Set<String> querySet = new HashSet<>();
for (String string : query) {
querySet.add(string);
}
// Get all the views that have at least one of the changed preferences.
Set<LiveView> result = new HashSet<>();
synchronized (sPrefsMap) {
for (LiveView view : sPrefsMap.keySet()) {
Set<String> viewPrefs = sPrefsMap.get(view);
if (viewPrefs != null && !Collections.disjoint(querySet, viewPrefs)) {
result.add(view);
}
} }
} }
if (LOCAL_LOGV) Log.v(TAG, "getViews returned:" + result.size() + " for preferences:" + querySet);
return result;
} }
} }
@@ -1,8 +1,5 @@
package com.moez.QKSMS.interfaces; package com.moez.QKSMS.interfaces;
/**
* A simple interface for views that refresh statically for certain actions.
*/
public interface LiveView { public interface LiveView {
public void refresh(); void refresh(String key);
} }
@@ -29,14 +29,14 @@ import com.moez.QKSMS.R;
import com.moez.QKSMS.common.ConversationPrefsHelper; import com.moez.QKSMS.common.ConversationPrefsHelper;
import com.moez.QKSMS.common.DialogHelper; import com.moez.QKSMS.common.DialogHelper;
import com.moez.QKSMS.common.DonationManager; import com.moez.QKSMS.common.DonationManager;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.QKRateSnack; import com.moez.QKSMS.common.QKRateSnack;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.google.DraftCache; import com.moez.QKSMS.common.google.DraftCache;
import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.common.utils.KeyboardUtils; import com.moez.QKSMS.common.utils.KeyboardUtils;
import com.moez.QKSMS.common.utils.MessageUtils; import com.moez.QKSMS.common.utils.MessageUtils;
import com.moez.QKSMS.common.utils.Units; import com.moez.QKSMS.common.utils.Units;
import com.moez.QKSMS.data.Conversation; import com.moez.QKSMS.data.Conversation;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.mmssms.Utils; import com.moez.QKSMS.mmssms.Utils;
import com.moez.QKSMS.receiver.IconColorReceiver; import com.moez.QKSMS.receiver.IconColorReceiver;
import com.moez.QKSMS.transaction.NotificationManager; import com.moez.QKSMS.transaction.NotificationManager;
@@ -61,7 +61,7 @@ import java.net.URLDecoder;
import java.util.Collection; import java.util.Collection;
public class MainActivity extends QKActivity implements LiveView, SlidingMenu.SlidingMenuListener { public class MainActivity extends QKActivity implements SlidingMenu.SlidingMenuListener {
private final String TAG = "MainActivity"; private final String TAG = "MainActivity";
@@ -122,8 +122,13 @@ public class MainActivity extends QKActivity implements LiveView, SlidingMenu.Sl
showDialogIfNeeded(savedInstanceState); showDialogIfNeeded(savedInstanceState);
LiveViewManager.registerView(this); LiveViewManager.registerView(QKPreference.BACKGROUND, key -> {
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND); // Update the background color. This code is important during the welcome screen setup, when the activity
// in the ThemeManager isn't the MainActivity
findViewById(R.id.menu_frame).getRootView().setBackgroundColor(ThemeManager.getBackgroundColor());
findViewById(R.id.menu_frame).setBackgroundColor(ThemeManager.getBackgroundColor());
findViewById(R.id.content_frame).setBackgroundColor(ThemeManager.getBackgroundColor());
});
//Adds a small/non intrusive snackbar that asks the user to rate the app //Adds a small/non intrusive snackbar that asks the user to rate the app
SnackEngage.from(this).withSnack(new QKRateSnack().withDuration(BaseSnack.DURATION_LONG)) SnackEngage.from(this).withSnack(new QKRateSnack().withDuration(BaseSnack.DURATION_LONG))
@@ -644,15 +649,6 @@ public class MainActivity extends QKActivity implements LiveView, SlidingMenu.Sl
.show(); .show();
} }
@Override
public void refresh() {
// Update the background color. This code is important during the welcome screen setup, when the activity
// in the ThemeManager isn't the MainActivity
findViewById(R.id.menu_frame).getRootView().setBackgroundColor(ThemeManager.getBackgroundColor());
findViewById(R.id.menu_frame).setBackgroundColor(ThemeManager.getBackgroundColor());
findViewById(R.id.content_frame).setBackgroundColor(ThemeManager.getBackgroundColor());
}
public static class DeleteThreadListener implements DialogInterface.OnClickListener { public static class DeleteThreadListener implements DialogInterface.OnClickListener {
private final Collection<Long> mThreadIds; private final Collection<Long> mThreadIds;
private final Conversation.ConversationQueryHandler mHandler; private final Conversation.ConversationQueryHandler mHandler;
@@ -26,7 +26,6 @@ import com.moez.QKSMS.common.AnalyticsManager;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.preferences.QKPreference; import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.common.utils.ColorUtils; import com.moez.QKSMS.common.utils.ColorUtils;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.receiver.IconColorReceiver; import com.moez.QKSMS.receiver.IconColorReceiver;
import com.moez.QKSMS.ui.base.QKActivity; import com.moez.QKSMS.ui.base.QKActivity;
import com.moez.QKSMS.ui.dialog.QKDialog; import com.moez.QKSMS.ui.dialog.QKDialog;
@@ -35,8 +34,6 @@ import com.moez.QKSMS.ui.view.QKTextView;
import com.moez.QKSMS.ui.view.colorpicker.ColorPickerPalette; import com.moez.QKSMS.ui.view.colorpicker.ColorPickerPalette;
import com.moez.QKSMS.ui.widget.WidgetProvider; import com.moez.QKSMS.ui.widget.WidgetProvider;
import java.util.Set;
public class ThemeManager { public class ThemeManager {
private final static String TAG = "ThemeManager"; private final static String TAG = "ThemeManager";
@@ -306,14 +303,14 @@ public class ThemeManager {
@Override @Override
public void onAnimationEnd(Animator animation) { public void onAnimationEnd(Animator animation) {
// This updates the colors and fonts of all the views. // This updates the colors and fonts of all the views.
LiveViewManager.refreshViews(SettingsFragment.BACKGROUND); LiveViewManager.refreshViews(QKPreference.BACKGROUND);
WidgetProvider.notifyThemeChanged(mContext); WidgetProvider.notifyThemeChanged(mContext);
} }
}); });
backgroundAnimation.start(); backgroundAnimation.start();
} else { } else {
// This updates the colors and fonts of all the views. // This updates the colors and fonts of all the views.
LiveViewManager.refreshViews(SettingsFragment.BACKGROUND); LiveViewManager.refreshViews(QKPreference.BACKGROUND);
background.setBackgroundColor(endColor); background.setBackgroundColor(endColor);
menu.setBackgroundColor(endColor); menu.setBackgroundColor(endColor);
content.setBackgroundColor(endColor); content.setBackgroundColor(endColor);
@@ -321,7 +318,7 @@ public class ThemeManager {
} }
} else { } else {
// This updates the colors and fonts of all the views. // This updates the colors and fonts of all the views.
LiveViewManager.refreshViews(SettingsFragment.BACKGROUND); LiveViewManager.refreshViews(QKPreference.BACKGROUND);
WidgetProvider.notifyThemeChanged(mContext); WidgetProvider.notifyThemeChanged(mContext);
} }
} }
@@ -639,12 +636,7 @@ public class ThemeManager {
// Some views are updated every frame of the animation; get these views here. We // Some views are updated every frame of the animation; get these views here. We
// build this list once beforehand since it's a slightly expensive operation. // build this list once beforehand since it's a slightly expensive operation.
final Set<LiveView> views = LiveViewManager.getViews(SettingsFragment.THEME); LiveViewManager.refreshViews(QKPreference.THEME);
// Refresh all the views with the new color.
for (LiveView view : views) {
view.refresh();
}
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colourFrom, color); ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colourFrom, color);
colorAnimation.setDuration(TRANSITION_LENGTH); colorAnimation.setDuration(TRANSITION_LENGTH);
@@ -705,9 +697,7 @@ public class ThemeManager {
mWindow.setNavigationBarColor(ColorUtils.darken(color)); mWindow.setNavigationBarColor(ColorUtils.darken(color));
} }
for (LiveView view : LiveViewManager.getViews(QKPreference.ACTIVE_THEME.getKey())) { LiveViewManager.refreshViews(QKPreference.ACTIVE_THEME);
view.refresh();
}
} }
private static boolean isColorDarkEnough(int color) { private static boolean isColorDarkEnough(int color) {
@@ -1,12 +1,7 @@
package com.moez.QKSMS.ui.base; package com.moez.QKSMS.ui.base;
import android.os.Bundle; import android.os.Bundle;
import android.view.View;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.ui.ContentFragment; import com.moez.QKSMS.ui.ContentFragment;
import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment;
/** /**
* Base class for Fragments. QKSMS uses the recycle pattern as an optimization, and our fragment * Base class for Fragments. QKSMS uses the recycle pattern as an optimization, and our fragment
@@ -22,7 +17,7 @@ import com.moez.QKSMS.ui.settings.SettingsFragment;
* - Manages the current state of the fragment in terms of the animation, i.e. opening, fully * - Manages the current state of the fragment in terms of the animation, i.e. opening, fully
* opened, etc. * opened, etc.
*/ */
public abstract class QKContentFragment extends QKFragment implements LiveView, ContentFragment { public abstract class QKContentFragment extends QKFragment implements ContentFragment {
/** /**
* It's not strictly necessary, but subclasses should call through to super() in their * It's not strictly necessary, but subclasses should call through to super() in their
@@ -60,18 +55,6 @@ public abstract class QKContentFragment extends QKFragment implements LiveView,
onNewArguments(); onNewArguments();
} }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
refresh();
}
/** /**
* Called when the content is being opened with an animation. * Called when the content is being opened with an animation.
*/ */
@@ -97,12 +80,4 @@ public abstract class QKContentFragment extends QKFragment implements LiveView,
*/ */
public void onContentClosed() { public void onContentClosed() {
} }
@Override
public void refresh() {
View view = getView();
if (view != null) {
view.setBackgroundColor(ThemeManager.getBackgroundColor());
}
}
} }
@@ -3,14 +3,14 @@ package com.moez.QKSMS.ui.base;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.os.Bundle; import android.os.Bundle;
import android.view.View;
import com.moez.QKSMS.QKSMSApp; import com.moez.QKSMS.QKSMSApp;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.interfaces.LiveView; import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment;
import com.squareup.leakcanary.RefWatcher; import com.squareup.leakcanary.RefWatcher;
public class QKFragment extends Fragment implements LiveView { public class QKFragment extends Fragment {
protected QKActivity mContext; protected QKActivity mContext;
@@ -21,10 +21,14 @@ public class QKFragment extends Fragment implements LiveView {
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onViewCreated(view, savedInstanceState);
LiveViewManager.registerView(this);
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND); LiveViewManager.registerView(QKPreference.BACKGROUND, key -> {
if (getView() != null) {
getView().setBackgroundColor(ThemeManager.getBackgroundColor());
}
});
} }
@Override @Override
@@ -33,11 +37,4 @@ public class QKFragment extends Fragment implements LiveView {
RefWatcher refWatcher = QKSMSApp.getRefWatcher(getActivity()); RefWatcher refWatcher = QKSMSApp.getRefWatcher(getActivity());
refWatcher.watch(this); refWatcher.watch(this);
} }
@Override
public void refresh() {
if (getView() != null) {
getView().setBackgroundColor(ThemeManager.getBackgroundColor());
}
}
} }
@@ -11,17 +11,16 @@ import com.moez.QKSMS.R;
import com.moez.QKSMS.common.ConversationPrefsHelper; import com.moez.QKSMS.common.ConversationPrefsHelper;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.emoji.EmojiRegistry; import com.moez.QKSMS.common.emoji.EmojiRegistry;
import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.common.utils.DateFormatter; import com.moez.QKSMS.common.utils.DateFormatter;
import com.moez.QKSMS.data.Contact; import com.moez.QKSMS.data.Contact;
import com.moez.QKSMS.data.Conversation; import com.moez.QKSMS.data.Conversation;
import com.moez.QKSMS.interfaces.LiveView;
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;
import com.moez.QKSMS.ui.base.RecyclerCursorAdapter; import com.moez.QKSMS.ui.base.RecyclerCursorAdapter;
import com.moez.QKSMS.ui.settings.SettingsFragment; import com.moez.QKSMS.ui.settings.SettingsFragment;
public class ConversationListAdapter extends RecyclerCursorAdapter<ConversationListViewHolder, Conversation> public class ConversationListAdapter extends RecyclerCursorAdapter<ConversationListViewHolder, Conversation> {
implements LiveView {
private final SharedPreferences mPrefs; private final SharedPreferences mPrefs;
@@ -38,9 +37,12 @@ public class ConversationListAdapter extends RecyclerCursorAdapter<ConversationL
mUnread = ContextCompat.getDrawable(context, R.drawable.ic_unread_indicator); mUnread = ContextCompat.getDrawable(context, R.drawable.ic_unread_indicator);
mError = ContextCompat.getDrawable(context, R.drawable.ic_error); mError = ContextCompat.getDrawable(context, R.drawable.ic_error);
LiveViewManager.registerView(this); LiveViewManager.registerView(QKPreference.THEME, key -> {
LiveViewManager.registerPreference(this, SettingsFragment.THEME); mMuted.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
refresh(); mUnread.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
mError.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
notifyDataSetChanged();
});
} }
protected Conversation getItem(int position) { protected Conversation getItem(int position) {
@@ -124,12 +126,4 @@ public class ConversationListAdapter extends RecyclerCursorAdapter<ConversationL
// Update the avatar and name // Update the avatar and name
holder.onUpdate(conversation.getRecipients().size() == 1 ? conversation.getRecipients().get(0) : null); holder.onUpdate(conversation.getRecipients().size() == 1 ? conversation.getRecipients().get(0) : null);
} }
@Override
public void refresh() {
mMuted.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
mUnread.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
mError.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
notifyDataSetChanged();
}
} }
@@ -27,10 +27,10 @@ import com.moez.QKSMS.common.BlockedConversationHelper;
import com.moez.QKSMS.common.DialogHelper; import com.moez.QKSMS.common.DialogHelper;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.conversationdetails.ConversationDetailsDialog; import com.moez.QKSMS.common.conversationdetails.ConversationDetailsDialog;
import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.common.utils.ColorUtils; import com.moez.QKSMS.common.utils.ColorUtils;
import com.moez.QKSMS.data.Conversation; import com.moez.QKSMS.data.Conversation;
import com.moez.QKSMS.data.ConversationLegacy; import com.moez.QKSMS.data.ConversationLegacy;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.transaction.SmsHelper; import com.moez.QKSMS.transaction.SmsHelper;
import com.moez.QKSMS.ui.ContentFragment; import com.moez.QKSMS.ui.ContentFragment;
import com.moez.QKSMS.ui.MainActivity; import com.moez.QKSMS.ui.MainActivity;
@@ -44,7 +44,7 @@ import java.util.Observable;
import java.util.Observer; import java.util.Observer;
public class ConversationListFragment extends QKFragment implements LoaderManager.LoaderCallbacks<Cursor>, LiveView, public class ConversationListFragment extends QKFragment implements LoaderManager.LoaderCallbacks<Cursor>,
RecyclerCursorAdapter.ItemClickListener<Conversation>, RecyclerCursorAdapter.MultiSelectListener, Observer { RecyclerCursorAdapter.ItemClickListener<Conversation>, RecyclerCursorAdapter.MultiSelectListener, Observer {
private final String TAG = "ConversationList"; private final String TAG = "ConversationList";
@@ -79,8 +79,17 @@ public class ConversationListFragment extends QKFragment implements LoaderManage
mLayoutManager = new LinearLayoutManager(mContext); mLayoutManager = new LinearLayoutManager(mContext);
mConversationDetailsDialog = new ConversationDetailsDialog(mContext, getFragmentManager()); mConversationDetailsDialog = new ConversationDetailsDialog(mContext, getFragmentManager());
LiveViewManager.registerView(this); LiveViewManager.registerView(QKPreference.THEME, key -> {
LiveViewManager.registerPreference(this, SettingsFragment.THEME); if (!mViewHasLoaded) {
return;
}
mFab.setColorNormal(ThemeManager.getColor());
mFab.setColorPressed(ColorUtils.lighten(ThemeManager.getColor()));
mFab.getDrawable().setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
mEmptyStateIcon.setColorFilter(ThemeManager.getTextOnBackgroundPrimary());
});
} }
@Override @Override
@@ -272,7 +281,6 @@ public class ConversationListFragment extends QKFragment implements LoaderManage
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
LiveViewManager.unregisterView(this);
BlockedConversationHelper.FutureBlockedConversationObservable.getInstance().deleteObserver(this); BlockedConversationHelper.FutureBlockedConversationObservable.getInstance().deleteObserver(this);
} }
@@ -301,19 +309,6 @@ public class ConversationListFragment extends QKFragment implements LoaderManage
} }
} }
@Override
public void refresh() {
if (!mViewHasLoaded) {
return;
}
mFab.setColorNormal(ThemeManager.getColor());
mFab.setColorPressed(ColorUtils.lighten(ThemeManager.getColor()));
mFab.getDrawable().setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
mEmptyStateIcon.setColorFilter(ThemeManager.getTextOnBackgroundPrimary());
}
@Override @Override
public void onMultiSelectStateChanged(boolean enabled) { public void onMultiSelectStateChanged(boolean enabled) {
mContext.invalidateOptionsMenu(); mContext.invalidateOptionsMenu();
@@ -68,7 +68,6 @@ public class ConversationNotificationSettingsDialog extends QKDialog implements
int padding = Units.dpToPx(getActivity(), 16); int padding = Units.dpToPx(getActivity(), 16);
QKTextView premiumWarning = new QKTextView(getActivity()); QKTextView premiumWarning = new QKTextView(getActivity());
premiumWarning.setLayoutParams(mLayoutParams); premiumWarning.setLayoutParams(mLayoutParams);
premiumWarning.setType(FontManager.TEXT_TYPE_PRIMARY);
premiumWarning.setPadding(padding, padding, padding, padding); premiumWarning.setPadding(padding, padding, padding, padding);
LinearLayout list = new LinearLayout(getActivity()); LinearLayout list = new LinearLayout(getActivity());
@@ -14,16 +14,12 @@ import android.telephony.TelephonyManager;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan; import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import com.android.mms.transaction.Transaction; import com.android.mms.transaction.Transaction;
import com.android.mms.transaction.TransactionBundle; import com.android.mms.transaction.TransactionBundle;
import com.android.mms.transaction.TransactionService; import com.android.mms.transaction.TransactionService;
@@ -44,7 +40,6 @@ import com.moez.QKSMS.ui.base.RecyclerCursorAdapter;
import com.moez.QKSMS.ui.mms.MmsThumbnailPresenter; import com.moez.QKSMS.ui.mms.MmsThumbnailPresenter;
import com.moez.QKSMS.ui.settings.SettingsFragment; import com.moez.QKSMS.ui.settings.SettingsFragment;
import com.moez.QKSMS.ui.view.AvatarView; import com.moez.QKSMS.ui.view.AvatarView;
import ezvcard.Ezvcard; import ezvcard.Ezvcard;
import ezvcard.VCard; import ezvcard.VCard;
@@ -312,14 +307,13 @@ public class MessageListAdapter extends RecyclerCursorAdapter<MessageListViewHol
ThemeManager.getReceivedBubbleRes()) : (messageItem.isMe() ? ThemeManager.getReceivedBubbleRes()) : (messageItem.isMe() ?
ThemeManager.getSentBubbleAltRes() : ThemeManager.getReceivedBubbleAltRes())); ThemeManager.getSentBubbleAltRes() : ThemeManager.getReceivedBubbleAltRes()));
holder.setLiveViewCallback(() -> { holder.setLiveViewCallback(key -> {
if (messageItem.isMe()) { if (messageItem.isMe()) {
holder.mBodyTextView.getBackground().setColorFilter(ThemeManager.getSentBubbleColor(), PorterDuff.Mode.SRC_ATOP); holder.mBodyTextView.getBackground().setColorFilter(ThemeManager.getSentBubbleColor(), PorterDuff.Mode.SRC_ATOP);
} else { } else {
holder.mBodyTextView.getBackground().setColorFilter(ThemeManager.getReceivedBubbleColor(), PorterDuff.Mode.SRC_ATOP); holder.mBodyTextView.getBackground().setColorFilter(ThemeManager.getReceivedBubbleColor(), PorterDuff.Mode.SRC_ATOP);
} }
}); });
holder.refresh();
if (messageItem.isMe() && !mPrefs.getBoolean(SettingsFragment.HIDE_AVATAR_SENT, true)) { if (messageItem.isMe() && !mPrefs.getBoolean(SettingsFragment.HIDE_AVATAR_SENT, true)) {
holder.mAvatarView.setVisibility(showAvatar ? View.VISIBLE : View.GONE); holder.mAvatarView.setVisibility(showAvatar ? View.VISIBLE : View.GONE);
@@ -13,8 +13,8 @@ import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.google.ItemLoadedCallback; import com.moez.QKSMS.common.google.ItemLoadedCallback;
import com.moez.QKSMS.common.google.ThumbnailManager; import com.moez.QKSMS.common.google.ThumbnailManager;
import com.moez.QKSMS.common.preferences.QKPreference; import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.interfaces.SlideViewInterface; import com.moez.QKSMS.interfaces.SlideViewInterface;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.ui.base.ClickyViewHolder; import com.moez.QKSMS.ui.base.ClickyViewHolder;
import com.moez.QKSMS.ui.base.QKActivity; import com.moez.QKSMS.ui.base.QKActivity;
import com.moez.QKSMS.ui.mms.Presenter; import com.moez.QKSMS.ui.mms.Presenter;
@@ -23,13 +23,9 @@ import com.moez.QKSMS.ui.view.QKTextView;
import java.util.Map; import java.util.Map;
public class MessageListViewHolder extends ClickyViewHolder<MessageItem> implements SlideViewInterface, LiveView { public class MessageListViewHolder extends ClickyViewHolder<MessageItem> implements SlideViewInterface {
private final String TAG = "MessageListViewHolder"; private final String TAG = "MessageListViewHolder";
protected interface LiveViewCallback {
void onRefresh();
}
// Views // Views
protected View mRoot; protected View mRoot;
protected QKTextView mBodyTextView; protected QKTextView mBodyTextView;
@@ -51,8 +47,6 @@ public class MessageListViewHolder extends ClickyViewHolder<MessageItem> impleme
protected ImageLoadedCallback mImageLoadedCallback; protected ImageLoadedCallback mImageLoadedCallback;
protected Presenter mPresenter; protected Presenter mPresenter;
protected LiveViewCallback mLiveViewCallback;
public MessageListViewHolder(QKActivity context, View view) { public MessageListViewHolder(QKActivity context, View view) {
super(context, view); super(context, view);
@@ -65,9 +59,6 @@ public class MessageListViewHolder extends ClickyViewHolder<MessageItem> impleme
mAvatarView = (AvatarView) view.findViewById(R.id.avatar); mAvatarView = (AvatarView) view.findViewById(R.id.avatar);
mMessageBlock = (LinearLayout) view.findViewById(R.id.message_block); mMessageBlock = (LinearLayout) view.findViewById(R.id.message_block);
mSpace = view.findViewById(R.id.space); mSpace = view.findViewById(R.id.space);
LiveViewManager.registerView(this);
LiveViewManager.registerPreference(this, QKPreference.ACTIVE_THEME.getKey());
} }
protected void showMmsView(boolean visible) { protected void showMmsView(boolean visible) {
@@ -102,8 +93,8 @@ public class MessageListViewHolder extends ClickyViewHolder<MessageItem> impleme
} }
} }
protected void setLiveViewCallback(LiveViewCallback liveViewCallback) { protected void setLiveViewCallback(LiveView liveViewCallback) {
mLiveViewCallback = liveViewCallback; LiveViewManager.registerView(QKPreference.ACTIVE_THEME, liveViewCallback);
} }
@Override @Override
@@ -225,13 +216,6 @@ public class MessageListViewHolder extends ClickyViewHolder<MessageItem> impleme
} }
@Override
public void refresh() {
if (mLiveViewCallback != null) {
mLiveViewCallback.onRefresh();
}
}
static protected class ImageLoadedCallback implements ItemLoadedCallback<ThumbnailManager.ImageLoaded> { static protected class ImageLoadedCallback implements ItemLoadedCallback<ThumbnailManager.ImageLoaded> {
private long mMessageId; private long mMessageId;
private final MessageListViewHolder mListItem; private final MessageListViewHolder mListItem;
@@ -72,7 +72,6 @@ public class QKReplyActivity extends QKPopupActivity implements DialogInterface.
mComposeView.setActivityLauncher(this); mComposeView.setActivityLauncher(this);
mComposeView.setOnSendListener(this); mComposeView.setOnSendListener(this);
mComposeView.setLabel("QKReply"); mComposeView.setLabel("QKReply");
mComposeView.refresh();
mAdapter = new QKReplyAdapter(this); mAdapter = new QKReplyAdapter(this);
@@ -36,10 +36,10 @@ import com.moez.QKSMS.common.DialogHelper;
import com.moez.QKSMS.common.DonationManager; import com.moez.QKSMS.common.DonationManager;
import com.moez.QKSMS.common.ListviewHelper; import com.moez.QKSMS.common.ListviewHelper;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.common.utils.DateFormatter; import com.moez.QKSMS.common.utils.DateFormatter;
import com.moez.QKSMS.common.utils.KeyboardUtils; import com.moez.QKSMS.common.utils.KeyboardUtils;
import com.moez.QKSMS.common.utils.PackageUtils; import com.moez.QKSMS.common.utils.PackageUtils;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.receiver.NightModeAutoReceiver; import com.moez.QKSMS.receiver.NightModeAutoReceiver;
import com.moez.QKSMS.transaction.EndlessJabber; import com.moez.QKSMS.transaction.EndlessJabber;
import com.moez.QKSMS.transaction.NotificationManager; import com.moez.QKSMS.transaction.NotificationManager;
@@ -66,7 +66,7 @@ import java.util.Set;
import java.util.Stack; import java.util.Stack;
public class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener, public class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener,
Preference.OnPreferenceClickListener, LiveView, ContentFragment { Preference.OnPreferenceClickListener, ContentFragment {
private final String TAG = "PreferenceFragment"; private final String TAG = "PreferenceFragment";
public static final String CATEGORY_APPEARANCE = "pref_key_category_appearance"; public static final String CATEGORY_APPEARANCE = "pref_key_category_appearance";
@@ -368,9 +368,14 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
KeyboardUtils.hide(mContext, view); KeyboardUtils.hide(mContext, view);
mListView = (ListView) view.findViewById(android.R.id.list); mListView = (ListView) view.findViewById(android.R.id.list);
LiveViewManager.registerView(this); LiveViewManager.registerView(QKPreference.BACKGROUND, key -> {
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND); ListviewHelper.applyCustomScrollbar(mContext, mListView);
refresh();
View view1 = getView();
if (view1 != null) {
view1.setBackgroundColor(ThemeManager.getBackgroundColor());
}
});
} }
@Override @Override
@@ -688,23 +693,6 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
} }
} }
@Override
public void onDestroy() {
super.onDestroy();
LiveViewManager.unregisterView(this);
}
@Override
public void refresh() {
ListviewHelper.applyCustomScrollbar(mContext, mListView);
View view = getView();
if (view != null) {
view.setBackgroundColor(ThemeManager.getBackgroundColor());
}
}
@Override @Override
public void onContentOpening() { public void onContentOpening() {
@@ -10,12 +10,12 @@ 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.common.TypefaceManager;
import com.moez.QKSMS.interfaces.LiveView; import com.moez.QKSMS.common.preferences.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;
import com.moez.QKSMS.ui.settings.SettingsFragment; import com.moez.QKSMS.ui.settings.SettingsFragment;
public class AutoCompleteContactView extends RecipientEditTextView implements LiveView { public class AutoCompleteContactView extends RecipientEditTextView {
public static final String TAG = "AutoCompleteContactView"; public static final String TAG = "AutoCompleteContactView";
private QKActivity mContext; private QKActivity mContext;
@@ -33,40 +33,34 @@ public class AutoCompleteContactView extends RecipientEditTextView implements Li
private void init(Context context) { private void init(Context context) {
mContext = (QKActivity) context; mContext = (QKActivity) context;
SharedPreferences prefs = mContext.getPrefs();
// Setup text size, typeface, etc.
refresh();
mAdapter = new BaseRecipientAdapter(BaseRecipientAdapter.QUERY_TYPE_PHONE, getContext()); mAdapter = new BaseRecipientAdapter(BaseRecipientAdapter.QUERY_TYPE_PHONE, getContext());
mAdapter.setShowMobileOnly(prefs.getBoolean(SettingsFragment.MOBILE_ONLY, false));
setThreshold(1); setThreshold(1);
setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
setAdapter(mAdapter); setAdapter(mAdapter);
setOnItemClickListener(this); setOnItemClickListener(this);
// Register this view for live updates. LiveViewManager.registerView(key -> {
LiveViewManager.registerView(this); int fontFamily = FontManager.getFontFamily(mContext);
LiveViewManager.registerPreference(this, SettingsFragment.FONT_FAMILY); int fontWeight = FontManager.getFontWeight(mContext, false);
LiveViewManager.registerPreference(this, SettingsFragment.FONT_SIZE); setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight));
LiveViewManager.registerPreference(this, SettingsFragment.FONT_WEIGHT); }, QKPreference.FONT_FAMILY, QKPreference.FONT_WEIGHT);
LiveViewManager.registerPreference(this, SettingsFragment.MOBILE_ONLY);
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND);
}
@Override LiveViewManager.registerView(QKPreference.FONT_SIZE, key -> {
public void refresh() { setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY));
setTypeface(TypefaceManager.obtainTypeface(mContext, FontManager.getFontFamily(mContext), });
FontManager.getFontWeight(mContext, false)));
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY));
setTextColor(ThemeManager.getTextOnBackgroundPrimary()); LiveViewManager.registerView(QKPreference.BACKGROUND, key -> {
setHintTextColor(ThemeManager.getTextOnBackgroundSecondary()); setTextColor(ThemeManager.getTextOnBackgroundPrimary());
setHintTextColor(ThemeManager.getTextOnBackgroundSecondary());
});
if (mAdapter != null) { LiveViewManager.registerView(QKPreference.MOBILE_ONLY, key -> {
SharedPreferences prefs = mContext.getPrefs(); if (mAdapter != null) {
mAdapter.setShowMobileOnly(prefs.getBoolean(SettingsFragment.MOBILE_ONLY, false)); SharedPreferences prefs1 = mContext.getPrefs();
} mAdapter.setShowMobileOnly(prefs1.getBoolean(SettingsFragment.MOBILE_ONLY, false));
}
});
} }
} }
@@ -28,10 +28,9 @@ import com.moez.QKSMS.common.TypefaceManager;
import com.moez.QKSMS.common.preferences.QKPreference; import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.common.utils.ImageUtils; import com.moez.QKSMS.common.utils.ImageUtils;
import com.moez.QKSMS.common.utils.Units; import com.moez.QKSMS.common.utils.Units;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.ThemeManager;
public class AvatarView extends ImageView implements View.OnClickListener, LiveView { public class AvatarView extends ImageView implements View.OnClickListener {
private final String TAG = "AvatarView"; private final String TAG = "AvatarView";
static final String[] PHONE_LOOKUP_PROJECTION = new String[]{ static final String[] PHONE_LOOKUP_PROJECTION = new String[]{
@@ -90,12 +89,17 @@ public class AvatarView extends ImageView implements View.OnClickListener, LiveV
} }
a.recycle(); a.recycle();
refresh();
setOnClickListener(this); setOnClickListener(this);
// Register this view for live updates. LiveViewManager.registerView(QKPreference.ACTIVE_THEME, key -> {
LiveViewManager.registerView(this); mPaint.setColor(ThemeManager.getTextOnColorPrimary());
LiveViewManager.registerPreference(this, QKPreference.ACTIVE_THEME.getKey()); mDefaultDrawable.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
if (getBackground() == null) {
setBackgroundResource(R.drawable.circle);
}
getBackground().setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
});
} }
} }
@@ -277,17 +281,6 @@ public class AvatarView extends ImageView implements View.OnClickListener, LiveV
} }
} }
@Override
public void refresh() {
mPaint.setColor(ThemeManager.getTextOnColorPrimary());
mDefaultDrawable.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
if (getBackground() == null) {
setBackgroundResource(R.drawable.circle);
}
getBackground().setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
}
private class QueryHandler extends AsyncQueryHandler { private class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) { public QueryHandler(ContentResolver cr) {
@@ -36,6 +36,7 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import com.github.lzyzsd.circleprogress.DonutProgress; import com.github.lzyzsd.circleprogress.DonutProgress;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.preferences.QKPreference; import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.mmssms.Transaction; import com.moez.QKSMS.mmssms.Transaction;
import com.moez.QKSMS.mmssms.Utils; import com.moez.QKSMS.mmssms.Utils;
@@ -44,9 +45,7 @@ import com.moez.QKSMS.common.AnalyticsManager;
import com.moez.QKSMS.data.Conversation; import com.moez.QKSMS.data.Conversation;
import com.moez.QKSMS.data.ConversationLegacy; import com.moez.QKSMS.data.ConversationLegacy;
import com.moez.QKSMS.interfaces.ActivityLauncher; import com.moez.QKSMS.interfaces.ActivityLauncher;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.interfaces.RecipientProvider; import com.moez.QKSMS.interfaces.RecipientProvider;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.utils.ImageUtils; import com.moez.QKSMS.common.utils.ImageUtils;
import com.moez.QKSMS.common.utils.PhoneNumberUtils; import com.moez.QKSMS.common.utils.PhoneNumberUtils;
import com.moez.QKSMS.common.utils.Units; import com.moez.QKSMS.common.utils.Units;
@@ -66,7 +65,7 @@ import java.io.InputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
public class ComposeView extends LinearLayout implements View.OnClickListener, LiveView { public class ComposeView extends LinearLayout implements View.OnClickListener {
public final static String TAG = "ComposeView"; public final static String TAG = "ComposeView";
private final String KEY_DELAYED_INFO_DIALOG_SHOWN = "delayed_info_dialog_shown"; private final String KEY_DELAYED_INFO_DIALOG_SHOWN = "delayed_info_dialog_shown";
@@ -182,11 +181,23 @@ public class ComposeView extends LinearLayout implements View.OnClickListener, L
mCancel.setOnClickListener(this); mCancel.setOnClickListener(this);
mDelay.setOnClickListener(this); mDelay.setOnClickListener(this);
LiveViewManager.registerView(this); LiveViewManager.registerView(QKPreference.ACTIVE_THEME, key -> {
LiveViewManager.registerPreference(this, QKPreference.ACTIVE_THEME.getKey()); mButtonBackground.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND); mButtonBar1.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
mButtonBar2.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
mAttachmentPanel.setBackgroundColor(ThemeManager.getColor());
mAttach.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
mCamera.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
updateDelayButton();
mProgress.setUnfinishedStrokeColor(ThemeManager.getTextOnColorSecondary());
mProgress.setFinishedStrokeColor(ThemeManager.getTextOnColorPrimary());
if (ThemeManager.getSentBubbleRes() != 0) mReplyText.setBackgroundResource(ThemeManager.getSentBubbleRes());
});
refresh(); LiveViewManager.registerView(QKPreference.BACKGROUND, key -> {
mReplyText.getBackground().setColorFilter(ThemeManager.getNeutralBubbleColor(), PorterDuff.Mode.SRC_ATOP);
getBackground().setColorFilter(ThemeManager.getBackgroundColor(), PorterDuff.Mode.SRC_ATOP);
});
// There is an option for using the return button instead of the emoticon button in the // There is an option for using the return button instead of the emoticon button in the
// keyboard; set that up here. // keyboard; set that up here.
@@ -949,23 +960,6 @@ public class ComposeView extends LinearLayout implements View.OnClickListener, L
} }
} }
@Override
public void refresh() {
mButtonBackground.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.SRC_ATOP);
mButtonBar1.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
mButtonBar2.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
mAttachmentPanel.setBackgroundColor(ThemeManager.getColor());
mAttach.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
mCamera.setColorFilter(ThemeManager.getTextOnColorPrimary(), PorterDuff.Mode.SRC_ATOP);
updateDelayButton();
mProgress.setUnfinishedStrokeColor(ThemeManager.getTextOnColorSecondary());
mProgress.setFinishedStrokeColor(ThemeManager.getTextOnColorPrimary());
if (ThemeManager.getSentBubbleRes() != 0) mReplyText.setBackgroundResource(ThemeManager.getSentBubbleRes());
mReplyText.getBackground().setColorFilter(ThemeManager.getNeutralBubbleColor(), PorterDuff.Mode.SRC_ATOP);
mReplyText.refresh();
getBackground().setColorFilter(ThemeManager.getBackgroundColor(), PorterDuff.Mode.SRC_ATOP);
}
private void updateDelayButton() { private void updateDelayButton() {
mDelay.setColorFilter(mDelayedMessagingEnabled ? mDelay.setColorFilter(mDelayedMessagingEnabled ?
ThemeManager.getTextOnColorPrimary() : ThemeManager.getTextOnColorSecondary(), ThemeManager.getTextOnColorPrimary() : ThemeManager.getTextOnColorSecondary(),
@@ -8,14 +8,13 @@ import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import com.moez.QKSMS.interfaces.LiveView;
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.common.TypefaceManager;
import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment;
public class QKEditText extends android.widget.EditText implements LiveView { public class QKEditText extends android.widget.EditText {
public static final String TAG = "QKEditText"; public static final String TAG = "QKEditText";
public interface TextChangedListener { public interface TextChangedListener {
@@ -28,7 +27,7 @@ public class QKEditText extends android.widget.EditText implements LiveView {
super(context); super(context);
if (!isInEditMode()) { if (!isInEditMode()) {
init(context, null); init(context);
} }
} }
@@ -36,7 +35,7 @@ public class QKEditText extends android.widget.EditText implements LiveView {
super(context, attrs); super(context, attrs);
if (!isInEditMode()) { if (!isInEditMode()) {
init(context, attrs); init(context);
} }
} }
@@ -44,24 +43,29 @@ public class QKEditText extends android.widget.EditText implements LiveView {
super(context, attrs, defStyle); super(context, attrs, defStyle);
if (!isInEditMode()) { if (!isInEditMode()) {
init(context, attrs); init(context);
} }
} }
private void init(Context context, AttributeSet attrs) { private void init(Context context) {
mContext = context; mContext = context;
// Load the properties LiveViewManager.registerView(key -> {
refresh(); 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_SIZE, key -> {
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY));
});
LiveViewManager.registerView(QKPreference.BACKGROUND, key -> {
setTextColor(ThemeManager.getTextOnBackgroundPrimary());
setHintTextColor(ThemeManager.getTextOnBackgroundSecondary());
});
setText(getText()); setText(getText());
// Register this view for live updates.
LiveViewManager.registerView(this);
LiveViewManager.registerPreference(this, SettingsFragment.FONT_FAMILY);
LiveViewManager.registerPreference(this, SettingsFragment.FONT_SIZE);
LiveViewManager.registerPreference(this, SettingsFragment.FONT_WEIGHT);
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND);
} }
@Override @Override
@@ -91,19 +95,4 @@ public class QKEditText extends android.widget.EditText implements LiveView {
}); });
} }
} }
@Override
public void refresh() {
// Typeface and colors
int fontFamily = FontManager.getFontFamily(mContext);
int fontWeight = FontManager.getFontWeight(mContext, false);
setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight
));
setTextColor(ThemeManager.getTextOnBackgroundPrimary());
setHintTextColor(ThemeManager.getTextOnBackgroundSecondary());
// Text size
int sp = FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY);
setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
}
} }
@@ -5,13 +5,11 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.ImageView; import android.widget.ImageView;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.interfaces.LiveView; import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment;
public class QKImageView extends ImageView implements LiveView { public class QKImageView extends ImageView {
private static final String TAG = "QKImageView"; private static final String TAG = "QKImageView";
private Drawable mDrawable; private Drawable mDrawable;
@@ -32,9 +30,9 @@ public class QKImageView extends ImageView implements LiveView {
} }
public void init() { public void init() {
// Register this view for live updates. LiveViewManager.registerView(QKPreference.THEME, key -> {
LiveViewManager.registerView(this); setImageDrawable(mDrawable);
LiveViewManager.registerPreference(this, SettingsFragment.THEME); });
} }
@Override @Override
@@ -49,11 +47,6 @@ public class QKImageView extends ImageView implements LiveView {
} }
} }
@Override
public void refresh() {
setImageDrawable(mDrawable);
}
@Override @Override
public void onAttachedToWindow() { public void onAttachedToWindow() {
super.onAttachedToWindow(); super.onAttachedToWindow();
@@ -9,11 +9,10 @@ import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet; import android.util.AttributeSet;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.interfaces.LiveView; import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment;
public class QKSwitch extends SwitchCompat implements LiveView { public class QKSwitch extends SwitchCompat {
private Resources mRes; private Resources mRes;
@@ -30,18 +29,10 @@ public class QKSwitch extends SwitchCompat implements LiveView {
private void init(Context context) { private void init(Context context) {
mRes = context.getResources(); mRes = context.getResources();
// Register this view for live updates. LiveViewManager.registerView(key -> {
LiveViewManager.registerView(this); DrawableCompat.setTintList(getThumbDrawable(), getSwitchThumbColorStateList());
LiveViewManager.registerPreference(this, SettingsFragment.THEME); DrawableCompat.setTintList(getTrackDrawable(), getSwitchTrackColorStateList());
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND); }, QKPreference.THEME, QKPreference.BACKGROUND);
refresh();
}
@Override
public void refresh() {
DrawableCompat.setTintList(getThumbDrawable(), getSwitchThumbColorStateList());
DrawableCompat.setTintList(getTrackDrawable(), getSwitchTrackColorStateList());
} }
private ColorStateList getSwitchThumbColorStateList() { private ColorStateList getSwitchThumbColorStateList() {
@@ -14,12 +14,12 @@ 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.common.TypefaceManager;
import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.common.utils.MessageUtils; import com.moez.QKSMS.common.utils.MessageUtils;
import com.moez.QKSMS.interfaces.LiveView;
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;
public class QKTextView extends TextView implements LiveView { public class QKTextView extends TextView {
private final String TAG = "QKTextView"; private final String TAG = "QKTextView";
private Context mContext; private Context mContext;
@@ -60,21 +60,32 @@ public class QKTextView extends TextView implements LiveView {
array.recycle(); array.recycle();
} }
refresh(); setTextColor(FontManager.getTextColor(mContext, mType));
setText(getText()); setText(getText());
// Register this view for live updates.
LiveViewManager.registerView(this);
LiveViewManager.registerPreference(this, SettingsFragment.FONT_FAMILY);
LiveViewManager.registerPreference(this, SettingsFragment.FONT_SIZE);
LiveViewManager.registerPreference(this, SettingsFragment.FONT_WEIGHT);
LiveViewManager.registerPreference(this, SettingsFragment.MARKDOWN_ENABLED);
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND);
// Register for theme updates if we're text that changes color dynamically. // Register for theme updates if we're text that changes color dynamically.
if (mType == FontManager.TEXT_TYPE_CATEGORY) { if (mType == FontManager.TEXT_TYPE_CATEGORY) {
LiveViewManager.registerPreference(this, SettingsFragment.THEME); LiveViewManager.registerView(QKPreference.THEME, key ->
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_SIZE, key -> {
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType));
});
LiveViewManager.registerView(QKPreference.BACKGROUND, key -> {
setTextColor(FontManager.getTextColor(mContext, mType));
});
LiveViewManager.registerView(QKPreference.TEXT_FORMATTING, key -> {
setText(getText(), BufferType.NORMAL);
});
} }
@Override @Override
@@ -124,11 +135,6 @@ public class QKTextView extends TextView implements LiveView {
} }
} }
public void setType(int type) {
mType = type;
refresh();
}
@Override @Override
public void setText(CharSequence text, BufferType type) { public void setText(CharSequence text, BufferType type) {
@@ -152,24 +158,4 @@ public class QKTextView extends TextView implements LiveView {
} }
} }
/**
* refresh() is called whenever a user preference around text size, font family, etc. has been
* updated and the view needs to refresh its properties.
*/
@Override
public void refresh() {
// Typeface
int fontFamily = FontManager.getFontFamily(mContext);
int fontWeight = FontManager.getFontWeight(mContext, FontManager.getIsFontHeavy(mType));
setTypeface(TypefaceManager.obtainTypeface(mContext, fontFamily, fontWeight
));
// Text size and color
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType));
setTextColor(FontManager.getTextColor(mContext, mType));
// Markdown support enabled
setText(getText(), BufferType.NORMAL);
}
} }
@@ -16,14 +16,14 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.preferences.QKPreference;
import com.moez.QKSMS.data.Contact; import com.moez.QKSMS.data.Contact;
import com.moez.QKSMS.data.ContactHelper; import com.moez.QKSMS.data.ContactHelper;
import com.moez.QKSMS.interfaces.LiveView;
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;
import com.moez.QKSMS.ui.settings.SettingsFragment; import com.moez.QKSMS.ui.settings.SettingsFragment;
public class StarredContactsView extends LinearLayout implements LoaderManager.LoaderCallbacks<Cursor>, View.OnClickListener, LiveView { public class StarredContactsView extends LinearLayout implements LoaderManager.LoaderCallbacks<Cursor>, View.OnClickListener {
private final String TAG = "StarredContactsView"; private final String TAG = "StarredContactsView";
private QKActivity mContext; private QKActivity mContext;
@@ -75,15 +75,16 @@ public class StarredContactsView extends LinearLayout implements LoaderManager.L
collapse(); collapse();
} }
LiveViewManager.registerView(this); LiveViewManager.registerView(QKPreference.BACKGROUND, key -> {
LiveViewManager.registerPreference(this, SettingsFragment.BACKGROUND); mIndicator.setColorFilter(ThemeManager.getTextOnBackgroundSecondary(), PorterDuff.Mode.SRC_ATOP);
refresh(); mFavoritesBackground.setBackgroundColor(ThemeManager.getBackgroundColor());
});
} }
public void setComposeScreenViews(AutoCompleteContactView recipients, ComposeView composeView) { public void setComposeScreenViews(AutoCompleteContactView recipients, ComposeView composeView) {
mRecipients = recipients; mRecipients = recipients;
mComposeView = composeView; mComposeView = composeView;
((QKActivity) mContext).getLoaderManager().initLoader(0, null, this); mContext.getLoaderManager().initLoader(0, null, this);
} }
@Override @Override
@@ -173,10 +174,4 @@ public class StarredContactsView extends LinearLayout implements LoaderManager.L
break; break;
} }
} }
@Override
public void refresh() {
mIndicator.setColorFilter(ThemeManager.getTextOnBackgroundSecondary(), PorterDuff.Mode.SRC_ATOP);
mFavoritesBackground.setBackgroundColor(ThemeManager.getBackgroundColor());
}
} }
@@ -10,7 +10,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator; import android.view.animation.DecelerateInterpolator;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.common.LiveViewManager;
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;
import com.moez.QKSMS.ui.view.QKTextView; import com.moez.QKSMS.ui.view.QKTextView;
@@ -36,8 +35,6 @@ public class WelcomeNightFragment extends BaseWelcomeFragment implements BaseWel
mContext.setFinished(); mContext.setFinished();
LiveViewManager.unregisterView(mNightHint);
return view; return view;
} }
@@ -92,9 +89,4 @@ public class WelcomeNightFragment extends BaseWelcomeFragment implements BaseWel
.commit(); .commit();
} }
} }
@Override
public void refresh() {
// Don't let the super class change the background, since we'll handle animations here
}
} }