/
WidgetDynamicThemeChange.java
231 lines (186 loc) · 8.4 KB
/
WidgetDynamicThemeChange.java
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
package com.taboola.android.sdksamples.sdk_via_native;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.taboola.android.TaboolaWidget;
import com.taboola.android.sdksamples.R;
import java.util.HashMap;
import java.util.List;
/**
* in this example we are using keepDependencies flag, You should call this flag in case you are calling
* {@link RecyclerView.Adapter#notifyDataSetChanged()}, in order to make taboolaWidget keep it's dependencies.
* If this flag is set to true, you need to call TaboolaWidget.onDestroy() once the adapter is going to be cleared,
*
* @see #onDestroy()
* @see DynamicRecyclerViewAdapter#createTaboolaWidget(Context)
*/
public class WidgetDynamicThemeChange extends Fragment {
private DynamicRecyclerViewAdapter mAdapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_rv_dynamic, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = view.findViewById(R.id.feed_rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(linearLayoutManager);
mAdapter = new DynamicRecyclerViewAdapter(DynamicRecyclerViewAdapter.Theme.WHITE);
recyclerView.setAdapter(mAdapter);
androidx.appcompat.widget.SwitchCompat
switchCompat = view.findViewById(R.id.simpleSwitch);
switchCompat.setOnCheckedChangeListener((buttonView, isChecked) -> {
mAdapter.changeTheme(isChecked);
mAdapter.notifyDataSetChanged();
});
}
@Override
public void onDestroy() {
//make sure you are calling this method
mAdapter.onDestroy();
super.onDestroy();
}
static class DynamicRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<ListItemsGenerator.FeedListItem> mData;
private TaboolaWidget mTaboolaWidget;
private Theme mTheme;
private boolean mThemeChanged = false;
DynamicRecyclerViewAdapter(Theme theme) {
mData = ListItemsGenerator.getGeneratedDataForWidgetDynamic();
mTheme = theme;
}
public enum Theme {
BLACK, WHITE
}
private static TaboolaWidget createTaboolaWidget(Context context) {
TaboolaWidget taboolaWidget = new TaboolaWidget(context);
HashMap<String, String> extraProperties = new HashMap<>();
extraProperties.put("keepDependencies", "true");
extraProperties.put("useOnlineTemplate", "true");
taboolaWidget.setExtraProperties(extraProperties);
taboolaWidget.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return taboolaWidget;
}
/**
* you must to call this method because you use the keepDependencies flag in {@link #createTaboolaWidget(Context)}
* In this sample was called by {@link WidgetDynamicThemeChange#onDestroy()}
*/
public void onDestroy() {
if (mTaboolaWidget != null) {
mTaboolaWidget.onDestroy();
}
}
private void loadWidget(TaboolaWidget taboolaWidget) {
taboolaWidget
.setPublisher("sdk-tester-demo")
.setPageType("article")
.setPageUrl("https://blog.taboola.com")
.setTargetType("mix");
switch (mTheme) {
case BLACK:
taboolaWidget
.setPlacement("Dark Mode Widget")
.setMode("alternating-widget-1x2-dark");
break;
case WHITE:
taboolaWidget
.setPlacement("Mid Article")
.setMode("alternating-widget-1x2");
break;
}
taboolaWidget.fetchContent();
}
public void changeTheme(boolean isChecked) {
mTheme = isChecked ? Theme.BLACK : Theme.WHITE;
mThemeChanged = true;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case ListItemsGenerator.FeedListItem.ItemType.TABOOLA_MID_ITEM:
if (mTaboolaWidget == null) {
mTaboolaWidget = createTaboolaWidget(parent.getContext());
loadWidget(mTaboolaWidget);
}
return new ViewHolderTaboola(mTaboolaWidget);
case ListItemsGenerator.FeedListItem.ItemType.RANDOM_ITEM:
default:
View appCompatImageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.random_item, parent, false);
return new RandomImageViewHolder(appCompatImageView);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ListItemsGenerator.FeedListItem item = getItem(position);
if (item.type == ListItemsGenerator.FeedListItem.ItemType.RANDOM_ITEM) {
RandomImageViewHolder vh = (RandomImageViewHolder) holder;
ListItemsGenerator.RandomItem randomItem = (ListItemsGenerator.RandomItem) item;
final ImageView imageView = vh.imageView;
vh.textView.setText(randomItem.randomText);
if (mTheme == Theme.BLACK) {
int bgColor = randomItem.color;
vh.itemView.setBackgroundColor(Color.BLACK);
vh.textView.setTextColor(Color.WHITE);
int invert = Color.rgb(255 - Color.red(bgColor),
255 - Color.green(bgColor),
255 - Color.blue(bgColor));
imageView.setBackgroundColor(invert);
} else {
imageView.setBackgroundColor(randomItem.color);
vh.itemView.setBackgroundColor(Color.TRANSPARENT);
vh.textView.setTextColor(Color.BLACK);
}
} else if (item.type == ListItemsGenerator.FeedListItem.ItemType.TABOOLA_MID_ITEM) {
ViewHolderTaboola viewHolderTaboola = (ViewHolderTaboola) holder;
final TaboolaWidget taboolaWidget = viewHolderTaboola.mTaboolaWidget;
if (mThemeChanged) {
taboolaWidget.reset();
taboolaWidget.post(() -> loadWidget(taboolaWidget));
mThemeChanged = false;
}
}
}
@NonNull
private ListItemsGenerator.FeedListItem getItem(int position) {
return mData.get(position);
}
@Override
public int getItemViewType(int position) {
ListItemsGenerator.FeedListItem item = getItem(position);
return item.type;
}
@Override
public int getItemCount() {
return mData.size();
}
static class ViewHolderTaboola extends RecyclerView.ViewHolder {
private final TaboolaWidget mTaboolaWidget;
ViewHolderTaboola(TaboolaWidget view) {
super(view);
mTaboolaWidget = view;
}
}
static class RandomImageViewHolder extends RecyclerView.ViewHolder {
final ImageView imageView;
final TextView textView;
RandomImageViewHolder(View view) {
super(view);
imageView = view.findViewById(R.id.feed_item_iv);
textView = view.findViewById(R.id.feed_item_tv);
}
}
}
}