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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
package ml.docilealligator.infinityforreddit.adapters;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.slider.Slider;
import com.google.android.material.textfield.TextInputEditText;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
public class MarkdownBottomBarRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int BOLD = 10;
public static final int ITALIC = 9;
public static final int LINK = 8;
public static final int STRIKE_THROUGH = 7;
public static final int SUPERSCRIPT = 6;
public static final int HEADER = 5;
public static final int ORDERED_LIST = 4;
public static final int UNORDERED_LIST = 3;
public static final int SPOILER = 2;
public static final int QUOTE = 1;
public static final int CODE_BLOCK = 0;
public static final int UPLOAD_IMAGE = 11;
public static final int GIPHY_GIF = 12;
private static final int REGULAR_ITEM_COUNT = 11;
private final CustomThemeWrapper customThemeWrapper;
private final boolean canUploadImage;
private final boolean canSendGiphyGIf;
private final ItemClickListener itemClickListener;
public interface ItemClickListener {
void onClick(int item);
void onUploadImage();
default void onSelectGiphyGif() {}
}
public MarkdownBottomBarRecyclerViewAdapter(CustomThemeWrapper customThemeWrapper,
ItemClickListener itemClickListener) {
this(customThemeWrapper, false, false, itemClickListener);
}
public MarkdownBottomBarRecyclerViewAdapter(CustomThemeWrapper customThemeWrapper,
boolean canUploadImage,
ItemClickListener itemClickListener) {
this(customThemeWrapper, canUploadImage, false, itemClickListener);
}
public MarkdownBottomBarRecyclerViewAdapter(CustomThemeWrapper customThemeWrapper,
boolean canUploadImage, boolean canSendGiphyGif,
ItemClickListener itemClickListener) {
this.customThemeWrapper = customThemeWrapper;
this.canUploadImage = canUploadImage;
this.canSendGiphyGIf = canSendGiphyGif;
this.itemClickListener = itemClickListener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MarkdownBottomBarItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_markdown_bottom_bar, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MarkdownBottomBarItemViewHolder) {
switch (position) {
case BOLD:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_bold_black_24dp);
break;
case ITALIC:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_italic_black_24dp);
break;
case LINK:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_link_round_black_24dp);
break;
case STRIKE_THROUGH:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_strikethrough_black_24dp);
break;
case SUPERSCRIPT:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_superscript_24dp);
break;
case HEADER:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_title_24dp);
break;
case ORDERED_LIST:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_ordered_list_black_24dp);
break;
case UNORDERED_LIST:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_unordered_list_24dp);
break;
case SPOILER:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_spoiler_black_24dp);
break;
case QUOTE:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_quote_24dp);
break;
case CODE_BLOCK:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_code_24dp);
break;
case UPLOAD_IMAGE:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_image_day_night_24dp);
break;
case GIPHY_GIF:
((MarkdownBottomBarItemViewHolder) holder).imageView.setImageResource(R.drawable.ic_gif_24dp);
break;
}
}
}
@Override
public int getItemCount() {
return canUploadImage ? (canSendGiphyGIf ? REGULAR_ITEM_COUNT + 2 : REGULAR_ITEM_COUNT + 1) : REGULAR_ITEM_COUNT;
}
public static void bindEditTextWithItemClickListener(Activity activity, EditText commentEditText, int item) {
switch (item) {
case MarkdownBottomBarRecyclerViewAdapter.BOLD: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"**" + currentSelection + "**", 0, "****".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
"****", 0, "****".length());
commentEditText.setSelection(start + "**".length());
}
break;
}
case MarkdownBottomBarRecyclerViewAdapter.ITALIC: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"*" + currentSelection + "*", 0, "**".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
"**", 0, "**".length());
commentEditText.setSelection(start + "*".length());
}
break;
}
case MarkdownBottomBarRecyclerViewAdapter.LINK: {
View dialogView = activity.getLayoutInflater().inflate(R.layout.dialog_insert_link, null);
TextInputEditText textEditText = dialogView.findViewById(R.id.edit_text_insert_link_dialog);
TextInputEditText linkEditText = dialogView.findViewById(R.id.edit_link_insert_link_dialog);
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
textEditText.setText(currentSelection);
}
new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.insert_link)
.setView(dialogView)
.setPositiveButton(R.string.ok, (editTextDialogInterface, i1)
-> {
String text = textEditText.getText().toString();
String link = linkEditText.getText().toString();
if (text.equals("")) {
text = link;
}
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"[" + text + "](" + link + ")", 0, "[]()".length() + text.length() + link.length());
})
.setNegativeButton(R.string.cancel, null)
.show();
break;
}
case MarkdownBottomBarRecyclerViewAdapter.STRIKE_THROUGH: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"~~" + currentSelection + "~~", 0, "~~~~".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
"~~~~", 0, "~~~~".length());
commentEditText.setSelection(start + "~~".length());
}
break;
}
case MarkdownBottomBarRecyclerViewAdapter.SUPERSCRIPT: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"^(" + currentSelection + ")", 0, "^()".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
"^()", 0, "^()".length());
commentEditText.setSelection(start + "^(".length());
}
break;
}
case MarkdownBottomBarRecyclerViewAdapter.HEADER: {
View dialogView = activity.getLayoutInflater().inflate(R.layout.dialog_select_header, null);
Slider seekBar = dialogView.findViewById(R.id.seek_bar_dialog_select_header);
new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.select_header_size)
.setView(dialogView)
.setPositiveButton(R.string.ok, (editTextDialogInterface, i1)
-> {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
String hashTags;
switch ((int) seekBar.getValue()) {
case 1:
hashTags = "# ";
break;
case 2:
hashTags = "## ";
break;
case 3:
hashTags = "### ";
break;
case 4:
hashTags = "#### ";
break;
case 5:
hashTags = "##### ";
break;
default:
hashTags = "###### ";
break;
}
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
hashTags + currentSelection, 0, hashTags.length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
hashTags, 0, hashTags.length());
}
})
.setNegativeButton(R.string.cancel, null)
.show();
break;
}
case MarkdownBottomBarRecyclerViewAdapter.ORDERED_LIST: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"1. " + currentSelection, 0, "1. ".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
"1. ", 0, "1. ".length());
}
break;
}
case MarkdownBottomBarRecyclerViewAdapter.UNORDERED_LIST: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"* " + currentSelection, 0, "* ".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
"* ", 0, "* ".length());
}
break;
}
case MarkdownBottomBarRecyclerViewAdapter.SPOILER: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
">!" + currentSelection + "!<", 0, ">!!<".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
">!!<", 0, ">!!<".length());
commentEditText.setSelection(start + ">!".length());
}
break;
}
case QUOTE: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"> " + currentSelection + "\n\n", 0, "> \n\n".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
"> \n\n", 0, "> \n\n".length());
commentEditText.setSelection(start + "> ".length());
}
break;
}
case CODE_BLOCK: {
int start = Math.max(commentEditText.getSelectionStart(), 0);
int end = Math.max(commentEditText.getSelectionEnd(), 0);
if (end != start) {
String currentSelection = commentEditText.getText().subSequence(start, end).toString();
commentEditText.getText().replace(Math.min(start, end), Math.max(start, end),
"```\n" + currentSelection + "\n```\n", 0, "```\n\n```\n".length() + currentSelection.length());
} else {
commentEditText.getText().replace(start, end,
"```\n\n```\n", 0, "```\n\n```\n".length());
commentEditText.setSelection(start + "```\n".length());
}
break;
}
}
}
class MarkdownBottomBarItemViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public MarkdownBottomBarItemViewHolder(@NonNull View itemView) {
super(itemView);
imageView = (ImageView) itemView;
itemView.setOnClickListener(view -> {
int position = getBindingAdapterPosition();
if (position == UPLOAD_IMAGE) {
itemClickListener.onUploadImage();
} else if (position == GIPHY_GIF) {
itemClickListener.onSelectGiphyGif();
} else {
itemClickListener.onClick(position);
}
});
imageView.setColorFilter(customThemeWrapper.getPrimaryIconColor(), android.graphics.PorterDuff.Mode.SRC_IN);
}
}
}
|