/
SwipeRefresh.java
295 lines (266 loc) · 9.28 KB
/
SwipeRefresh.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
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
package cn.colintree.aix.SwipeRefresh;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesLibraries;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.ListView;
import com.google.appinventor.components.runtime.VerticalScrollArrangement;
import com.google.appinventor.components.runtime.util.YailList;
import android.content.res.Resources;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.R.color;
@DesignerComponent(version = SwipeRefresh.VERSION,
description = "by ColinTree at http://aix.colintree.cn",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "aiwebres/icon.png")
@SimpleObject(external = true)
@UsesLibraries(libraries = "support-v4.aar")
public class SwipeRefresh extends AndroidNonvisibleComponent implements Component {
public static final int VERSION = 2;
private static final String LOG_TAG = "SwipeRefresh";
private ComponentContainer container;
private Resources res;
private SwipeRefreshLayout srl;
private boolean enabled = true;
private boolean nestedScrollingEnabled = true;
private boolean large = false;
private boolean scale = true;
private int dragStart = 0;
private int dragEnd = 150;
private int backgroundColor = 0xFFFAFAFA;
private YailList colorList;
public SwipeRefresh(ComponentContainer container) {
super(container.$form());
Log.d(LOG_TAG, "SwipeRefresh Created");
this.container = container;
res = container.$context().getResources();
ColorList(YailList.makeList(new Object[]{
_Color_holo_blue_bright(),
_Color_holo_green_light(),
_Color_holo_orange_light(),
_Color_holo_red_light()
}));
}
@SimpleEvent
public void Refresh() {
EventDispatcher.dispatchEvent(this, "Refresh");
}
@SimpleFunction
public void CancelRefreshing() {
if (srl != null) {
Refreshing(false);
}
}
@SimpleProperty
public void Refreshing(boolean refreshing) {
if (srl != null) {
srl.setRefreshing(refreshing);
}
}
@SimpleProperty
public boolean Refreshing() {
if (srl != null) {
return srl.isRefreshing();
}
return false;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "True")
public void Enabled(boolean enabled) {
this.enabled = enabled;
if (srl != null) {
srl.setEnabled(enabled);
}
}
@SimpleProperty
public boolean Enabled() {
return enabled;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "True")
public void NestedScrollingEnabled(boolean enabled) {
this.nestedScrollingEnabled = enabled;
if (srl != null) {
srl.setNestedScrollingEnabled(enabled);
}
}
@SimpleProperty
public boolean NestedScrollingEnabled() {
if (srl != null) {
return srl.isNestedScrollingEnabled();
}
return nestedScrollingEnabled;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "False")
public void SizeLarge(boolean large) {
this.large = large;
if (srl != null) {
srl.setSize(large ? SwipeRefreshLayout.LARGE : SwipeRefreshLayout.DEFAULT);
}
}
@SimpleProperty
public boolean SizeLarge() {
return large;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "True")
public void DragScale(boolean scale) {
this.scale = scale;
if (srl != null) {
srl.setProgressViewOffset(DragScale(), DragStart(), DragStart());
}
}
@SimpleProperty
public boolean DragScale() {
return scale;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER, defaultValue = "0")
public void DragStart(int dragStart) {
this.dragStart = dragStart;
if (srl != null) {
srl.setProgressViewOffset(DragScale(), DragStart(), DragStart());
}
}
@SimpleProperty
public int DragStart() {
return dragStart;
}
@SimpleProperty
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER, defaultValue = "150")
public void DragEnd(int dragEnd) {
this.dragEnd = dragEnd;
if (srl != null) {
srl.setProgressViewOffset(DragScale(), DragStart(), DragEnd());
}
}
@SimpleProperty
public int DragEnd() {
return dragEnd;
}
@SimpleProperty(description = "have to use the color that provided here")
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_COLOR, defaultValue = Component.DEFAULT_VALUE_COLOR_DEFAULT)
public void BackgroundColor(int color) {
if(color == Component.COLOR_DEFAULT) {
color = 0xFFFAFAFA;
}
this.backgroundColor = color;
if (srl != null) {
srl.setProgressBackgroundColorSchemeColor(color);
}
}
@SimpleProperty
public int BackgroundColor() {
return backgroundColor;
}
@SimpleProperty
public void ColorList(YailList list) {
if(list == null) {
return;
}
if (srl != null) {
this.colorList = list;
int[] color = new int[list.size()];
for(int i = list.size()-1; i >= 0; i--) { //avoid calling size() for many times
color[i] = Integer.parseInt(list.getString(i));
}
srl.setColorSchemeColors(color);
}
}
@SimpleProperty
public YailList ColorList() {
return colorList;
}
@SimpleFunction(description = "Vertical Scroll Arrangement allowed only")
public void RegisterArrangement(VerticalScrollArrangement arrangement) {
register(arrangement);
}
@SimpleFunction(description = "Vertical Scroll Arrangement allowed only")
public void RegisterListView(ListView listView) {
register(listView);
}
private void register(AndroidViewComponent component) {
if (srl != null) {
return;
}
srl = new SwipeRefreshLayout(container.$context());
Enabled(Enabled());
NestedScrollingEnabled(NestedScrollingEnabled());
DragScale(DragScale());
DragStart(DragStart());
DragEnd(DragEnd());
SizeLarge(SizeLarge());
BackgroundColor(BackgroundColor());
ColorList(ColorList());
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Refresh();
}
});
View child = component.getView();
ViewGroup vg = (ViewGroup) child.getParent();
if (vg.getChildCount() <= 0) {
return;
//check, though it is impossible
}
vg.addView(srl, vg.indexOfChild(child));
vg.removeView(child);
srl.addView(child);
}
@SimpleFunction
public int _Color_holo_blue_bright() {
return res.getColor(color.holo_blue_bright);
}
@SimpleFunction
public int _Color_holo_blue_dark() {
return res.getColor(color.holo_blue_dark);
}
@SimpleFunction
public int _Color_holo_blue_light() {
return res.getColor(color.holo_blue_light);
}
@SimpleFunction
public int _Color_holo_green_dark() {
return res.getColor(color.holo_green_dark);
}
@SimpleFunction
public int _Color_holo_green_light() {
return res.getColor(color.holo_green_light);
}
@SimpleFunction
public int _Color_holo_orange_dark() {
return res.getColor(color.holo_orange_dark);
}
@SimpleFunction
public int _Color_holo_orange_light() {
return res.getColor(color.holo_orange_light);
}
@SimpleFunction
public int _Color_holo_purple() {
return res.getColor(color.holo_purple);
}
@SimpleFunction
public int _Color_holo_red_dark() {
return res.getColor(color.holo_red_dark);
}
@SimpleFunction
public int _Color_holo_red_light() {
return res.getColor(color.holo_red_light);
}
}