-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ListItemProxy.java
914 lines (775 loc) Β· 25.7 KB
/
ListItemProxy.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
/**
* Titanium SDK
* Copyright TiDev, Inc. 04/07/2022-Present. 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.widget.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeSet;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollEventCallback;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollObject;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.ColorProxy;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiUIView;
import android.app.Activity;
import android.view.View;
import androidx.annotation.NonNull;
import ti.modules.titanium.ui.UIModule;
import ti.modules.titanium.ui.widget.TiView;
@Kroll.proxy(
creatableInModule = ti.modules.titanium.ui.UIModule.class,
propertyAccessors = {
TiC.PROPERTY_ACCESSORY_TYPE,
TiC.PROPERTY_CAN_EDIT,
TiC.PROPERTY_CAN_MOVE,
TiC.PROPERTY_ITEM_ID,
TiC.PROPERTY_SEARCHABLE_TEXT
}
)
public class ListItemProxy extends TiViewProxy
{
private static final String TAG = "ListItemProxy";
private final HashMap<String, TiViewProxy> binds = new HashMap<>();
private final HashMap<String, Object> childProperties = new HashMap<>();
private final List<String> ignoredTemplateKeys = new ArrayList<>();
public int index;
private int filteredIndex = -1;
private ListViewHolder holder;
private KrollDict template;
private String templateId;
private boolean placeholder = false;
private boolean hasAddedItemEvents = false;
private boolean selected = false;
public ListItemProxy()
{
super();
defaultValues.put(TiC.PROPERTY_ACCESSORY_TYPE, UIModule.LIST_ACCESSORY_TYPE_NONE);
}
public ListItemProxy(boolean placeholder)
{
this();
// Determine if item is placeholder for header or footer.
this.placeholder = placeholder;
}
/**
* Generate ListItem view from specified template.
*
* @param activity the context activity.
* @return TiUIView of ListItem.
*/
@Override
public TiUIView createView(Activity activity)
{
// Do not continue if this ListItem is a placeholder for a header/footer.
if (placeholder) {
return null;
}
// Fetch template assigned to this ListItem.
loadTemplate();
if (this.template == null) {
return null;
}
// Apply template defined properties to this ListItem.
final KrollDict templateProperties = this.template.getKrollDict(TiC.PROPERTY_PROPERTIES);
if (templateProperties != null) {
for (KrollDict.Entry<String, Object> entry : templateProperties.entrySet()) {
if (!this.ignoredTemplateKeys.contains(entry.getKey())) {
setProperty(entry.getKey(), entry.getValue());
}
}
}
// Generate all child proxies from template and create all views.
if (!hasChildren()) {
generateViewFromTemplate(this, this.template);
}
return new ItemView(this);
}
/**
* Handle event payload manipulation.
*
* @param eventName Name of fired event.
* @param data Data payload of fired event.
* @return Object of event payload.
*/
public Object handleEvent(String eventName, Object data, boolean fireItemClick)
{
// Inject row data into events.
final ListViewProxy listViewProxy = getListViewProxy();
if (listViewProxy != null) {
final KrollDict payload = data instanceof HashMap
? new KrollDict((HashMap<String, Object>) data) : new KrollDict();
final Object sourceObject = payload.containsKeyAndNotNull(TiC.EVENT_PROPERTY_SOURCE)
? payload.get(TiC.EVENT_PROPERTY_SOURCE) : this;
final TiViewProxy source = sourceObject instanceof TiViewProxy ? (TiViewProxy) sourceObject : this;
final Object parent = getParent();
if (parent instanceof ListSectionProxy) {
final ListSectionProxy section = (ListSectionProxy) parent;
// Include section specific properties.
payload.putIfAbsent(TiC.PROPERTY_SECTION, section);
payload.putIfAbsent(TiC.PROPERTY_SECTION_INDEX, listViewProxy.getIndexOfSection(section));
payload.putIfAbsent(TiC.PROPERTY_ITEM_INDEX, getIndexInSection());
}
final Object itemId = getProperties().get(TiC.PROPERTY_ITEM_ID);
if (itemId != null) {
// Include `itemId` if specified.
payload.put(TiC.PROPERTY_ITEM_ID, itemId);
}
for (final String key : binds.keySet()) {
if (binds.get(key).equals(source)) {
// Reverse lookup `bindId`.
// Include `bindId` of template if specified.
payload.put(TiC.PROPERTY_BIND_ID, key);
break;
}
}
final int accessoryType = getProperties().optInt(TiC.PROPERTY_ACCESSORY_TYPE,
UIModule.LIST_ACCESSORY_TYPE_NONE);
final boolean isAccessoryDetail = accessoryType == UIModule.LIST_ACCESSORY_TYPE_DETAIL;
// If item is `LIST_ACCESSORY_TYPE_DETAIL` then `accessoryClicked` will be `true`.
payload.put(TiC.EVENT_PROPERTY_ACCESSORY_CLICKED, isAccessoryDetail);
// Override data with new payload.
data = payload;
// Fire `itemclick` event on ListView.
if (fireItemClick && eventName.equals(TiC.EVENT_CLICK)) {
listViewProxy.fireSyncEvent(TiC.EVENT_ITEM_CLICK, data);
}
}
return data;
}
/**
* Override fireEvent to inject ListItem data into payload.
*
* @param eventName Name of fired event.
* @param data Data payload of fired event.
* @param bubbles Specify if event should bubble up to parent.
* @return
*/
@Override
public boolean fireEvent(String eventName, Object data, boolean bubbles)
{
data = handleEvent(eventName, data, true);
return super.fireEvent(eventName, data, bubbles);
}
@Override
public boolean fireSyncEvent(String eventName, Object data, boolean bubbles)
{
data = handleEvent(eventName, data, true);
return super.fireSyncEvent(eventName, data, bubbles);
}
/**
* Generate views from specified template.
*
* @param parent Parent proxy to add child views from template. (Creates new proxy when null)
* @param template Template to generate views from.
* @return Generated TiViewProxy.
*/
protected TiViewProxy generateViewFromTemplate(TiViewProxy parent, KrollDict template)
{
final TiViewProxy tiProxy = (TiViewProxy) template.get("tiProxy");
final KrollDict templateProperties = template.getKrollDict(TiC.PROPERTY_PROPERTIES);
final Object[] childTemplates = (Object[]) template.get(TiC.PROPERTY_CHILD_TEMPLATES);
final String bindId = template.getString(TiC.PROPERTY_BIND_ID);
if (parent == null) {
try {
// Parent undefined, create new proxy instance.
parent = tiProxy.getClass().newInstance();
final KrollDict properties = new KrollDict(tiProxy.getProperties());
if (this.childProperties.containsKey(bindId)) {
final Object childPropertiesObj = this.childProperties.get(bindId);
if (childPropertiesObj instanceof KrollDict) {
final KrollDict childProperties = (KrollDict) childPropertiesObj;
// Include child properties for specified `bindId`.
properties.putAll(childProperties);
}
}
// Create instance with new properties.
parent.setActivity(getActivity());
parent.handleCreationArgs(null, new Object[] { properties });
} catch (Exception e) {
Log.w(TAG, "Failed to create child proxy instance from template.");
}
}
if (bindId != null) {
// Include new instance binding.
binds.put(bindId, parent);
}
// Fetch event listeners from template and add them to the proxy.
addTemplateEventListeners(parent, template);
if (childTemplates != null) {
// Recursively process child templates.
for (Object o : childTemplates) {
if (o instanceof HashMap) {
final KrollDict childTemplate = new KrollDict((HashMap) o);
final TiViewProxy childView = generateViewFromTemplate(null, childTemplate);
if (childView != null) {
parent.add(childView);
}
}
}
}
return parent;
}
private void addTemplateEventListeners(final TiViewProxy proxy, KrollDict template)
{
// Validate arguments.
if ((proxy == null) || (template == null)) {
return;
}
// Fetch events assigned to given template.
final KrollDict eventDictionary = template.getKrollDict(TiC.PROPERTY_EVENTS);
if (eventDictionary == null) {
return;
}
// Make sure we only add events listeners once for the root ListItem.
// Note: ListItem's view is always re-created, unlike child views which are only created once.
if (proxy instanceof ListItemProxy) {
if (this.hasAddedItemEvents) {
return;
}
this.hasAddedItemEvents = true;
}
// Add event listeners to given proxy.
for (KrollDict.Entry<String, Object> entry : eventDictionary.entrySet()) {
if (!(entry.getValue() instanceof KrollFunction)) {
continue;
}
final String eventName = entry.getKey();
final KrollFunction callback = (KrollFunction) entry.getValue();
final KrollObject krollObject = proxy.getKrollObject();
proxy.addEventListener(eventName, new KrollEventCallback() {
@Override
public void call(Object data)
{
ListItemProxy itemProxy = getListItemContainerFor(proxy);
if (itemProxy != null) {
callback.call(krollObject, new Object[] { itemProxy.handleEvent(eventName, data, false) });
}
}
});
krollObject.setHasListenersForEventType(eventName, true);
}
}
private static ListItemProxy getListItemContainerFor(TiViewProxy proxy)
{
if (proxy == null) {
return null;
} else if (proxy instanceof ListItemProxy) {
return (ListItemProxy) proxy;
}
return getListItemContainerFor(proxy.getParent());
}
public void moveChildrenTo(ListItemProxy proxy)
{
// Validate argument.
if (proxy == null) {
return;
}
// Copy all child view proxy properties to this list item's dictionary.
// Saves view's current state when scrolled offscreen so we can restore it when scrolled back in.
// Note: Given "proxy" shouldn't have any child proxies, but do the copy just in case.
copyChildPropertiesFromProxies();
proxy.copyChildPropertiesFromProxies();
// Remove all children from given proxy. (If recycling is working as expected, it should have none.)
proxy.removeAllChildren();
// Do not continue if templates don't match. Prevents given proxy from re-using this proxy's views.
// Note: Should never happen if ListViewAdapter.getItemViewType() returns different IDs for different templates.
String templateId = getTemplateId();
if ((templateId == null) || !templateId.equals(proxy.getTemplateId())) {
return;
}
// Do not continue if this proxy has no child views.
// This will force given proxy to regenerate child proxies from template.
if (!hasChildren()) {
return;
}
TiViewProxy[] childProxies = getChildren();
if (childProxies[0].peekView() == null) {
return;
}
// Move this proxy's children to given proxy and overwrite their properties.
// Note: This also moves their native views. Updating properties will also update the native views.
proxy.loadTemplate();
proxy.copyChildPropertiesTo(childProxies, proxy.template);
proxy.add(childProxies);
// Now that this proxy has no children, release this proxy's 1 native view container.
// We do this to reduce memory footprint of all offscreen list items.
releaseViews();
// Remove all child binding IDs from this proxy except for the ListItem itself.
final String bindId = this.template.getString(TiC.PROPERTY_BIND_ID);
this.binds.clear();
if (bindId != null) {
this.binds.put(bindId, this);
}
}
private void copyChildPropertiesFromProxies()
{
for (HashMap.Entry<String, TiViewProxy> bindsEntry : this.binds.entrySet()) {
// Fetch the next child view proxy. (Skip the root ListItemProxy object.)
TiViewProxy proxy = bindsEntry.getValue();
if (proxy == this) {
continue;
}
// Do no continue if child proxy has no properties.
KrollDict proxyProperties = proxy.getProperties();
if (proxyProperties == null) {
continue;
}
// Copy all view proxy properties to this list item's dictionary.
// Saves view's current state when scrolled offscreen so we can restore it when scrolled back in.
Object object = this.childProperties.get(bindsEntry.getKey());
if (object instanceof HashMap) {
((HashMap) object).putAll(proxyProperties);
} else {
this.childProperties.put(bindsEntry.getKey(), new KrollDict(proxyProperties));
}
}
}
private void copyChildPropertiesTo(TiViewProxy[] proxies, KrollDict template)
{
// Validate arguments.
if ((proxies == null) || (proxies.length <= 0) || (template == null)) {
return;
}
// Fetch child templates nested under given template.
final Object[] childTemplates = (Object[]) template.get(TiC.PROPERTY_CHILD_TEMPLATES);
// Update all child proxies.
var excludeKeys = new TreeSet<String>();
for (int index = 0; index < proxies.length; index++) {
// Fetch child's template.
KrollDict childTemplate = null;
if ((childTemplates != null) && (index < childTemplates.length)) {
Object objectTemplate = childTemplates[index];
if (objectTemplate instanceof KrollDict) {
childTemplate = (KrollDict) objectTemplate;
} else if (objectTemplate instanceof HashMap) {
childTemplate = new KrollDict((HashMap) objectTemplate);
}
}
// Fetch child's assigned configuration.
TiViewProxy tiProxy = null;
String bindId = null;
KrollDict events = null;
if (childTemplate != null) {
tiProxy = (TiViewProxy) childTemplate.get("tiProxy");
bindId = childTemplate.getString(TiC.PROPERTY_BIND_ID);
events = childTemplate.getKrollDict(TiC.PROPERTY_EVENTS);
}
// Fetch child's properties.
TiViewProxy proxy = proxies[index];
final KrollDict properties = new KrollDict();
if (tiProxy != null) {
properties.putAll(tiProxy.getProperties());
}
final Object childPropertiesObj = this.childProperties.get(bindId);
if (childPropertiesObj instanceof HashMap) {
properties.putAll((HashMap) childPropertiesObj);
}
// Assign properties to child proxy.
excludeKeys.clear();
for (var entry : properties.entrySet()) {
// Skip property if in exclusion set. We do this for localized properties.
if (excludeKeys.contains(entry.getKey())) {
continue;
}
// Skip copying property if value isn't changing. (This drastically improves performance.)
if (!proxy.shouldFireChange(proxy.getProperty(entry.getKey()), entry.getValue())) {
continue;
}
// Do special handling for localized properties, such as "textid" or "titleid".
// Returned "pair" provides property key/value that should be changed, such as "text" or "title".
if (proxy.isLocaleProperty(entry.getKey())) {
var pair = proxy.updateLocaleProperty(entry.getKey(), TiConvert.toString(entry.getValue()));
if (pair != null) {
excludeKeys.add(pair.first);
proxy.setProperty(entry.getKey(), entry.getValue());
proxy.firePropertyChanged(pair.first, proxy.getProperty(pair.first), pair.second);
continue;
}
}
// Update property value and invoke onPropetyChanged() callback.
proxy.setPropertyAndFire(entry.getKey(), entry.getValue());
}
// Add child's binding ID to main dictionary.
if (bindId != null) {
this.binds.put(bindId, proxy);
}
// Update child's children. (This is recursive.)
copyChildPropertiesTo(proxy.getChildren(), childTemplate);
}
}
@Override
public String getApiName()
{
return "Ti.UI.ListItem";
}
/**
* Get ListDataItem dictionary.
*
* @return KrollDict
*/
public KrollDict getDataItem()
{
final KrollDict dataItem = new KrollDict();
// Include child items.
for (final String key : this.childProperties.keySet()) {
dataItem.put(key, this.childProperties.get(key));
}
// Include properties and template.
dataItem.put(TiC.PROPERTY_PROPERTIES, getProperties());
dataItem.put(TiC.PROPERTY_TEMPLATE, getTemplateId());
return dataItem;
}
/**
* Get filtered index of row in section.
*
* @return Integer of filtered index.
*/
public int getFilteredIndex()
{
return this.filteredIndex;
}
/**
* Set filtered index of row in section.
*
* @param index Filtered index to set.
*/
public void setFilteredIndex(int index)
{
this.filteredIndex = index;
}
/**
* Get current ListViewHolder for item.
*
* @return ListViewHolder
*/
public ListViewHolder getHolder()
{
return this.holder;
}
/**
* Set new ListViewHolder for item.
*
* @param holder ListViewHolder to set.
*/
public void setHolder(ListViewHolder holder)
{
this.holder = holder;
}
/**
* Get item index in section.
*
* @return Integer of index.
*/
public int getIndexInSection()
{
final TiViewProxy parent = getParent();
if (parent instanceof ListSectionProxy) {
final ListSectionProxy section = (ListSectionProxy) parent;
return section.getListItemIndex(this);
}
return -1;
}
/**
* Get related ListView proxy for item.
*
* @return ListViewProxy
*/
public ListViewProxy getListViewProxy()
{
TiViewProxy parent = getParent();
while (!(parent instanceof ListViewProxy) && parent != null) {
parent = parent.getParent();
}
return (ListViewProxy) parent;
}
/**
* Override getRect() to amend dimensions.
*
* @return Dictionary of view dimensions.
*/
@Override
public KrollDict getRect()
{
final View view = this.holder != null ? this.holder.itemView : null;
return getViewRect(view);
}
/**
* Get template identifier associated with item.
*
* @return Template identifier.
*/
public String getTemplateId()
{
if (this.templateId == null) {
loadTemplate();
}
return this.templateId;
}
/**
* Determine if item is currently selected.
*
* @return Boolean of selection status.
*/
public boolean isSelected()
{
return selected;
}
/**
* Set item selection status.
*/
public void setSelected(boolean selected)
{
this.selected = selected;
}
/**
* Handle creation from ListDataItem object.
*
* @param options ListDataItem properties.
*/
public void handleCreationDataItem(KrollDict options)
{
if (options == null) {
options = new KrollDict();
}
final KrollDict properties = options.getKrollDict(TiC.PROPERTY_PROPERTIES);
// Iterate through properties for children.
for (String k : options.keySet()) {
// Ignore reserved ListDataItem properties.
if (k.equals(TiC.PROPERTY_PROPERTIES)
|| k.equals(TiC.PROPERTY_TEMPLATE)) {
continue;
}
final KrollDict childDict = options.getKrollDict(k);
if (childDict != null) {
// Additional dictionary properties are regarded as child properties for templates.
// NOTE: `getKrollDict()` is necessary as using `get()` would return a `HashMap`.
this.childProperties.put(k, childDict);
} else {
// Non-template properties are regarded as custom properties.
this.childProperties.put(k, options.get(k));
}
}
// Set item template.
this.templateId = options.getString(TiC.PROPERTY_TEMPLATE);
// Process item properties.
handleCreationDict(properties);
}
/**
* Handle initial creation properties.
*
* @param options Initial creation properties.
*/
@Override
public void handleCreationDict(KrollDict options)
{
if (options == null) {
options = new KrollDict();
}
// Prevent template from overriding specified properties.
ignoredTemplateKeys.addAll(options.keySet());
super.handleCreationDict(options);
}
/**
* Determine if proxy contains listener for specified event.
*
* @param event Event name.
* @return Boolean determining if listener is present.
*/
@Override
public boolean hierarchyHasListener(String event)
{
// Override to always detect `click` events, even when no listeners are present.
// This is so we can fire `itemclick` events.
if (event.equals(TiC.EVENT_CLICK)) {
return true;
}
return super.hierarchyHasListener(event);
}
/**
* Invalidate item to re-bind holder.
* This will update the current holder to display new changes.
*/
public void invalidate()
{
if (this.holder != null) {
this.holder.bind(this, this.holder.itemView.isActivated());
}
}
/**
* Is current item a placeholder for header or footer.
*
* @return Boolean
*/
public boolean isPlaceholder()
{
return this.placeholder;
}
@Override
public void onPropertyChanged(String name, Object value)
{
super.onPropertyChanged(name, value);
processProperty(name, value);
}
/**
* Process property set on proxy.
*
* @param name Property name.
* @param value Property value.
*/
private void processProperty(String name, Object value)
{
// Handle convenience properties for default template.
if (name.equals(TiC.PROPERTY_TITLE)
|| name.equals(TiC.PROPERTY_FONT)
|| name.equals(TiC.PROPERTY_COLOR)) {
// Properties apply to `title` in default template.
final KrollDict titleProperties = new KrollDict();
// Handle title text.
if (name.equals(TiC.PROPERTY_TITLE) && value instanceof String) {
titleProperties.put(TiC.PROPERTY_TEXT, value);
}
// Handle title font.
if (name.equals(TiC.PROPERTY_FONT) && value instanceof HashMap) {
titleProperties.put(TiC.PROPERTY_FONT, value);
}
// Handle title color.
if (name.equals(TiC.PROPERTY_COLOR)
&& (value instanceof String || value instanceof ColorProxy)) {
titleProperties.put(TiC.PROPERTY_COLOR, value);
}
if (titleProperties.size() > 0) {
if (this.childProperties.containsKey(TiC.PROPERTY_TITLE)) {
final Object existingTitleObject = this.childProperties.get(TiC.PROPERTY_TITLE);
if (existingTitleObject instanceof KrollDict) {
final KrollDict existingTitleProperties = (KrollDict) existingTitleObject;
// Child template already exists.
// Merge new properties with existing properties.
existingTitleProperties.putAll(titleProperties);
}
} else {
// Add as child property to apply to default template.
this.childProperties.put(TiC.PROPERTY_TITLE, titleProperties);
}
}
}
if (name.equals(TiC.PROPERTY_IMAGE) && value instanceof String) {
final KrollDict imageProperties = new KrollDict();
// Handle image path.
imageProperties.put(TiC.PROPERTY_IMAGE, value);
if (this.childProperties.containsKey(TiC.PROPERTY_IMAGE)) {
final Object existingImageObject = this.childProperties.get(TiC.PROPERTY_IMAGE);
if (existingImageObject instanceof KrollDict) {
final KrollDict existingImageProperties = (KrollDict) existingImageObject;
// Child template already exists.
// Merge new properties with existing properties.
existingImageProperties.putAll(imageProperties);
}
} else {
// Add as child property to apply to default template.
this.childProperties.put(TiC.PROPERTY_IMAGE, imageProperties);
}
}
if (name.equals(TiC.PROPERTY_HEADER_VIEW) && value instanceof TiViewProxy) {
final TiViewProxy headerProxy = (TiViewProxy) value;
// Set header view parent, so it can be released correctly.
headerProxy.setParent(this);
}
if (name.equals(TiC.PROPERTY_FOOTER_VIEW) && value instanceof TiViewProxy) {
final TiViewProxy footerProxy = (TiViewProxy) value;
// Set footer view parent, so it can be released correctly.
footerProxy.setParent(this);
}
if (name.equals(TiC.PROPERTY_SELECTED_BACKGROUND_COLOR)) {
Log.w(TAG, "selectedBackgroundColor is deprecated, use backgroundSelectedColor instead.");
setProperty(TiC.PROPERTY_BACKGROUND_SELECTED_COLOR, value);
}
if (name.equals(TiC.PROPERTY_SELECTED_BACKGROUND_IMAGE)) {
Log.w(TAG, "selectedBackgroundImage is deprecated, use backgroundSelectedImage instead.");
setProperty(TiC.PROPERTY_BACKGROUND_SELECTED_IMAGE, value);
}
// Set selected color from selection style.
if (!hasPropertyAndNotNull(TiC.PROPERTY_BACKGROUND_SELECTED_COLOR)
&& name.equals(TiC.PROPERTY_SELECTION_STYLE)
&& value instanceof Integer) {
String selectionColor = null;
switch ((Integer) value) {
case UIModule.SELECTION_STYLE_NONE:
selectionColor = "transparent";
break;
}
setProperty(TiC.PROPERTY_BACKGROUND_SELECTED_COLOR, selectionColor);
}
if (name.equals(TiC.PROPERTY_CAN_MOVE)) {
invalidate();
}
}
private void loadTemplate()
{
// Fetch the ListView this ListItem has been added to.
final ListViewProxy listViewProxy = getListViewProxy();
if (listViewProxy == null) {
return;
}
// Fetch template that this ListItem should use, if not done already.
final KrollDict listViewProperties = listViewProxy.getProperties();
final KrollDict listViewTemplates = listViewProperties.getKrollDict(TiC.PROPERTY_TEMPLATES);
if (this.templateId == null) {
// Attempt to obtain ListItem `template` identifier. If not set, then use default template.
final String defaultTemplateId = listViewProperties.optString(
TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE, UIModule.LIST_ITEM_TEMPLATE_DEFAULT);
this.templateId = properties.optString(TiC.PROPERTY_TEMPLATE, defaultTemplateId);
}
if ((listViewTemplates != null) && listViewTemplates.containsKey(this.templateId)) {
// Obtain specified template for item.
this.template = listViewTemplates.getKrollDict(this.templateId);
}
}
/**
* Release views for item to reclaim memory.
*/
@Override
public void releaseViews()
{
this.holder = null;
super.releaseViews();
}
/**
* Process property set on proxy.
*
* @param name Name of proxy property.
* @param value Value to set property.
*/
@Override
public void setProperty(String name, Object value)
{
super.setProperty(name, value);
processProperty(name, value);
}
/**
* ItemView class used for ListItem.
* Auto-fills to width of parent.
*/
private static class ItemView extends TiView
{
public ItemView(TiViewProxy proxy)
{
super(proxy);
getLayoutParams().autoFillsWidth = true;
}
@Override
protected boolean canApplyTouchFeedback(@NonNull KrollDict props)
{
// Prevent TiUIView from overriding `touchFeedback` effect.
return false;
}
}
}