diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-03-24 06:14:47 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-03-24 06:14:47 +0000 |
commit | f5fc3850392601797a44c6dc07128155a420faa7 (patch) | |
tree | 3f63906f1f1f60debf55b833542fa723fe1cae97 /app/src/main/java/ml/docilealligator | |
parent | 9c4889b1b02a6bfa6486ce1c3bfc0c6b590f1f4e (diff) | |
download | infinity-for-reddit-f5fc3850392601797a44c6dc07128155a420faa7.tar infinity-for-reddit-f5fc3850392601797a44c6dc07128155a420faa7.tar.gz infinity-for-reddit-f5fc3850392601797a44c6dc07128155a420faa7.tar.bz2 infinity-for-reddit-f5fc3850392601797a44c6dc07128155a420faa7.tar.lz infinity-for-reddit-f5fc3850392601797a44c6dc07128155a420faa7.tar.xz infinity-for-reddit-f5fc3850392601797a44c6dc07128155a420faa7.tar.zst infinity-for-reddit-f5fc3850392601797a44c6dc07128155a420faa7.zip |
Add CustomThemeListingActivity.
Diffstat (limited to 'app/src/main/java/ml/docilealligator')
5 files changed, 141 insertions, 15 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomThemeListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomThemeListingActivity.java index 8ba9a885..791cd336 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomThemeListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomThemeListingActivity.java @@ -2,8 +2,12 @@ package ml.docilealligator.infinityforreddit.Activity; import android.content.SharedPreferences; import android.os.Bundle; +import android.view.MenuItem; +import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.appbar.AppBarLayout; @@ -12,7 +16,11 @@ import javax.inject.Inject; import javax.inject.Named; import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Adapter.CustomThemeListingRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeViewModel; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; @@ -31,11 +39,40 @@ public class CustomThemeListingActivity extends BaseActivity { RedditDataRoomDatabase redditDataRoomDatabase; @Inject CustomThemeWrapper customThemeWrapper; + public CustomThemeViewModel customThemeViewModel; @Override protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_theme_listing); + + ButterKnife.bind(this); + + applyCustomTheme(); + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + CustomThemeListingRecyclerViewAdapter adapter = new CustomThemeListingRecyclerViewAdapter(this, + CustomThemeWrapper.getPredifinedThemes(this)); + recyclerView.setAdapter(adapter); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + + customThemeViewModel = new ViewModelProvider(this, + new CustomThemeViewModel.Factory(redditDataRoomDatabase)) + .get(CustomThemeViewModel.class); + customThemeViewModel.getAllCustomThemes().observe(this, customThemes -> 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<RecyclerView.ViewHolder> { + 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<CustomTheme> customThemes; + private ArrayList<CustomTheme> predefinedCustomThemes; + private ArrayList<CustomTheme> userCustomThemes; - public CustomThemeListingRecyclerViewAdapter(Context context) { + public CustomThemeListingRecyclerViewAdapter(Context context, ArrayList<CustomTheme> 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<CustomTheme> userThemes) { + userCustomThemes = (ArrayList<CustomTheme>) 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"; } |