/
TableViewRowProxy.java
584 lines (499 loc) Β· 14.1 KB
/
TableViewRowProxy.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
/**
* 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;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.view.TiUIView;
import android.app.Activity;
import android.view.View;
import androidx.annotation.NonNull;
import ti.modules.titanium.ui.widget.TiView;
import ti.modules.titanium.ui.widget.tableview.TableViewHolder;
import ti.modules.titanium.ui.widget.tableview.TiTableView;
@Kroll.proxy(
creatableInModule = UIModule.class,
propertyAccessors = {
TiC.PROPERTY_HAS_CHECK,
TiC.PROPERTY_HAS_CHILD,
TiC.PROPERTY_HAS_DETAIL,
TiC.PROPERTY_EDITABLE,
TiC.PROPERTY_MOVABLE,
TiC.PROPERTY_MOVEABLE,
TiC.PROPERTY_CLASS_NAME,
TiC.PROPERTY_LAYOUT,
TiC.PROPERTY_LEFT_IMAGE,
TiC.PROPERTY_RIGHT_IMAGE,
TiC.PROPERTY_TITLE,
TiC.PROPERTY_HEADER_TITLE,
TiC.PROPERTY_HEADER,
TiC.PROPERTY_HEADER_VIEW,
TiC.PROPERTY_FOOTER_TITLE,
TiC.PROPERTY_FOOTER,
TiC.PROPERTY_FOOTER_VIEW
}
)
public class TableViewRowProxy extends TiViewProxy
{
private static final String TAG = "TableViewRowProxy";
public int index;
private int filteredIndex = -1;
private TableViewHolder holder;
private boolean placeholder = false;
private boolean selected = false;
// FIXME: On iOS the same row can be added to a table multiple times.
// Due to constraints, we need to create a new proxy and track changes.
private final List<WeakReference<TableViewRowProxy>> clones = new ArrayList<>(0);
public TableViewRowProxy()
{
super();
}
public TableViewRowProxy(boolean placeholder)
{
this();
// Determine if row is placeholder for header or footer.
this.placeholder = placeholder;
}
/**
* Create clone of existing proxy.
*
* @return TableViewRowProxy
*/
@Override
public TableViewRowProxy clone()
{
final TableViewRowProxy proxy = (TableViewRowProxy) super.clone();
// Reference clone, to update properties.
clones.add(new WeakReference<>(proxy));
return proxy;
}
/**
* Generate RowView for current proxy.
*
* @param activity the context activity.
* @return TiUIView of row.
*/
@Override
public TiUIView createView(Activity activity)
{
if (placeholder) {
// Placeholder for header or footer, do not create view.
return null;
}
return new RowView(this);
}
/**
* Handle event data to generate payload with table data.
*
* @param eventName Name of fired event.
* @param data Data payload of fired event.
* @return Object of payload.
*/
private Object handleEvent(String eventName, Object data)
{
// Inject row data into events.
final TableViewProxy tableViewProxy = getTableViewProxy();
if (tableViewProxy != null) {
final KrollDict payload = data instanceof HashMap
? new KrollDict((HashMap<String, Object>) data) : new KrollDict();
final TiTableView tableView = tableViewProxy.getTableView();
payload.put(TiC.PROPERTY_ROW_DATA, properties);
if (getParent() instanceof TableViewSectionProxy) {
payload.put(TiC.PROPERTY_SECTION, getParent());
}
payload.put(TiC.EVENT_PROPERTY_ROW, this);
payload.put(TiC.EVENT_PROPERTY_INDEX, index);
if (tableView != null) {
payload.put(TiC.EVENT_PROPERTY_SEARCH_MODE, tableView.isFiltered());
}
data = payload;
}
return data;
}
/**
* Override fireEvent to inject row 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 Returns true if the event was successfully fired to listener(s)
*/
@Override
public boolean fireEvent(String eventName, Object data, boolean bubbles)
{
data = handleEvent(eventName, data);
return super.fireEvent(eventName, data, bubbles);
}
@Override
public boolean fireSyncEvent(String eventName, Object data, boolean bubbles)
{
data = handleEvent(eventName, data);
return super.fireSyncEvent(eventName, data, bubbles);
}
@Override
public String getApiName()
{
return "Ti.UI.TableViewRow";
}
/**
* 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 TableViewHolder for row.
*
* @return TableViewHolder
*/
public TableViewHolder getHolder()
{
return this.holder;
}
/**
* Set new TableViewHolder for item.
*
* @param holder TableViewHolder to set.
*/
public void setHolder(TableViewHolder holder)
{
this.holder = holder;
// Update to new holder.
final RowView row = (RowView) getOrCreateView();
if (row != null) {
// Reset native view to our holder.
row.setNativeView(this.holder.getNativeView());
// Register touch events.
row.registerForTouch();
// Grab latest `nativeView`.
final View nativeView = row.getNativeView();
// Reset opacity of view.
nativeView.setAlpha(1.0f);
// Apply proxy properties.
row.processProperties(this.properties);
}
}
/**
* Override getRect() to amend dimensions.
*
* @return Dictinary of view dimensions.
*/
@Override
public KrollDict getRect()
{
View view = null;
if (this.holder != null) {
view = this.holder.getNativeView();
}
return getViewRect(view);
}
/**
* Get item index in section.
*
* @return Integer of index.
*/
public int getIndexInSection()
{
final TiViewProxy parent = getParent();
if (parent instanceof TableViewSectionProxy) {
final TableViewSectionProxy section = (TableViewSectionProxy) parent;
return section.getRowIndex(this);
}
return -1;
}
/**
* Get related TableView proxy for item.
*
* @return TableViewProxy
*/
public TableViewProxy getTableViewProxy()
{
TiViewProxy parent = getParent();
while (!(parent instanceof TableViewProxy) && parent != null) {
parent = parent.getParent();
}
return (TableViewProxy) parent;
}
/**
* Determine if row is currently selected.
*
* @return Boolean of selection status.
*/
public boolean isSelected()
{
return selected;
}
/**
* Set row selection status.
*/
public void setSelected(boolean selected)
{
this.selected = selected;
}
/**
* Handle initial creation properties.
*
* @param options Initial creation properties.
*/
@Override
public void handleCreationDict(KrollDict options)
{
if (options == null) {
options = new KrollDict();
}
// TableViewRow.header and TableViewRow.footer have been deprecated.
if (options.containsKey(TiC.PROPERTY_HEADER)) {
headerDeprecationLog();
}
if (options.containsKey(TiC.PROPERTY_FOOTER)) {
footerDeprecationLog();
}
if (options.containsKeyAndNotNull(TiC.PROPERTY_HEADER_VIEW)) {
final TiViewProxy headerProxy = (TiViewProxy) options.get(TiC.PROPERTY_HEADER_VIEW);
// Set header view parent, so it can be released correctly.
headerProxy.setParent(this);
}
if (options.containsKeyAndNotNull(TiC.PROPERTY_FOOTER_VIEW)) {
final TiViewProxy footerProxy = (TiViewProxy) options.get(TiC.PROPERTY_FOOTER_VIEW);
// Set header view parent, so it can be released correctly.
footerProxy.setParent(this);
}
super.handleCreationDict(options);
}
/**
* Deprecation log for 'TableViewRow.header' usage.
*/
private void headerDeprecationLog()
{
Log.w(TAG, "Usage of 'TableViewRow.header' has been deprecated, use 'TableViewRow.headerTitle' instead.");
}
/**
* Deprecation log for 'TableViewRow.footer' usage.
*/
private void footerDeprecationLog()
{
Log.w(TAG, "Usage of 'TableViewRow.footer' has been deprecated, use 'TableViewRow.footerTitle' instead.");
}
/**
* 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);
for (final WeakReference ref : clones) {
final TableViewRowProxy clone = (TableViewRowProxy) ref.get();
if (ref != null && clone != null) {
clone.onPropertyChanged(name, value);
}
}
}
/**
* Process property set on proxy.
*
* @param name Property name.
* @param value Property value.
*/
private void processProperty(String name, Object value)
{
final TableViewProxy tableViewProxy = getTableViewProxy();
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);
}
// TableViewRow.header and TableViewRow.footer have been deprecated.
if (name.equals(TiC.PROPERTY_HEADER)) {
headerDeprecationLog();
}
if (name.equals(TiC.PROPERTY_FOOTER)) {
footerDeprecationLog();
}
// 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);
invalidate();
}
if (name.equals(TiC.PROPERTY_LEFT_IMAGE)
|| name.equals(TiC.PROPERTY_RIGHT_IMAGE)
|| name.equals(TiC.PROPERTY_HAS_CHECK)
|| name.equals(TiC.PROPERTY_HAS_CHILD)
|| name.equals(TiC.PROPERTY_HAS_DETAIL)
|| name.equals(TiC.PROPERTY_BACKGROUND_COLOR)
|| name.equals(TiC.PROPERTY_BACKGROUND_IMAGE)
|| name.equals(TiC.PROPERTY_SELECTED_BACKGROUND_COLOR)
|| name.equals(TiC.PROPERTY_BACKGROUND_SELECTED_COLOR)
|| name.equals(TiC.PROPERTY_SELECTED_BACKGROUND_IMAGE)
|| name.equals(TiC.PROPERTY_BACKGROUND_SELECTED_IMAGE)
|| name.equals(TiC.PROPERTY_TITLE)
|| name.equals(TiC.PROPERTY_COLOR)
|| name.equals(TiC.PROPERTY_FONT)
|| name.equals(TiC.PROPERTY_LEFT)
|| name.equals(TiC.PROPERTY_RIGHT)
|| name.equals(TiC.PROPERTY_TOP)
|| name.equals(TiC.PROPERTY_BOTTOM)
|| name.equals(TiC.PROPERTY_MOVABLE)) {
// Force re-bind of row.
invalidate();
}
}
/**
* Release row views.
*/
@Override
public void releaseViews()
{
this.holder = null;
final KrollDict properties = getProperties();
if (properties.containsKeyAndNotNull(TiC.PROPERTY_HEADER_VIEW)) {
final TiViewProxy header = (TiViewProxy) properties.get(TiC.PROPERTY_HEADER_VIEW);
if (header.getParent() == this) {
header.releaseViews();
}
}
if (properties.containsKeyAndNotNull(TiC.PROPERTY_FOOTER_VIEW)) {
final TiViewProxy footer = (TiViewProxy) properties.get(TiC.PROPERTY_FOOTER_VIEW);
if (footer.getParent() == this) {
footer.releaseViews();
}
}
final RowView rowView = (RowView) peekView();
if (rowView != null) {
// Set `nativeView` back to original content to release correctly.
rowView.setNativeView(rowView.getContent());
}
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);
}
/**
* RowView class used for table row.
* Auto-fills to width of parent.
*/
public static class RowView extends TiView
{
View content;
public RowView(TiViewProxy proxy)
{
super(proxy);
getLayoutParams().autoFillsWidth = true;
// Maintain a reference to our original row contents.
// Since we adjust native view.
this.content = this.nativeView;
}
public View getContent()
{
return this.content;
}
public void setNativeView(View view)
{
if (this.content == null) {
this.content = view;
}
super.setNativeView(view);
}
@Override
public void add(TiUIView child)
{
// TODO: This could be improved to prevent the need for swapping native views.
// Our `nativeView` is currently set as our TableViewHolder view as a workaround
// for allowing events/properties to set on our holder instead of our row content.
// Temporarily swap our native view back to original content while new child is added.
final View nativeView = getNativeView();
if (nativeView != null) {
setNativeView(this.content);
super.add(child);
setNativeView(nativeView);
} else {
super.add(child);
}
}
@Override
public void remove(TiUIView child)
{
// TODO: This could be improved to prevent the need for swapping native views.
// Our `nativeView` is currently set as our TableViewHolder view as a workaround
// for allowing events/properties to set on our holder instead of our row content.
// Temporarily swap our native view back to original content while new child is removed.
final View nativeView = getNativeView();
if (nativeView != null) {
setNativeView(this.content);
super.remove(child);
setNativeView(nativeView);
} else {
super.remove(child);
}
}
protected boolean canApplyTouchFeedback(@NonNull KrollDict props)
{
// Prevent TiUIView from overriding `touchFeedback` effect.
return false;
}
@Override
protected boolean hasBorder(KrollDict d)
{
// Always create custom background drawable.
return true;
}
}
}