forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableViewProxy.java
1027 lines (919 loc) · 29.4 KB
/
TableViewProxy.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui;
import java.util.ArrayList;
import java.util.HashMap;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.AsyncResult;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiUIView;
import ti.modules.titanium.ui.widget.TiUITableView;
import ti.modules.titanium.ui.widget.tableview.TableViewModel.Item;
import android.app.Activity;
import android.os.Message;
// clang-format off
@Kroll.proxy(creatableInModule = UIModule.class,
propertyAccessors = {
TiC.PROPERTY_FILTER_ATTRIBUTE,
TiC.PROPERTY_FILTER_ANCHORED,
TiC.PROPERTY_FILTER_CASE_INSENSITIVE,
TiC.PROPERTY_HEADER_TITLE,
TiC.PROPERTY_HEADER_VIEW,
TiC.PROPERTY_FOOTER_TITLE,
TiC.PROPERTY_FOOTER_VIEW,
TiC.PROPERTY_SEARCH,
TiC.PROPERTY_SEPARATOR_COLOR,
TiC.PROPERTY_SEPARATOR_STYLE,
TiC.PROPERTY_OVER_SCROLL_MODE,
TiC.PROPERTY_MIN_ROW_HEIGHT,
TiC.PROPERTY_HEADER_DIVIDERS_ENABLED,
TiC.PROPERTY_FOOTER_DIVIDERS_ENABLED,
TiC.PROPERTY_MAX_CLASSNAME,
TiC.PROPERTY_REFRESH_CONTROL,
TiC.PROPERTY_SCROLLABLE
})
// clang-format on
public class TableViewProxy extends TiViewProxy
{
private static final String TAG = "TableViewProxy";
private static final int INSERT_ROW_BEFORE = 0;
private static final int INSERT_ROW_AFTER = 1;
private static final int INSERT_SECTION_BEFORE = 0;
private static final int INSERT_SECTION_AFTER = 1;
private static final int MSG_UPDATE_VIEW = TiViewProxy.MSG_LAST_ID + 5001;
private static final int MSG_SCROLL_TO_INDEX = TiViewProxy.MSG_LAST_ID + 5002;
private static final int MSG_SET_DATA = TiViewProxy.MSG_LAST_ID + 5003;
private static final int MSG_DELETE_ROW = TiViewProxy.MSG_LAST_ID + 5004;
private static final int MSG_INSERT_ROW = TiViewProxy.MSG_LAST_ID + 5005;
private static final int MSG_APPEND_ROW = TiViewProxy.MSG_LAST_ID + 5006;
private static final int MSG_SCROLL_TO_TOP = TiViewProxy.MSG_LAST_ID + 5007;
private static final int MSG_SELECT_ROW = TiViewProxy.MSG_LAST_ID + 5008;
private static final int MSG_APPEND_SECTION = TiViewProxy.MSG_LAST_ID + 5009;
private static final int MSG_DELETE_SECTION = TiViewProxy.MSG_LAST_ID + 5010;
private static final int MSG_INSERT_SECTION = TiViewProxy.MSG_LAST_ID + 5011;
public static final String CLASSNAME_DEFAULT = "__default__";
public static final String CLASSNAME_HEADER = "__header__";
public static final String CLASSNAME_HEADERVIEW = "__headerView__";
public static final String CLASSNAME_NORMAL = "__normal__";
class RowResult
{
int sectionIndex;
TableViewSectionProxy section;
TableViewRowProxy row;
int rowIndexInSection;
}
private ArrayList<TableViewSectionProxy> localSections;
public TableViewProxy()
{
super();
defaultValues.put(TiC.PROPERTY_OVER_SCROLL_MODE, 0);
defaultValues.put(TiC.PROPERTY_SCROLLABLE, true);
// eventManager.addOnEventChangeListener(this);
}
@Override
public void handleCreationDict(KrollDict dict)
{
Object data[] = null;
if (dict.containsKey(TiC.PROPERTY_DATA)) {
Object o = dict.get(TiC.PROPERTY_DATA);
if (o != null && o instanceof Object[]) {
data = (Object[]) o;
dict.remove(TiC.PROPERTY_DATA); // don't override our data accessor
}
}
// Treat sections in the creation dict just like data. Unlike the setter, we don't
// check whether the items are sections first. This is consistent with the handling
// of the data property--a bad object is dropped silently.
if (dict.containsKey(TiC.PROPERTY_SECTIONS)) {
Object o = dict.get(TiC.PROPERTY_SECTIONS);
if (o != null && o instanceof Object[]) {
data = (Object[]) o;
dict.remove(TiC.PROPERTY_SECTIONS); // don't override our data accessor
}
}
super.handleCreationDict(dict);
if (data != null) {
processData(data);
}
}
@Override
public void setActivity(Activity activity)
{
super.setActivity(activity);
if (localSections != null) {
for (TableViewSectionProxy section : localSections) {
section.setActivity(activity);
}
}
}
@Override
public void releaseViews()
{
super.releaseViews();
if (localSections != null) {
for (TableViewSectionProxy section : localSections) {
section.releaseViews();
}
}
}
@Override
public void release()
{
super.release();
releaseViews();
if (localSections != null) {
localSections.clear();
localSections = null;
}
}
@Override
public TiUIView createView(Activity activity)
{
return new TiUITableView(this);
}
public TiUITableView getTableView()
{
return (TiUITableView) getOrCreateView();
}
@Override
public boolean fireEvent(String eventName, Object data, boolean bubbles)
{
if (data instanceof HashMap) {
// The data object may already be in use by the runtime thread
// due to a child view's event fire. Create a copy to be thread safe.
@SuppressWarnings("unchecked")
KrollDict dataCopy = new KrollDict((HashMap<String, Object>) data);
if (dataCopy.containsKey(TiC.PROPERTY_X) && dataCopy.containsKey(TiC.PROPERTY_Y)) {
double x = dataCopy.getDouble(TiC.PROPERTY_X);
double y = dataCopy.getDouble(TiC.PROPERTY_Y);
Object source = dataCopy.get(TiC.PROPERTY_SOURCE);
int index = getTableView().getTableView().getIndexFromXY(x, y);
if (index != -1 && source == this) {
Item item = getTableView().getTableView().getItemAtPosition(index);
if (item != null) {
dataCopy.put(TiC.PROPERTY_SOURCE, item.proxy);
return item.proxy.fireEvent(eventName, dataCopy, bubbles);
}
}
}
}
return super.fireEvent(eventName, data, bubbles);
}
@Kroll.method
public void updateRow(Object row, Object data, @Kroll.argument(optional = true) KrollDict options)
{
TableViewSectionProxy sectionProxy = null;
int rowIndex = -1;
if (row instanceof Number) {
RowResult rr = new RowResult();
rowIndex = ((Number) row).intValue();
locateIndex(rowIndex, rr);
sectionProxy = rr.section;
rowIndex = rr.rowIndexInSection;
} else if (row instanceof TableViewRowProxy) {
ArrayList<TableViewSectionProxy> sections = getSectionsArray();
sectionLoop:
for (int i = 0; i < sections.size(); i++) {
ArrayList<TableViewRowProxy> rows = sections.get(i).rows;
for (int j = 0; j < rows.size(); j++) {
if (rows.get(j) == row) {
sectionProxy = sections.get(i);
rowIndex = j;
break sectionLoop;
}
}
}
}
TableViewRowProxy rowProxy = rowProxyFor(data);
if (rowProxy == null) {
Log.e(TAG, "Unable to update row. Invalid type for row: " + data);
return;
}
if (sectionProxy != null) {
sectionProxy.updateRowAt(rowIndex, rowProxy);
updateView();
} else {
Log.e(TAG, "Unable to update row. Non-existent row: " + row);
}
}
@Kroll.method
public void updateSection(Number index, Object data, @Kroll.argument(optional = true) KrollDict options)
{
int sectionIndex = index.intValue();
TableViewSectionProxy sectionProxy = sectionProxyFor(data);
if (sectionProxy == null) {
Log.e(TAG, "Unable to update section. Invalid type for section: " + data);
return;
}
try {
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
TableViewSectionProxy oldSection = currentSections.get(sectionIndex);
currentSections.set(sectionIndex, sectionProxy);
if (sectionProxy != oldSection) {
sectionProxy.setParent(this);
if (oldSection.getParent() == this) {
oldSection.setParent(null);
}
}
updateView();
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, "Unable to update section. Index out of range. Non-existent section at " + index);
}
}
// options argument exists in order to maintain parity with iOS, do not remove
@Kroll.method
public void appendRow(Object rows, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleAppendRow(rows);
return;
}
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_APPEND_ROW), rows);
}
private void handleAppendRow(Object rows)
{
Object[] rowList = null;
if (rows instanceof Object[]) {
rowList = (Object[]) rows;
} else {
rowList = new Object[] { rows };
}
ArrayList<TableViewSectionProxy> sections = getSectionsArray();
if (sections.size() == 0) {
processData(rowList);
} else {
for (int i = 0; i < rowList.length; i++) {
TableViewRowProxy rowProxy = rowProxyFor(rowList[i]);
TableViewSectionProxy lastSection = sections.get(sections.size() - 1);
TableViewSectionProxy addedToSection = addRowToSection(rowProxy, lastSection);
if (lastSection == null || !lastSection.equals(addedToSection)) {
sections.add(addedToSection);
addedToSection.setParent(this);
}
}
}
updateView();
}
@Kroll.method
public void appendSection(Object sections, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleAppendSection(sections);
return;
}
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_APPEND_SECTION), sections);
}
private void handleAppendSection(Object sections)
{
Object[] sectionList = null;
if (sections instanceof Object[]) {
sectionList = (Object[]) sections;
} else {
sectionList = new Object[] { sections };
}
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
for (int i = 0; i < sectionList.length; i++) {
TableViewSectionProxy sectionProxy = sectionProxyFor(sectionList[i]);
if (sectionProxy != null) {
currentSections.add(sectionProxy);
sectionProxy.setParent(this);
}
}
updateView();
}
@Kroll.method
public void deleteRow(Object row, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleDeleteRow(row);
return;
}
Object asyncResult = TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_DELETE_ROW), row);
if (asyncResult instanceof IllegalStateException) {
throw(IllegalStateException) asyncResult;
}
}
private void setModelDirtyIfNecessary()
{
TiUITableView nativeTableViewReference = ((TiUITableView) peekView());
if (nativeTableViewReference != null) {
nativeTableViewReference.setModelDirty();
}
}
private void handleDeleteRow(Object row) throws IllegalStateException
{
if (row instanceof Integer) {
int index = (Integer) row;
RowResult rr = new RowResult();
if (locateIndex(index, rr)) {
rr.section.removeRowAt(rr.rowIndexInSection);
updateView();
} else {
Log.e(TAG, "Unable to delete row. Index out of range. Non-existent row at " + index);
}
} else if (row instanceof TableViewRowProxy) {
TableViewRowProxy rowProxy = (TableViewRowProxy) row;
TiViewProxy section = rowProxy.getParent();
if (section instanceof TableViewSectionProxy) {
((TableViewSectionProxy) section).remove(rowProxy);
updateView();
} else {
Log.e(TAG, "Unable to delete row. The row is not added to the table yet.");
}
} else {
Log.e(TAG, "Unable to delete row. Invalid type of row: " + row);
}
}
@Kroll.method
public void deleteSection(int index, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleDeleteSection(index);
return;
}
Object asyncResult =
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_DELETE_SECTION), index);
if (asyncResult instanceof IllegalStateException) {
Log.e(TAG, ((IllegalStateException) asyncResult).getMessage());
}
}
private void handleDeleteSection(int index) throws IllegalStateException
{
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
try {
TableViewSectionProxy section = currentSections.get(index);
currentSections.remove(index);
if (section.getParent() == this) {
section.setParent(null);
}
updateView();
} catch (IndexOutOfBoundsException e) {
throw new IllegalStateException("Unable to delete section. Index out of range. Non-existent section at "
+ index);
}
}
@Kroll.method
public int getIndexByName(String name)
{
int index = -1;
int idx = 0;
if (name != null) {
for (TableViewSectionProxy section : getSections()) {
for (TableViewRowProxy row : section.getRows()) {
String rname = TiConvert.toString(row.getProperty(TiC.PROPERTY_NAME));
if (rname != null && name.equals(rname)) {
index = idx;
break;
}
idx++;
}
if (index > -1) {
break;
}
}
}
return index;
}
@Kroll.method
public void insertRowBefore(int index, Object data, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleInsertRowBefore(index, data);
return;
}
Object asyncResult = TiMessenger.sendBlockingMainMessage(
getMainHandler().obtainMessage(MSG_INSERT_ROW, INSERT_ROW_BEFORE, index), data);
if (asyncResult instanceof IllegalStateException) {
throw(IllegalStateException) asyncResult;
}
}
private void handleInsertRowBefore(int index, Object data) throws IllegalStateException
{
if (getSectionsArray().size() > 0) {
if (index < 0) {
index = 0;
}
RowResult rr = new RowResult();
if (locateIndex(index, rr)) {
TableViewRowProxy rowProxy = rowProxyFor(data);
rr.section.insertRowAt(rr.rowIndexInSection, rowProxy);
} else {
throw new IllegalStateException("Index out of range. Non-existent row at " + index);
}
} else {
// Add first row.
Object[] args = { rowProxyFor(data) };
processData(args);
}
updateView();
}
@Kroll.method
public void insertSectionBefore(int index, Object data, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleInsertSectionBefore(index, data);
return;
}
Object asyncResult = TiMessenger.sendBlockingMainMessage(
getMainHandler().obtainMessage(MSG_INSERT_SECTION, INSERT_SECTION_BEFORE, index), data);
if (asyncResult instanceof IllegalStateException) {
Log.e(TAG, ((IllegalStateException) asyncResult).getMessage());
}
}
private void handleInsertSectionBefore(int index, Object data) throws IllegalStateException
{
TableViewSectionProxy sectionProxy = sectionProxyFor(data);
if (sectionProxy == null) {
throw new IllegalStateException("Unable to insert section. Invalid type for section: " + data);
}
try {
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
currentSections.add(index, sectionProxy);
sectionProxy.setParent(this);
updateView();
} catch (IndexOutOfBoundsException e) {
throw new IllegalStateException("Unable to insert section. Index out of range. Non-existent row at "
+ index);
}
}
@Kroll.method
public void insertRowAfter(int index, Object data, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleInsertRowAfter(index, data);
return;
}
Object asyncResult = TiMessenger.sendBlockingMainMessage(
getMainHandler().obtainMessage(MSG_INSERT_ROW, INSERT_ROW_AFTER, index), data);
if (asyncResult instanceof IllegalStateException) {
throw(IllegalStateException) asyncResult;
}
}
private void handleInsertRowAfter(int index, Object data) throws IllegalStateException
{
RowResult rr = new RowResult();
if (locateIndex(index, rr)) {
// TODO check for section
TableViewRowProxy rowProxy = rowProxyFor(data);
rr.section.insertRowAt(rr.rowIndexInSection + 1, rowProxy);
updateView();
} else {
throw new IllegalStateException("Index out of range. Non-existent row at " + index);
}
}
@Kroll.method
public void insertSectionAfter(int index, Object data, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
handleInsertSectionAfter(index, data);
return;
}
Object asyncResult = TiMessenger.sendBlockingMainMessage(
getMainHandler().obtainMessage(MSG_INSERT_SECTION, INSERT_SECTION_AFTER, index), data);
if (asyncResult instanceof IllegalStateException) {
Log.e(TAG, ((IllegalStateException) asyncResult).getMessage());
}
}
private void handleInsertSectionAfter(int index, Object data) throws IllegalStateException
{
TableViewSectionProxy sectionProxy = sectionProxyFor(data);
if (sectionProxy == null) {
throw new IllegalStateException("Unable to insert section. Invalid type for section: " + data);
}
if (index < 0) {
throw new IllegalStateException("Unable to insert section. Index out of range. Non-existent row at "
+ index);
}
try {
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
currentSections.add(index + 1, sectionProxy);
sectionProxy.setParent(this);
updateView();
} catch (IndexOutOfBoundsException e) {
throw new IllegalStateException("Unable to insert section. Index out of range. Non-existent row at "
+ index);
}
}
// clang-format off
@Kroll.method
@Kroll.getProperty
public TableViewSectionProxy[] getSections()
// clang-format on
{
ArrayList<TableViewSectionProxy> sections = getSectionsArray();
return sections.toArray(new TableViewSectionProxy[sections.size()]);
}
// clang-format off
@Kroll.method
@Kroll.getProperty
public int getSectionCount()
// clang-format on
{
ArrayList<TableViewSectionProxy> sections = getSectionsArray();
return sections.size();
}
public ArrayList<TableViewSectionProxy> getSectionsArray()
{
ArrayList<TableViewSectionProxy> sections = localSections;
if (sections == null) {
sections = new ArrayList<TableViewSectionProxy>();
localSections = sections;
}
return sections;
}
/**
* If the row does not carry section information, it will be added
* to the currentSection. If it does carry section information (i.e., a header),
* that section will be created and the row added to it. Either way,
* whichever section the row gets added to will be returned.
*/
private TableViewSectionProxy addRowToSection(TableViewRowProxy row, TableViewSectionProxy currentSection)
{
TableViewSectionProxy addedToSection = null;
if (currentSection == null || row.hasProperty(TiC.PROPERTY_HEADER)) {
addedToSection = new TableViewSectionProxy();
} else {
addedToSection = currentSection;
}
if (row.hasProperty(TiC.PROPERTY_HEADER)) {
addedToSection.setProperty(TiC.PROPERTY_HEADER_TITLE, row.getProperty(TiC.PROPERTY_HEADER));
}
if (row.hasProperty(TiC.PROPERTY_FOOTER)) {
addedToSection.setProperty(TiC.PROPERTY_FOOTER_TITLE, row.getProperty(TiC.PROPERTY_FOOTER));
}
addedToSection.add(row);
return addedToSection;
}
public void processData(Object[] data)
{
ArrayList<TableViewSectionProxy> sections = getSectionsArray();
cleanupSections();
TableViewSectionProxy currentSection = null;
if (hasProperty(TiC.PROPERTY_HEADER_TITLE)) {
currentSection = new TableViewSectionProxy();
currentSection.setActivity(getActivity());
sections.add(currentSection);
currentSection.setParent(this);
currentSection.setProperty(TiC.PROPERTY_HEADER_TITLE, getProperty(TiC.PROPERTY_HEADER_TITLE));
}
if (hasProperty(TiC.PROPERTY_FOOTER_TITLE)) {
if (currentSection == null) {
currentSection = new TableViewSectionProxy();
currentSection.setActivity(getActivity());
sections.add(currentSection);
currentSection.setParent(this);
}
currentSection.setProperty(TiC.PROPERTY_FOOTER_TITLE, getProperty(TiC.PROPERTY_FOOTER_TITLE));
}
for (int i = 0; i < data.length; i++) {
Object o = data[i];
if (o instanceof HashMap || o instanceof TableViewRowProxy) {
TableViewRowProxy rowProxy = rowProxyFor(o);
TableViewSectionProxy addedToSection = addRowToSection(rowProxy, currentSection);
if (currentSection == null || !currentSection.equals(addedToSection)) {
currentSection = addedToSection;
sections.add(currentSection);
currentSection.setParent(this);
}
} else if (o instanceof TableViewSectionProxy) {
currentSection = (TableViewSectionProxy) o;
sections.add(currentSection);
currentSection.setParent(this);
}
}
}
private void cleanupSections()
{
ArrayList<TableViewSectionProxy> sections = getSectionsArray();
for (TableViewSectionProxy section : sections) {
section.releaseViews();
section.setParent(null);
}
sections.clear();
}
// clang-format off
@Kroll.method
@Kroll.setProperty
public void setData(Object[] args)
// clang-format on
{
Object[] data = args;
if (args != null && args.length > 0 && args[0] instanceof Object[]) {
data = (Object[]) args[0];
}
if (TiApplication.isUIThread()) {
handleSetData(data);
} else {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_DATA), data);
}
}
// clang-format off
@Kroll.method
@Kroll.setProperty
public void setSections(Object[] args)
// clang-format on
{
Object[] data = args;
if (args != null && args.length > 0 && args[0] instanceof Object[]) {
data = (Object[]) args[0];
}
for (Object section : data) {
if (!(section instanceof TableViewSectionProxy)) {
Log.e(TAG, "Unable to set sections. Invalid type for section: " + section);
return;
}
}
if (TiApplication.isUIThread()) {
handleSetData(data);
} else {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_DATA), data);
}
}
private void handleSetData(Object[] data)
{
if (data != null) {
processData(data);
updateView();
}
}
// clang-format off
@Kroll.method
@Kroll.getProperty
public Object[] getData()
// clang-format on
{
ArrayList<TableViewSectionProxy> sections = getSectionsArray();
if (sections != null) {
return sections.toArray();
}
return new Object[0];
}
@SuppressWarnings("unchecked")
private TableViewRowProxy rowProxyFor(Object row)
{
TableViewRowProxy rowProxy = null;
if (row instanceof TableViewRowProxy) {
rowProxy = (TableViewRowProxy) row;
rowProxy.setProperty(TiC.PROPERTY_ROW_DATA, new KrollDict(rowProxy.getProperties()));
} else {
KrollDict rowDict = null;
if (row instanceof KrollDict) {
rowDict = (KrollDict) row;
} else if (row instanceof HashMap) {
rowDict = new KrollDict((HashMap<String, Object>) row);
}
if (rowDict != null) {
rowProxy = new TableViewRowProxy();
rowProxy.setCreationUrl(creationUrl.getNormalizedUrl());
rowProxy.handleCreationDict(rowDict);
rowProxy.setProperty(TiC.PROPERTY_CLASS_NAME, CLASSNAME_NORMAL);
rowProxy.setCreationProperties(rowDict);
rowProxy.setProperty(TiC.PROPERTY_ROW_DATA, row);
rowProxy.setActivity(getActivity());
}
}
if (rowProxy == null) {
Log.e(
TAG,
"Unable to create table view row proxy for object, likely an error in the type of the object passed in...");
return null;
}
return rowProxy;
}
@SuppressWarnings("unchecked")
private TableViewSectionProxy sectionProxyFor(Object section)
{
TableViewSectionProxy sectionProxy = null;
if (section instanceof TableViewSectionProxy) {
sectionProxy = (TableViewSectionProxy) section;
sectionProxy.setActivity(getActivity());
} else {
KrollDict sectionDict = null;
if (section instanceof KrollDict) {
sectionDict = (KrollDict) section;
} else if (section instanceof HashMap) {
sectionDict = new KrollDict((HashMap<String, Object>) section);
}
if (sectionDict != null) {
sectionProxy = new TableViewSectionProxy();
if (sectionDict.containsKey(TiC.PROPERTY_HEADER_TITLE)) {
sectionProxy.setProperty(TiC.PROPERTY_HEADER_TITLE, sectionDict.get(TiC.PROPERTY_HEADER_TITLE));
}
if (sectionDict.containsKey(TiC.PROPERTY_FOOTER_TITLE)) {
sectionProxy.setProperty(TiC.PROPERTY_FOOTER_TITLE, sectionDict.get(TiC.PROPERTY_FOOTER_TITLE));
}
if (sectionDict.containsKey(TiC.PROPERTY_HEADER_VIEW)) {
sectionProxy.setProperty(TiC.PROPERTY_HEADER_VIEW, sectionDict.get(TiC.PROPERTY_HEADER_VIEW));
}
if (sectionDict.containsKey(TiC.PROPERTY_FOOTER_VIEW)) {
sectionProxy.setProperty(TiC.PROPERTY_FOOTER_VIEW, sectionDict.get(TiC.PROPERTY_FOOTER_VIEW));
}
sectionProxy.setActivity(getActivity());
}
}
if (sectionProxy == null) {
Log.e(
TAG,
"Unable to create table view section proxy for object, likely an error in the type of the object passed in...");
return null;
}
return sectionProxy;
}
private boolean locateIndex(int index, RowResult rowResult)
{
boolean found = false;
int rowCount = 0;
int sectionIndex = 0;
if (index < 0) {
return found;
}
for (TableViewSectionProxy section : getSections()) {
int sectionRowCount = (int) section.getRowCount();
if (sectionRowCount + rowCount > index) {
rowResult.section = section;
rowResult.sectionIndex = sectionIndex;
TableViewRowProxy[] rowsInSection = section.getRows();
int rowIndexInSection = index - rowCount;
if (rowIndexInSection >= 0 && rowIndexInSection < rowsInSection.length) {
rowResult.row = rowsInSection[rowIndexInSection];
rowResult.rowIndexInSection = rowIndexInSection;
found = true;
break;
}
} else {
rowCount += sectionRowCount;
}
sectionIndex += 1;
}
return found;
}
public void updateView()
{
if (TiApplication.isUIThread()) {
setModelDirtyIfNecessary();
return;
}
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_UPDATE_VIEW));
}
@Kroll.method
public void scrollToIndex(int index)
{
Message message = getMainHandler().obtainMessage(MSG_SCROLL_TO_INDEX);
// Message msg = getUIHandler().obtainMessage(MSG_SCROLL_TO_INDEX);
message.arg1 = index;
message.sendToTarget();
}
@Kroll.method
public void selectRow(int row_id)
{
Message message = getMainHandler().obtainMessage(MSG_SELECT_ROW);
message.arg1 = row_id;
message.sendToTarget();
}
@Kroll.method
public void scrollToTop(int index)
{
Message message = getMainHandler().obtainMessage(MSG_SCROLL_TO_TOP);
// Message msg = getUIHandler().obtainMessage(MSG_SCROLL_TO_TOP);
message.arg1 = index;
message.sendToTarget();
}
@Override
public boolean handleMessage(Message msg)
{
TiUITableView tableNativeView = ((TiUITableView) peekView());
boolean tableNativeViewCreated = (tableNativeView != null);
AsyncResult result = null;
Object asyncResult = null;
switch (msg.what) {
case MSG_UPDATE_VIEW:
setModelDirtyIfNecessary();
result = (AsyncResult) msg.obj;
if (tableNativeViewCreated) {
tableNativeView.updateView();
}
break;
case MSG_SCROLL_TO_INDEX:
if (tableNativeViewCreated) {
tableNativeView.scrollToIndex(msg.arg1);
}
return true;
case MSG_SET_DATA:
result = ((AsyncResult) msg.obj);
Object[] data = (Object[]) result.getArg();
handleSetData(data);
break;
case MSG_INSERT_ROW:
result = ((AsyncResult) msg.obj);
try {
if (msg.arg1 == INSERT_ROW_AFTER) {
handleInsertRowAfter(msg.arg2, result.getArg());
} else {
handleInsertRowBefore(msg.arg2, result.getArg());
}
result = (AsyncResult) msg.obj;
} catch (IllegalStateException e) {
asyncResult = e;
e.printStackTrace();
}
break;
case MSG_APPEND_ROW:
result = ((AsyncResult) msg.obj);
handleAppendRow(result.getArg());
break;
case MSG_DELETE_ROW:
result = ((AsyncResult) msg.obj);
try {
handleDeleteRow(result.getArg());
} catch (IllegalStateException e) {
asyncResult = e;
e.printStackTrace();
}
break;
case MSG_INSERT_SECTION:
result = ((AsyncResult) msg.obj);
try {
if (msg.arg1 == INSERT_SECTION_AFTER) {
handleInsertSectionAfter(msg.arg2, result.getArg());
} else {
handleInsertSectionBefore(msg.arg2, result.getArg());
}
} catch (IllegalStateException e) {
asyncResult = e;
e.printStackTrace();
}
break;
case MSG_APPEND_SECTION:
result = ((AsyncResult) msg.obj);
handleAppendSection(result.getArg());
break;
case MSG_DELETE_SECTION:
result = ((AsyncResult) msg.obj);
try {
handleDeleteSection((Integer) result.getArg());
} catch (IllegalStateException e) {
asyncResult = e;
e.printStackTrace();
}
break;
case MSG_SCROLL_TO_TOP:
if (tableNativeViewCreated) {
tableNativeView.scrollToTop(msg.arg1);
}
break;
case MSG_SELECT_ROW:
if (tableNativeViewCreated) {
tableNativeView.selectRow(msg.arg1);
}
break;
default:
return super.handleMessage(msg);
}
if (result != null) {
result.setResult(asyncResult);
}
return true;
}
// labels only send out click events when they are explicitly told to do so.
// we need to tell each label child to enable clicks when a click listener is added
@Override
public void eventListenerAdded(String eventName, int count, KrollProxy proxy)
{
super.eventListenerAdded(eventName, count, proxy);
if (eventName.equals(TiC.EVENT_CLICK) && proxy == this) {