1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
package ml.docilealligator.infinityforreddit.adapters;
import android.content.res.ColorStateList;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import java.util.ArrayList;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeSettingsItem;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.ColorPickerDialog;
import ml.docilealligator.infinityforreddit.databinding.ItemCustomThemeColorItemBinding;
import ml.docilealligator.infinityforreddit.databinding.ItemCustomThemeSwitchItemBinding;
import ml.docilealligator.infinityforreddit.databinding.ItemThemeNameBinding;
import ml.docilealligator.infinityforreddit.utils.Utils;
public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_COLOR = 1;
private static final int VIEW_TYPE_SWITCH = 2;
private static final int VIEW_TYPE_THEME_NAME = 3;
private final BaseActivity activity;
private final CustomThemeWrapper customThemeWrapper;
private final ArrayList<CustomThemeSettingsItem> customThemeSettingsItems;
private String themeName;
public CustomizeThemeRecyclerViewAdapter(BaseActivity activity, CustomThemeWrapper customThemeWrapper,
String themeName) {
this.activity = activity;
this.customThemeWrapper = customThemeWrapper;
customThemeSettingsItems = new ArrayList<>();
this.themeName = themeName;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return VIEW_TYPE_THEME_NAME;
} else if (position > 3 && position < customThemeSettingsItems.size() - 2) {
return VIEW_TYPE_COLOR;
}
return VIEW_TYPE_SWITCH;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_SWITCH) {
return new ThemeSwitchItemViewHolder(ItemCustomThemeSwitchItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
} else if (viewType == VIEW_TYPE_THEME_NAME) {
return new ThemeNameItemViewHolder(ItemThemeNameBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
return new ThemeColorItemViewHolder(ItemCustomThemeColorItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ThemeColorItemViewHolder) {
CustomThemeSettingsItem customThemeSettingsItem = customThemeSettingsItems.get(position - 1);
((ThemeColorItemViewHolder) holder).binding.themeItemNameTextViewItemCustomThemeColorItem.setText(customThemeSettingsItem.itemName);
((ThemeColorItemViewHolder) holder).binding.themeItemInfoTextViewItemCustomThemeColorItem.setText(customThemeSettingsItem.itemDetails);
((ThemeColorItemViewHolder) holder).binding.colorImageViewItemCustomThemeColorItem.setBackgroundTintList(ColorStateList.valueOf(customThemeSettingsItem.colorValue));
holder.itemView.setOnClickListener(view -> {
new ColorPickerDialog(activity, customThemeSettingsItem.colorValue, color -> {
customThemeSettingsItem.colorValue = color;
((ThemeColorItemViewHolder) holder).binding.colorImageViewItemCustomThemeColorItem.setBackgroundTintList(ColorStateList.valueOf(color));
}).show();
});
} else if (holder instanceof ThemeSwitchItemViewHolder) {
CustomThemeSettingsItem customThemeSettingsItem = customThemeSettingsItems.get(position - 1);
((ThemeSwitchItemViewHolder) holder).binding.themeItemNameTextViewItemCustomThemeSwitchItem.setText(customThemeSettingsItem.itemName);
((ThemeSwitchItemViewHolder) holder).binding.themeItemInfoTextViewItemCustomThemeSwitchItem.setText(customThemeSettingsItem.itemName);
((ThemeSwitchItemViewHolder) holder).binding.themeItemSwitchItemCustomThemeSwitchItem.setChecked(customThemeSettingsItem.isEnabled);
((ThemeSwitchItemViewHolder) holder).binding.themeItemSwitchItemCustomThemeSwitchItem.setOnClickListener(view -> customThemeSettingsItem.isEnabled = ((ThemeSwitchItemViewHolder) holder).binding.themeItemSwitchItemCustomThemeSwitchItem.isChecked());
holder.itemView.setOnClickListener(view -> ((ThemeSwitchItemViewHolder) holder).binding.themeItemSwitchItemCustomThemeSwitchItem.performClick());
} else if (holder instanceof ThemeNameItemViewHolder) {
((ThemeNameItemViewHolder) holder).binding.themeNameTextViewItemThemeName.setText(themeName);
holder.itemView.setOnClickListener(view -> {
View dialogView = activity.getLayoutInflater().inflate(R.layout.dialog_edit_name, null);
EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog);
themeNameEditText.setText(themeName);
themeNameEditText.requestFocus();
Utils.showKeyboard(activity, new Handler(), themeNameEditText);
new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.edit_theme_name)
.setView(dialogView)
.setPositiveButton(R.string.ok, (dialogInterface, i)
-> {
Utils.hideKeyboard(activity);
themeName = themeNameEditText.getText().toString();
((ThemeNameItemViewHolder) holder).binding.themeNameTextViewItemThemeName.setText(themeName);
})
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
Utils.hideKeyboard(activity);
})
.setOnDismissListener(dialogInterface -> {
Utils.hideKeyboard(activity);
})
.show();
});
}
}
@Override
public int getItemCount() {
return customThemeSettingsItems.size() + 1;
}
public void setCustomThemeSettingsItem(ArrayList<CustomThemeSettingsItem> customThemeSettingsItems) {
this.customThemeSettingsItems.clear();
this.customThemeSettingsItems.addAll(customThemeSettingsItems);
notifyDataSetChanged();
}
public String getThemeName() {
return themeName;
}
class ThemeColorItemViewHolder extends RecyclerView.ViewHolder {
ItemCustomThemeColorItemBinding binding;
ThemeColorItemViewHolder(@NonNull ItemCustomThemeColorItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
binding.themeItemNameTextViewItemCustomThemeColorItem.setTextColor(customThemeWrapper.getPrimaryTextColor());
binding.themeItemInfoTextViewItemCustomThemeColorItem.setTextColor(customThemeWrapper.getSecondaryTextColor());
if (activity.typeface != null) {
binding.themeItemNameTextViewItemCustomThemeColorItem.setTypeface(activity.typeface);
binding.themeItemInfoTextViewItemCustomThemeColorItem.setTypeface(activity.typeface);
}
}
}
class ThemeSwitchItemViewHolder extends RecyclerView.ViewHolder {
ItemCustomThemeSwitchItemBinding binding;
ThemeSwitchItemViewHolder(@NonNull ItemCustomThemeSwitchItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
binding.themeItemNameTextViewItemCustomThemeSwitchItem.setTextColor(customThemeWrapper.getPrimaryTextColor());
binding.themeItemInfoTextViewItemCustomThemeSwitchItem.setTextColor(customThemeWrapper.getSecondaryTextColor());
if (activity.typeface != null) {
binding.themeItemNameTextViewItemCustomThemeSwitchItem.setTypeface(activity.typeface);
binding.themeItemInfoTextViewItemCustomThemeSwitchItem.setTypeface(activity.typeface);
}
}
}
class ThemeNameItemViewHolder extends RecyclerView.ViewHolder {
ItemThemeNameBinding binding;
public ThemeNameItemViewHolder(@NonNull ItemThemeNameBinding binding) {
super(binding.getRoot());
this.binding = binding;
binding.themeNameTextViewItemThemeName.setTextColor(customThemeWrapper.getPrimaryTextColor());
binding.descriptionTextViewItemThemeName.setTextColor(customThemeWrapper.getSecondaryTextColor());
if (activity.typeface != null) {
binding.themeNameTextViewItemThemeName.setTypeface(activity.typeface);
binding.descriptionTextViewItemThemeName.setTypeface(activity.typeface);
}
}
}
}
|