1+ package cn .colintree .aix .SwipeRefresh ;
2+
3+ import com .google .appinventor .components .annotations .DesignerComponent ;
4+ import com .google .appinventor .components .annotations .DesignerProperty ;
5+ import com .google .appinventor .components .annotations .SimpleEvent ;
6+ import com .google .appinventor .components .annotations .SimpleFunction ;
7+ import com .google .appinventor .components .annotations .SimpleObject ;
8+ import com .google .appinventor .components .annotations .SimpleProperty ;
9+ import com .google .appinventor .components .annotations .UsesLibraries ;
10+ import com .google .appinventor .components .common .ComponentCategory ;
11+ import com .google .appinventor .components .common .PropertyTypeConstants ;
12+ import com .google .appinventor .components .runtime .AndroidNonvisibleComponent ;
13+ import com .google .appinventor .components .runtime .AndroidViewComponent ;
14+ import com .google .appinventor .components .runtime .Component ;
15+ import com .google .appinventor .components .runtime .ComponentContainer ;
16+ import com .google .appinventor .components .runtime .EventDispatcher ;
17+ import com .google .appinventor .components .runtime .ListView ;
18+ import com .google .appinventor .components .runtime .VerticalScrollArrangement ;
19+ import com .google .appinventor .components .runtime .util .YailList ;
20+
21+ import android .content .res .Resources ;
22+ import android .support .v4 .widget .SwipeRefreshLayout ;
23+ import android .util .Log ;
24+ import android .view .View ;
25+ import android .view .ViewGroup ;
26+ import android .R .color ;
27+
28+ @ DesignerComponent (version = SwipeRefresh .VERSION ,
29+ description = "by ColinTree at http://aix.colintree.cn" ,
30+ category = ComponentCategory .EXTENSION ,
31+ nonVisible = true ,
32+ iconName = "aiwebres/icon.png" )
33+
34+ @ SimpleObject (external = true )
35+ @ UsesLibraries (libraries = "support-v4.aar" )
36+ public class SwipeRefresh extends AndroidNonvisibleComponent implements Component {
37+
38+ public static final int VERSION = 2 ;
39+
40+ private static final String LOG_TAG = "SwipeRefresh" ;
41+
42+ private ComponentContainer container ;
43+ private Resources res ;
44+
45+ private SwipeRefreshLayout srl ;
46+
47+ private boolean enabled = true ;
48+ private boolean nestedScrollingEnabled = true ;
49+ private boolean large = false ;
50+ private boolean scale = true ;
51+ private int dragStart = 0 ;
52+ private int dragEnd = 150 ;
53+ private int backgroundColor = 0xFFFAFAFA ;
54+ private YailList colorList ;
55+
56+ public SwipeRefresh (ComponentContainer container ) {
57+ super (container .$form ());
58+ Log .d (LOG_TAG , "SwipeRefresh Created" );
59+
60+ this .container = container ;
61+ res = container .$context ().getResources ();
62+
63+ ColorList (YailList .makeList (new Object []{
64+ _Color_holo_blue_bright (),
65+ _Color_holo_green_light (),
66+ _Color_holo_orange_light (),
67+ _Color_holo_red_light ()
68+ }));
69+ }
70+
71+ @ SimpleEvent
72+ public void Refresh () {
73+ EventDispatcher .dispatchEvent (this , "Refresh" );
74+ }
75+
76+ @ SimpleFunction
77+ public void CancelRefreshing () {
78+ if (srl != null ) {
79+ Refreshing (false );
80+ }
81+ }
82+
83+ @ SimpleProperty
84+ public void Refreshing (boolean refreshing ) {
85+ if (srl != null ) {
86+ srl .setRefreshing (refreshing );
87+ }
88+ }
89+ @ SimpleProperty
90+ public boolean Refreshing () {
91+ if (srl != null ) {
92+ return srl .isRefreshing ();
93+ }
94+ return false ;
95+ }
96+
97+ @ SimpleProperty
98+ @ DesignerProperty (editorType = PropertyTypeConstants .PROPERTY_TYPE_BOOLEAN , defaultValue = "True" )
99+ public void Enabled (boolean enabled ) {
100+ this .enabled = enabled ;
101+ if (srl != null ) {
102+ srl .setEnabled (enabled );
103+ }
104+ }
105+ @ SimpleProperty
106+ public boolean Enabled () {
107+ return enabled ;
108+ }
109+
110+ @ SimpleProperty
111+ @ DesignerProperty (editorType = PropertyTypeConstants .PROPERTY_TYPE_BOOLEAN , defaultValue = "True" )
112+ public void NestedScrollingEnabled (boolean enabled ) {
113+ this .nestedScrollingEnabled = enabled ;
114+ if (srl != null ) {
115+ srl .setNestedScrollingEnabled (enabled );
116+ }
117+ }
118+ @ SimpleProperty
119+ public boolean NestedScrollingEnabled () {
120+ if (srl != null ) {
121+ return srl .isNestedScrollingEnabled ();
122+ }
123+ return nestedScrollingEnabled ;
124+ }
125+
126+ @ SimpleProperty
127+ @ DesignerProperty (editorType = PropertyTypeConstants .PROPERTY_TYPE_BOOLEAN , defaultValue = "False" )
128+ public void SizeLarge (boolean large ) {
129+ this .large = large ;
130+ if (srl != null ) {
131+ srl .setSize (large ? SwipeRefreshLayout .LARGE : SwipeRefreshLayout .DEFAULT );
132+ }
133+ }
134+ @ SimpleProperty
135+ public boolean SizeLarge () {
136+ return large ;
137+ }
138+
139+ @ SimpleProperty
140+ @ DesignerProperty (editorType = PropertyTypeConstants .PROPERTY_TYPE_BOOLEAN , defaultValue = "True" )
141+ public void DragScale (boolean scale ) {
142+ this .scale = scale ;
143+ if (srl != null ) {
144+ srl .setProgressViewOffset (DragScale (), DragStart (), DragStart ());
145+ }
146+ }
147+ @ SimpleProperty
148+ public boolean DragScale () {
149+ return scale ;
150+ }
151+
152+ @ SimpleProperty
153+ @ DesignerProperty (editorType = PropertyTypeConstants .PROPERTY_TYPE_NON_NEGATIVE_INTEGER , defaultValue = "0" )
154+ public void DragStart (int dragStart ) {
155+ this .dragStart = dragStart ;
156+ if (srl != null ) {
157+ srl .setProgressViewOffset (DragScale (), DragStart (), DragStart ());
158+ }
159+ }
160+ @ SimpleProperty
161+ public int DragStart () {
162+ return dragStart ;
163+ }
164+
165+ @ SimpleProperty
166+ @ DesignerProperty (editorType = PropertyTypeConstants .PROPERTY_TYPE_NON_NEGATIVE_INTEGER , defaultValue = "150" )
167+ public void DragEnd (int dragEnd ) {
168+ this .dragEnd = dragEnd ;
169+ if (srl != null ) {
170+ srl .setProgressViewOffset (DragScale (), DragStart (), DragEnd ());
171+ }
172+ }
173+ @ SimpleProperty
174+ public int DragEnd () {
175+ return dragEnd ;
176+ }
177+
178+ @ SimpleProperty (description = "have to use the color that provided here" )
179+ @ DesignerProperty (editorType = PropertyTypeConstants .PROPERTY_TYPE_COLOR , defaultValue = Component .DEFAULT_VALUE_COLOR_DEFAULT )
180+ public void BackgroundColor (int color ) {
181+ if (color == Component .COLOR_DEFAULT ) {
182+ color = 0xFFFAFAFA ;
183+ }
184+ this .backgroundColor = color ;
185+ if (srl != null ) {
186+ srl .setProgressBackgroundColorSchemeColor (color );
187+ }
188+ }
189+ @ SimpleProperty
190+ public int BackgroundColor () {
191+ return backgroundColor ;
192+ }
193+
194+ @ SimpleProperty
195+ public void ColorList (YailList list ) {
196+ if (list == null ) {
197+ return ;
198+ }
199+ if (srl != null ) {
200+ this .colorList = list ;
201+ int [] color = new int [list .size ()];
202+ for (int i = list .size ()-1 ; i >= 0 ; i --) { //avoid calling size() for many times
203+ color [i ] = Integer .parseInt (list .getString (i ));
204+ }
205+ srl .setColorSchemeColors (color );
206+ }
207+ }
208+ @ SimpleProperty
209+ public YailList ColorList () {
210+ return colorList ;
211+ }
212+
213+ @ SimpleFunction (description = "Vertical Scroll Arrangement allowed only" )
214+ public void RegisterArrangement (VerticalScrollArrangement arrangement ) {
215+ register (arrangement );
216+ }
217+ @ SimpleFunction (description = "Vertical Scroll Arrangement allowed only" )
218+ public void RegisterListView (ListView listView ) {
219+ register (listView );
220+ }
221+
222+ private void register (AndroidViewComponent component ) {
223+ if (srl != null ) {
224+ return ;
225+ }
226+ srl = new SwipeRefreshLayout (container .$context ());
227+
228+ Enabled (Enabled ());
229+ NestedScrollingEnabled (NestedScrollingEnabled ());
230+ DragScale (DragScale ());
231+ DragStart (DragStart ());
232+ DragEnd (DragEnd ());
233+ SizeLarge (SizeLarge ());
234+ BackgroundColor (BackgroundColor ());
235+ ColorList (ColorList ());
236+
237+ srl .setOnRefreshListener (new SwipeRefreshLayout .OnRefreshListener () {
238+ @ Override
239+ public void onRefresh () {
240+ Refresh ();
241+ }
242+ });
243+
244+ View child = component .getView ();
245+ ViewGroup vg = (ViewGroup ) child .getParent ();
246+ if (vg .getChildCount () <= 0 ) {
247+ return ;
248+ //check, though it is impossible
249+ }
250+ vg .addView (srl , vg .indexOfChild (child ));
251+ vg .removeView (child );
252+ srl .addView (child );
253+ }
254+
255+ @ SimpleFunction
256+ public int _Color_holo_blue_bright () {
257+ return res .getColor (color .holo_blue_bright );
258+ }
259+ @ SimpleFunction
260+ public int _Color_holo_blue_dark () {
261+ return res .getColor (color .holo_blue_dark );
262+ }
263+ @ SimpleFunction
264+ public int _Color_holo_blue_light () {
265+ return res .getColor (color .holo_blue_light );
266+ }
267+ @ SimpleFunction
268+ public int _Color_holo_green_dark () {
269+ return res .getColor (color .holo_green_dark );
270+ }
271+ @ SimpleFunction
272+ public int _Color_holo_green_light () {
273+ return res .getColor (color .holo_green_light );
274+ }
275+ @ SimpleFunction
276+ public int _Color_holo_orange_dark () {
277+ return res .getColor (color .holo_orange_dark );
278+ }
279+ @ SimpleFunction
280+ public int _Color_holo_orange_light () {
281+ return res .getColor (color .holo_orange_light );
282+ }
283+ @ SimpleFunction
284+ public int _Color_holo_purple () {
285+ return res .getColor (color .holo_purple );
286+ }
287+ @ SimpleFunction
288+ public int _Color_holo_red_dark () {
289+ return res .getColor (color .holo_red_dark );
290+ }
291+ @ SimpleFunction
292+ public int _Color_holo_red_light () {
293+ return res .getColor (color .holo_red_light );
294+ }
295+ }
0 commit comments