From 3626b500a4c82abaa9b520d63e7f53c42b590c8c Mon Sep 17 00:00:00 2001 From: David Vavra Date: Thu, 5 Mar 2015 17:54:41 +0100 Subject: [PATCH] Refactoring to properly support dark theme --- .../dialogs/core/BaseDialogFragment.java | 15 --- .../dialogs/fragment/ListDialogFragment.java | 102 ++++++++---------- .../fragment/ProgressDialogFragment.java | 5 - .../main/res/drawable-v21/sdl_button_dark.xml | 5 + .../drawable-v21/sdl_button_selector_dark.xml | 5 + .../main/res/drawable-v21/sdl_list_dark.xml | 5 + .../drawable-v21/sdl_list_selector_dark.xml | 5 + .../src/main/res/drawable/sdl_button_dark.xml | 5 + .../main/res/drawable/sdl_button_pressed.xml | 2 +- .../res/drawable/sdl_button_pressed_dark.xml | 6 ++ .../src/main/res/drawable/sdl_list_dark.xml | 5 + .../main/res/drawable/sdl_list_pressed.xml | 2 +- .../res/drawable/sdl_list_pressed_dark.xml | 5 + library/src/main/res/values-v21/styles.xml | 42 ++++++++ library/src/main/res/values/sdl_attrs.xml | 11 ++ library/src/main/res/values/sdl_colors.xml | 26 +++-- library/src/main/res/values/sdl_styles.xml | 61 +++++------ 17 files changed, 185 insertions(+), 122 deletions(-) create mode 100644 library/src/main/res/drawable-v21/sdl_button_dark.xml create mode 100644 library/src/main/res/drawable-v21/sdl_button_selector_dark.xml create mode 100644 library/src/main/res/drawable-v21/sdl_list_dark.xml create mode 100644 library/src/main/res/drawable-v21/sdl_list_selector_dark.xml create mode 100644 library/src/main/res/drawable/sdl_button_dark.xml create mode 100644 library/src/main/res/drawable/sdl_button_pressed_dark.xml create mode 100644 library/src/main/res/drawable/sdl_list_dark.xml create mode 100644 library/src/main/res/drawable/sdl_list_pressed_dark.xml create mode 100644 library/src/main/res/values-v21/styles.xml create mode 100644 library/src/main/res/values/sdl_attrs.xml diff --git a/library/src/main/java/com/avast/android/dialogs/core/BaseDialogFragment.java b/library/src/main/java/com/avast/android/dialogs/core/BaseDialogFragment.java index 8082da5..5d7a135 100755 --- a/library/src/main/java/com/avast/android/dialogs/core/BaseDialogFragment.java +++ b/library/src/main/java/com/avast/android/dialogs/core/BaseDialogFragment.java @@ -61,13 +61,6 @@ public abstract class BaseDialogFragment extends DialogFragment implements Dialo private static boolean darkTheme; protected int mRequestCode; - /** - * @return True if dark theme is to be used - */ - public static boolean isDarkTheme() { - return darkTheme; - } - @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { @@ -240,7 +233,6 @@ private boolean isScrollable(ViewGroup listView) { * This method resolves the current theme declared in the manifest */ private void resolveTheme() { - //Try-catch block is used to overcome resource not found exception try { TypedValue val = new TypedValue(); @@ -406,13 +398,6 @@ public View create() { View vButtonsStacked = content.findViewById(R.id.sdl_buttons_stacked); ListView vList = (ListView) content.findViewById(R.id.sdl_list); - - //Dark theme is enabled - if (isDarkTheme()) { - vTitle.setTextAppearance(mContext, R.style.SDL_TextView_Title_Dark); - vMessage.setTextAppearance(mContext, R.style.SDL_TextView_Message_Dark); - } - Typeface regularFont = TypefaceHelper.get(mContext, "Roboto-Regular"); Typeface mediumFont = TypefaceHelper.get(mContext, "Roboto-Medium"); diff --git a/library/src/main/java/com/avast/android/dialogs/fragment/ListDialogFragment.java b/library/src/main/java/com/avast/android/dialogs/fragment/ListDialogFragment.java index c5b71a2..f4a0491 100755 --- a/library/src/main/java/com/avast/android/dialogs/fragment/ListDialogFragment.java +++ b/library/src/main/java/com/avast/android/dialogs/fragment/ListDialogFragment.java @@ -1,5 +1,8 @@ package com.avast.android.dialogs.fragment; +import java.util.Arrays; +import java.util.List; + import android.content.Context; import android.content.res.Resources; import android.os.Bundle; @@ -8,14 +11,10 @@ import android.support.v4.app.FragmentManager; import android.text.TextUtils; import android.util.SparseBooleanArray; +import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.AbsListView; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.ListAdapter; -import android.widget.ListView; -import android.widget.TextView; +import android.widget.*; import com.avast.android.dialogs.R; import com.avast.android.dialogs.core.BaseDialogBuilder; @@ -25,9 +24,6 @@ import com.avast.android.dialogs.iface.ISimpleDialogCancelListener; import com.avast.android.dialogs.util.SparseBooleanArrayParcelable; -import java.util.Arrays; -import java.util.List; - /** * Dialog with a list of options. *

@@ -76,77 +72,71 @@ public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (getArguments() == null) { throw new IllegalArgumentException( - "use SimpleListDialogBuilder to construct this dialog"); + "use SimpleListDialogBuilder to construct this dialog"); } } - private ListAdapter prepareAdapter(int itemLayoutId) { + private ListAdapter prepareAdapter(final int itemLayoutId) { return new ArrayAdapter(getActivity(), - itemLayoutId, - R.id.sdl_text, - getItems()) { + itemLayoutId, + R.id.sdl_text, + getItems()) { /** - * This function of list adapter class is overriden to set the dark style attrributes - * to the list view dynamically - * - * @param position - * @param convertView - * @param parent - * @return + * Overriding default implementation because it ignores current light/dark theme. */ @Override public View getView(int position, View convertView, ViewGroup parent) { - //Dark theme is enabled - if (isDarkTheme()) { - View v = super.getView(position, convertView, parent); - TextView t = (TextView) v.findViewById(R.id.sdl_text); - t.setTextColor(getResources().getColor(R.color.sdl_textColor_primary_dark)); - return v; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()).inflate(itemLayoutId, parent, false); + } + TextView t = (TextView)convertView.findViewById(R.id.sdl_text); + if (t != null) { + t.setText((CharSequence)getItem(position)); } - return super.getView(position, convertView, parent); + return convertView; } }; } private void buildMultiChoice(Builder builder) { builder.setItems( - prepareAdapter(R.layout.sdl_list_item_multichoice), - asIntArray(getCheckedItems()), AbsListView.CHOICE_MODE_MULTIPLE, - new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - SparseBooleanArray checkedPositions = ((ListView) parent).getCheckedItemPositions(); - setCheckedItems(new SparseBooleanArrayParcelable(checkedPositions)); - } - }); + prepareAdapter(R.layout.sdl_list_item_multichoice), + asIntArray(getCheckedItems()), AbsListView.CHOICE_MODE_MULTIPLE, + new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + SparseBooleanArray checkedPositions = ((ListView)parent).getCheckedItemPositions(); + setCheckedItems(new SparseBooleanArrayParcelable(checkedPositions)); + } + }); } private void buildSingleChoice(Builder builder) { builder.setItems( - prepareAdapter(R.layout.sdl_list_item_singlechoice), - asIntArray(getCheckedItems()), - AbsListView.CHOICE_MODE_SINGLE, new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - SparseBooleanArray checkedPositions = ((ListView) parent).getCheckedItemPositions(); - setCheckedItems(new SparseBooleanArrayParcelable(checkedPositions)); - } - }); + prepareAdapter(R.layout.sdl_list_item_singlechoice), + asIntArray(getCheckedItems()), + AbsListView.CHOICE_MODE_SINGLE, new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + SparseBooleanArray checkedPositions = ((ListView)parent).getCheckedItemPositions(); + setCheckedItems(new SparseBooleanArrayParcelable(checkedPositions)); + } + }); } private void buildNormalChoice(Builder builder) { builder.setItems( - prepareAdapter(R.layout.sdl_list_item), -1, - new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - for (IListDialogListener listener : getSingleDialogListeners()) { - listener.onListItemSelected(getItems()[position], position, mRequestCode); - } - dismiss(); + prepareAdapter(R.layout.sdl_list_item), -1, + new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + for (IListDialogListener listener : getSingleDialogListeners()) { + listener.onListItemSelected(getItems()[position], position, mRequestCode); } - }); + dismiss(); + } + }); } @Override @@ -415,7 +405,7 @@ public SimpleListDialogBuilder setCancelButtonText(int cancelBttTextResID) { @Override public ListDialogFragment show() { - return (ListDialogFragment) super.show(); + return (ListDialogFragment)super.show(); } @Override diff --git a/library/src/main/java/com/avast/android/dialogs/fragment/ProgressDialogFragment.java b/library/src/main/java/com/avast/android/dialogs/fragment/ProgressDialogFragment.java index f04ee48..da7912c 100644 --- a/library/src/main/java/com/avast/android/dialogs/fragment/ProgressDialogFragment.java +++ b/library/src/main/java/com/avast/android/dialogs/fragment/ProgressDialogFragment.java @@ -38,11 +38,6 @@ protected Builder build(Builder builder) { final View view = inflater.inflate(R.layout.sdl_progress, null, false); final TextView tvMessage = (TextView) view.findViewById(R.id.sdl_message); - //Changing the textview style to dark dynamically if dark theme is in use - if (isDarkTheme()) { - tvMessage.setTextAppearance(getActivity(), R.style.SDL_TextView_Message_Progress_Dark); - } - tvMessage.setText(getArguments().getCharSequence(ARG_MESSAGE)); builder.setView(view); diff --git a/library/src/main/res/drawable-v21/sdl_button_dark.xml b/library/src/main/res/drawable-v21/sdl_button_dark.xml new file mode 100644 index 0000000..ea93bfe --- /dev/null +++ b/library/src/main/res/drawable-v21/sdl_button_dark.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable-v21/sdl_button_selector_dark.xml b/library/src/main/res/drawable-v21/sdl_button_selector_dark.xml new file mode 100644 index 0000000..c6cd8f2 --- /dev/null +++ b/library/src/main/res/drawable-v21/sdl_button_selector_dark.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable-v21/sdl_list_dark.xml b/library/src/main/res/drawable-v21/sdl_list_dark.xml new file mode 100644 index 0000000..47ea914 --- /dev/null +++ b/library/src/main/res/drawable-v21/sdl_list_dark.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable-v21/sdl_list_selector_dark.xml b/library/src/main/res/drawable-v21/sdl_list_selector_dark.xml new file mode 100644 index 0000000..f7bf642 --- /dev/null +++ b/library/src/main/res/drawable-v21/sdl_list_selector_dark.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable/sdl_button_dark.xml b/library/src/main/res/drawable/sdl_button_dark.xml new file mode 100644 index 0000000..cd013e9 --- /dev/null +++ b/library/src/main/res/drawable/sdl_button_dark.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable/sdl_button_pressed.xml b/library/src/main/res/drawable/sdl_button_pressed.xml index 596e94e..f5d0b7b 100644 --- a/library/src/main/res/drawable/sdl_button_pressed.xml +++ b/library/src/main/res/drawable/sdl_button_pressed.xml @@ -2,5 +2,5 @@ - + \ No newline at end of file diff --git a/library/src/main/res/drawable/sdl_button_pressed_dark.xml b/library/src/main/res/drawable/sdl_button_pressed_dark.xml new file mode 100644 index 0000000..453d9b0 --- /dev/null +++ b/library/src/main/res/drawable/sdl_button_pressed_dark.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable/sdl_list_dark.xml b/library/src/main/res/drawable/sdl_list_dark.xml new file mode 100644 index 0000000..8a02fa4 --- /dev/null +++ b/library/src/main/res/drawable/sdl_list_dark.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable/sdl_list_pressed.xml b/library/src/main/res/drawable/sdl_list_pressed.xml index 5ce2954..30b1fb2 100644 --- a/library/src/main/res/drawable/sdl_list_pressed.xml +++ b/library/src/main/res/drawable/sdl_list_pressed.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/library/src/main/res/drawable/sdl_list_pressed_dark.xml b/library/src/main/res/drawable/sdl_list_pressed_dark.xml new file mode 100644 index 0000000..0908b0e --- /dev/null +++ b/library/src/main/res/drawable/sdl_list_pressed_dark.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/library/src/main/res/values-v21/styles.xml b/library/src/main/res/values-v21/styles.xml new file mode 100644 index 0000000..9e92506 --- /dev/null +++ b/library/src/main/res/values-v21/styles.xml @@ -0,0 +1,42 @@ + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/sdl_attrs.xml b/library/src/main/res/values/sdl_attrs.xml new file mode 100644 index 0000000..a46c49b --- /dev/null +++ b/library/src/main/res/values/sdl_attrs.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/sdl_colors.xml b/library/src/main/res/values/sdl_colors.xml index 1e582fd..9e3325c 100755 --- a/library/src/main/res/values/sdl_colors.xml +++ b/library/src/main/res/values/sdl_colors.xml @@ -1,12 +1,20 @@ - - #DE000000 - - #8A000000 - - #1F000000 - - #D5D5D5 - #FFFFFFFF + + + #DE000000 + + #FFFFFFFF + + #8A000000 + + #B3FFFFFF + + #1F000000 + + #1FFFFFFF + + #29000000 + + #29FFFFFF diff --git a/library/src/main/res/values/sdl_styles.xml b/library/src/main/res/values/sdl_styles.xml index 515ebd0..521a665 100755 --- a/library/src/main/res/values/sdl_styles.xml +++ b/library/src/main/res/values/sdl_styles.xml @@ -16,6 +16,25 @@ 0.6 @null @drawable/sdl_background + @drawable/abc_btn_radio_material + @drawable/abc_btn_check_material + @color/sdl_text_primary_light + @color/sdl_text_secondary_light + @color/sdl_divider_light + @color/sdl_pressed_light + @drawable/sdl_button + @drawable/sdl_list + + + + @@ -87,7 +106,7 @@ wrap_content @dimen/grid_13 18sp - @color/sdl_text_primary + ?sdlTextPrimaryColor center_vertical @dimen/grid_6 @dimen/grid_6 @@ -96,7 +115,7 @@ @@ -124,7 +143,7 @@ - - - - - - - - - - - - - -