From f5fc3850392601797a44c6dc07128155a420faa7 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 24 Mar 2020 14:14:47 +0800 Subject: Add CustomThemeListingActivity. --- app/src/main/AndroidManifest.xml | 5 +- .../Activity/CustomThemeListingActivity.java | 37 ++++++++ .../CustomThemeListingRecyclerViewAdapter.java | 99 +++++++++++++++++++--- .../infinityforreddit/AppComponent.java | 3 + .../Settings/ThemePreferenceFragment.java | 16 +++- .../Utils/SharedPreferencesUtils.java | 1 + .../main/res/layout/item_theme_type_divider.xml | 5 ++ app/src/main/res/values/strings.xml | 5 ++ app/src/main/res/xml/theme_preferences.xml | 9 ++ 9 files changed, 164 insertions(+), 16 deletions(-) create mode 100644 app/src/main/res/layout/item_theme_type_divider.xml (limited to 'app') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 233d5efc..dafb5596 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -21,7 +21,10 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true" tools:replace="android:label"> - + adapter.setUserThemes(customThemes)); + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + return false; } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomThemeListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomThemeListingRecyclerViewAdapter.java index 9504317d..81487a02 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomThemeListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomThemeListingRecyclerViewAdapter.java @@ -12,6 +12,7 @@ import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; +import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; @@ -20,49 +21,123 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomTheme; import ml.docilealligator.infinityforreddit.R; public class CustomThemeListingRecyclerViewAdapter extends RecyclerView.Adapter { + private static final int VIEW_TYPE_PREDEFINED_THEME = 0; + private static final int VIEW_TYPE_USER_THME = 1; + private static final int VIEW_TYPE_PREDEFINED_THEME_DIVIDER = 2; + private static final int VIEW_TYPE_USER_THEME_DIVIDER = 3; + private Context context; - private ArrayList customThemes; + private ArrayList predefinedCustomThemes; + private ArrayList userCustomThemes; - public CustomThemeListingRecyclerViewAdapter(Context context) { + public CustomThemeListingRecyclerViewAdapter(Context context, ArrayList predefinedCustomThemes) { this.context = context; - customThemes = new ArrayList<>(); + this.predefinedCustomThemes = predefinedCustomThemes; + userCustomThemes = new ArrayList<>(); + } + + @Override + public int getItemViewType(int position) { + if (position == 0) { + return VIEW_TYPE_PREDEFINED_THEME_DIVIDER; + } else if (position < 1 + predefinedCustomThemes.size()) { + return VIEW_TYPE_PREDEFINED_THEME; + } else if (position == 1 + predefinedCustomThemes.size()) { + return VIEW_TYPE_USER_THEME_DIVIDER; + } else { + return VIEW_TYPE_USER_THME; + } } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - return new CustomThemeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_custom_theme, parent, false)); + switch (viewType) { + case VIEW_TYPE_PREDEFINED_THEME_DIVIDER: + return new PreDefinedThemeDividerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_theme_type_divider, parent, false)); + case VIEW_TYPE_PREDEFINED_THEME: + return new PredefinedCustomThemeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_custom_theme, parent, false)); + case VIEW_TYPE_USER_THEME_DIVIDER: + return new UserThemeDividerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_theme_type_divider, parent, false)); + default: + return new UserCustomThemeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_custom_theme, parent, false)); + } } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { - if (holder instanceof CustomThemeViewHolder) { - CustomTheme customTheme = customThemes.get(position); - ((CustomThemeViewHolder) holder).colorPrimaryView.setBackgroundTintList(ColorStateList.valueOf(customTheme.colorPrimary)); - ((CustomThemeViewHolder) holder).nameTextView.setText(customTheme.name); - ((CustomThemeViewHolder) holder).itemView.setOnClickListener(view -> { + if (holder instanceof PredefinedCustomThemeViewHolder) { + CustomTheme customTheme = predefinedCustomThemes.get(position - 1); + ((PredefinedCustomThemeViewHolder) holder).colorPrimaryView.setBackgroundTintList(ColorStateList.valueOf(customTheme.colorPrimary)); + ((PredefinedCustomThemeViewHolder) holder).nameTextView.setText(customTheme.name); + ((PredefinedCustomThemeViewHolder) holder).itemView.setOnClickListener(view -> { Intent intent = new Intent(context, CustomizeThemeActivity.class); intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, customTheme.name); context.startActivity(intent); }); + } else if (holder instanceof UserCustomThemeViewHolder) { + CustomTheme customTheme = userCustomThemes.get(position - predefinedCustomThemes.size() - 2); + ((UserCustomThemeViewHolder) holder).colorPrimaryView.setBackgroundTintList(ColorStateList.valueOf(customTheme.colorPrimary)); + ((UserCustomThemeViewHolder) holder).nameTextView.setText(customTheme.name); + ((UserCustomThemeViewHolder) holder).itemView.setOnClickListener(view -> { + Intent intent = new Intent(context, CustomizeThemeActivity.class); + intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, customTheme.name); + context.startActivity(intent); + }); + } else if (holder instanceof PreDefinedThemeDividerViewHolder) { + ((TextView) ((PreDefinedThemeDividerViewHolder) holder).itemView).setText(R.string.predefined_themes); + } else if (holder instanceof UserThemeDividerViewHolder) { + ((TextView) ((UserThemeDividerViewHolder) holder).itemView).setText(R.string.user_themes); } } @Override public int getItemCount() { - return 0; + return predefinedCustomThemes.size() + userCustomThemes.size() + 2; + } + + public void setUserThemes(List userThemes) { + userCustomThemes = (ArrayList) userThemes; + notifyDataSetChanged(); } - class CustomThemeViewHolder extends RecyclerView.ViewHolder { + class PredefinedCustomThemeViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.color_primary_item_custom_theme) View colorPrimaryView; @BindView(R.id.name_text_view_item_custom_theme) TextView nameTextView; - public CustomThemeViewHolder(@NonNull View itemView) { + public PredefinedCustomThemeViewHolder(@NonNull View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } + + class UserCustomThemeViewHolder extends RecyclerView.ViewHolder { + + @BindView(R.id.color_primary_item_custom_theme) + View colorPrimaryView; + @BindView(R.id.name_text_view_item_custom_theme) + TextView nameTextView; + + public UserCustomThemeViewHolder(@NonNull View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + } + } + + class PreDefinedThemeDividerViewHolder extends RecyclerView.ViewHolder { + + public PreDefinedThemeDividerViewHolder(@NonNull View itemView) { + super(itemView); + } + } + + class UserThemeDividerViewHolder extends RecyclerView.ViewHolder { + + public UserThemeDividerViewHolder(@NonNull View itemView) { + super(itemView); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 80c6c6c7..b7c2c947 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -7,6 +7,7 @@ import ml.docilealligator.infinityforreddit.Activity.AccountPostsActivity; import ml.docilealligator.infinityforreddit.Activity.AccountSavedThingActivity; import ml.docilealligator.infinityforreddit.Activity.CommentActivity; import ml.docilealligator.infinityforreddit.Activity.CreateMultiRedditActivity; +import ml.docilealligator.infinityforreddit.Activity.CustomThemeListingActivity; import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.Activity.EditCommentActivity; import ml.docilealligator.infinityforreddit.Activity.EditPostActivity; @@ -143,4 +144,6 @@ public interface AppComponent { void inject(ThemePreferenceFragment themePreferenceFragment); void inject(CustomizeThemeActivity customizeThemeActivity); + + void inject(CustomThemeListingActivity customThemeListingActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/ThemePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/ThemePreferenceFragment.java index 58dbf619..f96f6f21 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/ThemePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/ThemePreferenceFragment.java @@ -19,6 +19,7 @@ import org.greenrobot.eventbus.EventBus; import javax.inject.Inject; +import ml.docilealligator.infinityforreddit.Activity.CustomThemeListingActivity; import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; @@ -52,6 +53,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { Preference customizeLightThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_LIGHT_THEME); Preference customizeDarkThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_DARK_THEME); Preference customizeAmoledThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_AMOLED_THEME); + Preference selectAndCustomizeThemePreference = findPreference(SharedPreferencesUtils.SELECT_AND_CUSTOMIZE_THEME); boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; if (themePreference != null && amoledDarkSwitch != null) { @@ -112,7 +114,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { Intent intent = new Intent(activity, CustomizeThemeActivity.class); intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_TYPE, CustomizeThemeActivity.EXTRA_LIGHT_THEME); startActivity(intent); - return false; + return true; }); } @@ -121,7 +123,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { Intent intent = new Intent(activity, CustomizeThemeActivity.class); intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_TYPE, CustomizeThemeActivity.EXTRA_DARK_THEME); startActivity(intent); - return false; + return true; }); } @@ -130,7 +132,15 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { Intent intent = new Intent(activity, CustomizeThemeActivity.class); intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_TYPE, CustomizeThemeActivity.EXTRA_AMOLED_THEME); startActivity(intent); - return false; + return true; + }); + } + + if (selectAndCustomizeThemePreference != null) { + selectAndCustomizeThemePreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, CustomThemeListingActivity.class); + startActivity(intent); + return true; }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index ef15d5e6..d9385249 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -80,4 +80,5 @@ public class SharedPreferencesUtils { public static final String CUSTOMIZE_LIGHT_THEME = "customize_light_theme"; public static final String CUSTOMIZE_DARK_THEME = "customize_dark_theme"; public static final String CUSTOMIZE_AMOLED_THEME = "customize_amoled_theme"; + public static final String SELECT_AND_CUSTOMIZE_THEME = "select_and_customize_theme"; } diff --git a/app/src/main/res/layout/item_theme_type_divider.xml b/app/src/main/res/layout/item_theme_type_divider.xml new file mode 100644 index 00000000..0933ae9c --- /dev/null +++ b/app/src/main/res/layout/item_theme_type_divider.xml @@ -0,0 +1,5 @@ + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fc88749c..b8d4e657 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -19,6 +19,7 @@ Multireddit Create Multireddit Select Subreddits + Custom Themes Open navigation drawer Close navigation drawer @@ -361,6 +362,8 @@ Light Theme Dark Theme Amoled Theme + Select and Customize Themes + Custom themes cannot be applied to settings page. Cannot get the link @@ -598,6 +601,8 @@ Only available for Android 8.0 or above. Only available for Android 6.0 or above. + Predefined Themes + Your Themes Indigo Indigo Dark Indigo Amoled diff --git a/app/src/main/res/xml/theme_preferences.xml b/app/src/main/res/xml/theme_preferences.xml index e7d9c13f..5bcc5ef3 100644 --- a/app/src/main/res/xml/theme_preferences.xml +++ b/app/src/main/res/xml/theme_preferences.xml @@ -33,4 +33,13 @@ app:icon="@drawable/ic_dark_theme_preference_24dp" app:title="@string/settings_customize_amoled_theme_title" /> + + + + \ No newline at end of file -- cgit v1.2.3