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
|
package ml.docilealligator.infinityforreddit.adapters;
import android.content.Intent;
import android.net.Uri;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import io.noties.markwon.AbstractMarkwonPlugin;
import io.noties.markwon.Markwon;
import io.noties.markwon.MarkwonConfiguration;
import io.noties.markwon.MarkwonPlugin;
import io.noties.markwon.core.MarkwonTheme;
import io.noties.markwon.recycler.MarkwonAdapter;
import ml.docilealligator.infinityforreddit.subreddit.Rule;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.activities.ViewImageOrGifActivity;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface;
import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager;
import ml.docilealligator.infinityforreddit.customviews.slidr.widget.SliderPanel;
import ml.docilealligator.infinityforreddit.databinding.ItemRuleBinding;
import ml.docilealligator.infinityforreddit.markdown.EmoteCloseBracketInlineProcessor;
import ml.docilealligator.infinityforreddit.markdown.EmotePlugin;
import ml.docilealligator.infinityforreddit.markdown.EvenBetterLinkMovementMethod;
import ml.docilealligator.infinityforreddit.markdown.ImageAndGifEntry;
import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin;
import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecyclerViewAdapter.RuleViewHolder> {
private final BaseActivity activity;
private final EmoteCloseBracketInlineProcessor emoteCloseBracketInlineProcessor;
private final EmotePlugin emotePlugin;
private final ImageAndGifPlugin imageAndGifPlugin;
private final ImageAndGifEntry imageAndGifEntry;
private final Markwon markwon;
@Nullable
private final SliderPanel sliderPanel;
private ArrayList<Rule> rules;
private final int mPrimaryTextColor;
public RulesRecyclerViewAdapter(@NonNull BaseActivity activity,
@NonNull CustomThemeWrapper customThemeWrapper,
@Nullable SliderPanel sliderPanel, String subredditName) {
this.activity = activity;
this.sliderPanel = sliderPanel;
mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor();
int spoilerBackgroundColor = mPrimaryTextColor | 0xFF000000;
MarkwonPlugin miscPlugin = new AbstractMarkwonPlugin() {
@Override
public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) {
if (activity.typeface != null) {
textView.setTypeface(activity.typeface);
}
textView.setTextColor(mPrimaryTextColor);
}
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.linkResolver((view, link) -> {
Intent intent = new Intent(activity, LinkResolverActivity.class);
Uri uri = Uri.parse(link);
intent.setData(uri);
activity.startActivity(intent);
});
}
@Override
public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
builder.linkColor(customThemeWrapper.getLinkColor());
}
};
EvenBetterLinkMovementMethod.OnLinkLongClickListener onLinkLongClickListener = (textView, url) -> {
if (!activity.isDestroyed() && !activity.isFinishing()) {
UrlMenuBottomSheetFragment urlMenuBottomSheetFragment = UrlMenuBottomSheetFragment.newInstance(url);
urlMenuBottomSheetFragment.show(activity.getSupportFragmentManager(), null);
}
return true;
};
emoteCloseBracketInlineProcessor = new EmoteCloseBracketInlineProcessor();
emotePlugin = EmotePlugin.create(activity, SharedPreferencesUtils.EMBEDDED_MEDIA_ALL,
mediaMetadata -> {
Intent imageIntent = new Intent(activity, ViewImageOrGifActivity.class);
if (mediaMetadata.isGIF) {
imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url);
} else {
imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url);
}
imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName);
imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName);
});
imageAndGifPlugin = new ImageAndGifPlugin();
imageAndGifEntry = new ImageAndGifEntry(activity,
Glide.with(activity), SharedPreferencesUtils.EMBEDDED_MEDIA_ALL,
mediaMetadata -> {
Intent imageIntent = new Intent(activity, ViewImageOrGifActivity.class);
if (mediaMetadata.isGIF) {
imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url);
} else {
imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url);
}
imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName);
imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName);
});
markwon = MarkdownUtils.createFullRedditMarkwon(activity,
miscPlugin, emoteCloseBracketInlineProcessor, emotePlugin, imageAndGifPlugin, mPrimaryTextColor,
spoilerBackgroundColor, onLinkLongClickListener);
}
@NonNull
@Override
public RuleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new RuleViewHolder(ItemRuleBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(@NonNull RuleViewHolder holder, int position) {
Rule rule = rules.get(holder.getBindingAdapterPosition());
holder.binding.shortNameTextViewItemRule.setText(rule.getShortName());
if (rule.getDescriptionHtml() == null) {
holder.binding.descriptionMarkwonViewItemRule.setVisibility(View.GONE);
} else {
holder.markwonAdapter.setMarkdown(markwon, rule.getDescriptionHtml());
//noinspection NotifyDatasetChanged
holder.markwonAdapter.notifyDataSetChanged();
}
}
@Override
public int getItemCount() {
return rules == null ? 0 : rules.size();
}
@Override
public void onViewRecycled(@NonNull RuleViewHolder holder) {
super.onViewRecycled(holder);
holder.binding.descriptionMarkwonViewItemRule.setVisibility(View.VISIBLE);
}
public void changeDataset(ArrayList<Rule> rules) {
this.rules = rules;
notifyDataSetChanged();
}
public void setDataSavingMode(boolean dataSavingMode) {
emotePlugin.setDataSavingMode(dataSavingMode);
imageAndGifEntry.setDataSavingMode(dataSavingMode);
}
class RuleViewHolder extends RecyclerView.ViewHolder {
ItemRuleBinding binding;
@NonNull
final MarkwonAdapter markwonAdapter;
RuleViewHolder(@NonNull ItemRuleBinding binding) {
super(binding.getRoot());
this.binding = binding;
binding.shortNameTextViewItemRule.setTextColor(mPrimaryTextColor);
if (activity.typeface != null) {
binding.shortNameTextViewItemRule.setTypeface(activity.typeface);
}
markwonAdapter = MarkdownUtils.createCustomTablesAndImagesAdapter(activity, imageAndGifEntry);
SwipeLockLinearLayoutManager swipeLockLinearLayoutManager = new SwipeLockLinearLayoutManager(activity,
new SwipeLockInterface() {
@Override
public void lockSwipe() {
if (sliderPanel != null) {
sliderPanel.lock();
}
}
@Override
public void unlockSwipe() {
if (sliderPanel != null) {
sliderPanel.unlock();
}
}
});
binding.descriptionMarkwonViewItemRule.setLayoutManager(swipeLockLinearLayoutManager);
binding.descriptionMarkwonViewItemRule.setAdapter(markwonAdapter);
}
}
}
|