/
TiUIListView.m
2717 lines (2348 loc) Β· 97.9 KB
/
TiUIListView.m
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
/**
* 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.
*/
#ifdef USE_TI_UILISTVIEW
#import "TiUIListView.h"
#import "TiUILabelProxy.h"
#import "TiUIListItem.h"
#import "TiUIListItemProxy.h"
#import "TiUIListSectionProxy.h"
#import "TiUISearchBarProxy.h"
#import <TitaniumKit/TiApp.h>
#import <TitaniumKit/TiRootViewController.h>
#import <TitaniumKit/TiWindowProxy.h>
#ifdef USE_TI_UIREFRESHCONTROL
#import "TiUIRefreshControlProxy.h"
#endif
#import <MobileCoreServices/MobileCoreServices.h>
#import <TitaniumKit/ImageLoader.h>
@interface TiUIListView ()
@property (nonatomic, readonly) TiUIListViewProxy *listViewProxy;
@property (nonatomic, copy, readwrite) NSString *searchString;
@property (nonatomic, copy, readwrite) NSString *searchedString;
@end
static TiViewProxy *FindViewProxyWithBindIdContainingPoint(UIView *view, CGPoint point);
@implementation TiUIListView {
UITableView *_tableView;
NSDictionary<id, TiViewTemplate *> *_templates;
id _defaultItemTemplate;
TiDimension _rowHeight;
TiViewProxy *_headerViewProxy;
TiViewProxy *_searchWrapper;
TiViewProxy *_headerWrapper;
TiViewProxy *_footerViewProxy;
TiViewProxy *_pullViewProxy;
#ifdef USE_TI_UIREFRESHCONTROL
TiUIRefreshControlProxy *_refreshControlProxy;
#endif
TiUISearchBarProxy *searchViewProxy;
UISearchController *searchController;
UIViewController *searchControllerPresenter;
NSMutableArray<NSString *> *sectionTitles;
NSMutableArray<NSNumber *> *sectionIndices;
NSMutableArray<NSString *> *filteredTitles;
NSMutableArray<NSNumber *> *filteredIndices;
UIView *_pullViewWrapper;
CGFloat pullThreshhold;
BOOL pullActive;
CGPoint tapPoint;
BOOL editing;
BOOL pruneSections;
BOOL caseInsensitiveSearch;
NSString *_searchString;
BOOL searchActive;
BOOL keepSectionsInSearch;
NSMutableArray *_searchResults;
UIEdgeInsets _defaultSeparatorInsets;
UIEdgeInsets _rowSeparatorInsets;
NSMutableDictionary<id, TiUIListItem *> *_measureProxies;
BOOL canFireScrollStart;
BOOL canFireScrollEnd;
BOOL isScrollingToTop;
BOOL _dimsBackgroundDuringPresentation;
CGPoint tableContentOffset;
BOOL isSearched;
UIView *dimmingView;
BOOL isSearchBarInNavigation;
int lastVisibleItem;
int lastVisibleSection;
BOOL forceUpdates;
}
#ifdef TI_USE_AUTOLAYOUT
- (void)initializeTiLayoutView
{
[super initializeTiLayoutView];
[self setDefaultHeight:TiDimensionAutoFill];
[self setDefaultWidth:TiDimensionAutoFill];
}
#endif
- (id)init
{
self = [super init];
if (self) {
canFireScrollEnd = NO;
canFireScrollStart = YES;
_defaultItemTemplate = [[NSNumber numberWithUnsignedInteger:UITableViewCellStyleDefault] retain];
_defaultSeparatorInsets = UIEdgeInsetsZero;
_dimsBackgroundDuringPresentation = YES;
forceUpdates = NO;
}
return self;
}
- (void)dealloc
{
if ([searchController isActive]) {
searchController.view.hidden = YES;
[searchController setActive:NO];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
_tableView.delegate = nil;
_tableView.dataSource = nil;
_tableView.prefetchDataSource = nil;
[_tableView release];
[_templates release];
[_defaultItemTemplate release];
[_headerViewProxy setProxyObserver:nil];
[_footerViewProxy setProxyObserver:nil];
[_pullViewProxy setProxyObserver:nil];
[searchController setDelegate:nil];
[searchController setSearchResultsUpdater:nil];
RELEASE_TO_NIL(_searchString);
RELEASE_TO_NIL(_searchResults);
RELEASE_TO_NIL(_pullViewWrapper);
RELEASE_TO_NIL(_pullViewProxy);
RELEASE_TO_NIL(_headerViewProxy);
RELEASE_TO_NIL(_searchWrapper);
RELEASE_TO_NIL(_headerWrapper)
RELEASE_TO_NIL(_footerViewProxy);
RELEASE_TO_NIL(searchViewProxy);
RELEASE_TO_NIL(searchController);
RELEASE_TO_NIL(dimmingView);
RELEASE_TO_NIL(sectionTitles);
RELEASE_TO_NIL(sectionIndices);
RELEASE_TO_NIL(filteredTitles);
RELEASE_TO_NIL(filteredIndices);
RELEASE_TO_NIL(_measureProxies);
RELEASE_TO_NIL(_searchedString);
#ifdef USE_TI_UIREFRESHCONTROL
RELEASE_TO_NIL(_refreshControlProxy);
#endif
[super dealloc];
}
- (TiViewProxy *)initWrapperProxy
{
TiViewProxy *theProxy = [[TiViewProxy alloc] init];
#ifndef TI_USE_AUTOLAYOUT
LayoutConstraint *viewLayout = [theProxy layoutProperties];
viewLayout->width = TiDimensionAutoFill;
viewLayout->height = TiDimensionAutoSize;
#endif
return theProxy;
}
- (void)setHeaderFooter:(TiViewProxy *)theProxy isHeader:(BOOL)header
{
[theProxy setProxyObserver:self];
if (header) {
[self.tableView setTableHeaderView:[theProxy view]];
} else {
[self.tableView setTableFooterView:[theProxy view]];
}
[theProxy windowWillOpen];
[theProxy setParentVisible:YES];
[theProxy windowDidOpen];
}
- (void)configureFooter
{
if (_footerViewProxy == nil) {
_footerViewProxy = [self initWrapperProxy];
[self setHeaderFooter:_footerViewProxy isHeader:NO];
}
}
- (void)configureHeaders
{
_headerViewProxy = [self initWrapperProxy];
#ifndef TI_USE_AUTOLAYOUT
LayoutConstraint *viewLayout = [_headerViewProxy layoutProperties];
viewLayout->layoutStyle = TiLayoutRuleVertical;
#endif
[self setHeaderFooter:_headerViewProxy
isHeader:YES];
_searchWrapper = [self initWrapperProxy];
_headerWrapper = [self initWrapperProxy];
isSearchBarInNavigation = [TiUtils boolValue:[(TiViewProxy *)self.proxy valueForUndefinedKey:@"showSearchBarInNavBar"] def:NO];
if (!isSearchBarInNavigation) {
[_headerViewProxy add:_searchWrapper];
}
[_headerViewProxy add:_headerWrapper];
}
- (UITableView *)tableView
{
if (_tableView == nil) {
UITableViewStyle style = [TiUtils intValue:[self.proxy valueForKey:@"style"] def:UITableViewStylePlain];
BOOL requiresEditingToMove = [TiUtils boolValue:[self.proxy valueForKey:@"requiresEditingToMove"] def:YES];
forceUpdates = [TiUtils boolValue:[self.proxy valueForKey:@"forceUpdates"] def:NO];
_tableView = [[UITableView alloc] initWithFrame:self.bounds style:style];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tableView.delegate = self;
_tableView.dataSource = self;
if (!requiresEditingToMove) {
_tableView.dragDelegate = self;
_tableView.dropDelegate = self;
_tableView.dragInteractionEnabled = YES;
}
// Fixes incorrect heights in iOS 11 as we calculate them internally already
_tableView.estimatedRowHeight = 0;
_tableView.estimatedSectionFooterHeight = 0;
_tableView.estimatedSectionHeaderHeight = 0;
_tableView.prefetchDataSource = self;
if (TiDimensionIsDip(_rowHeight)) {
[_tableView setRowHeight:_rowHeight.value];
}
id backgroundColor = [self.proxy valueForKey:@"backgroundColor"];
BOOL doSetBackground = YES;
if (style == UITableViewStyleGrouped) {
doSetBackground = (backgroundColor != nil);
}
if (doSetBackground) {
[[self class] setBackgroundColor:[TiUtils colorValue:backgroundColor] onTable:_tableView];
}
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapGestureRecognizer.delegate = self;
[_tableView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
[self configureHeaders];
_defaultSeparatorInsets = [_tableView separatorInset];
if (style == UITableViewStyleGrouped || style == UITableViewStylePlain) {
_tableView.layoutMargins = UIEdgeInsetsZero;
}
_tableView.cellLayoutMarginsFollowReadableWidth = NO;
}
if ([_tableView superview] != self) {
[self addSubview:_tableView];
}
return _tableView;
}
- (void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
{
if (![searchController isActive]) {
[searchViewProxy ensureSearchBarHierarchy];
#ifndef TI_USE_AUTOLAYOUT
if (_searchWrapper != nil) {
CGFloat rowWidth = [self computeRowWidth:_tableView];
if (rowWidth > 0) {
CGFloat right = _tableView.bounds.size.width - rowWidth;
[_searchWrapper layoutProperties]->right = TiDimensionDip(right);
}
}
#endif
} else {
[_tableView reloadData];
}
[super frameSizeChanged:frame bounds:bounds];
if (_headerViewProxy != nil) {
[_headerViewProxy parentSizeWillChange];
}
if (_footerViewProxy != nil) {
[_footerViewProxy parentSizeWillChange];
}
if (_pullViewWrapper != nil) {
_pullViewWrapper.frame = CGRectMake(0.0f, 0.0f - bounds.size.height, bounds.size.width, bounds.size.height);
[_pullViewProxy parentSizeWillChange];
}
if ([searchController isActive]) {
[self updateSearchControllerFrames];
}
}
- (id)accessibilityElement
{
return self.tableView;
}
- (TiUIListViewProxy *)listViewProxy
{
return (TiUIListViewProxy *)self.proxy;
}
- (void)deselectAll:(BOOL)animated
{
if (_tableView != nil) {
[_tableView.indexPathsForSelectedRows enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
[_tableView deselectRowAtIndexPath:indexPath animated:animated];
}];
}
}
- (void)updateIndicesForVisibleRows
{
if (_tableView == nil || [self isSearchActive]) {
return;
}
NSArray *visibleRows = [_tableView indexPathsForVisibleRows];
[visibleRows enumerateObjectsUsingBlock:^(NSIndexPath *vIndexPath, NSUInteger idx, BOOL *stop) {
UITableViewCell *theCell = [_tableView cellForRowAtIndexPath:vIndexPath];
if ([theCell isKindOfClass:[TiUIListItem class]]) {
((TiUIListItem *)theCell).proxy.indexPath = vIndexPath;
[((TiUIListItem *)theCell) ensureVisibleSelectorWithTableView:_tableView];
}
}];
}
- (void)proxyDidRelayout:(id)sender
{
TiThreadPerformOnMainThread(
^{
if (sender == _headerViewProxy) {
UIView *headerView = [[self tableView] tableHeaderView];
[headerView setFrame:[headerView bounds]];
[[self tableView] setTableHeaderView:headerView];
[((TiUIListViewProxy *)[self proxy]) contentsWillChange];
} else if (sender == _footerViewProxy) {
UIView *footerView = [[self tableView] tableFooterView];
[footerView setFrame:[footerView bounds]];
[[self tableView] setTableFooterView:footerView];
[((TiUIListViewProxy *)[self proxy]) contentsWillChange];
} else if (sender == _pullViewProxy) {
pullThreshhold = ([_pullViewProxy view].frame.origin.y - _pullViewWrapper.bounds.size.height);
}
},
NO);
}
- (void)setContentOffset_:(id)value withObject:(id)args
{
CGPoint offset = [TiUtils pointValue:value];
BOOL animated = [TiUtils boolValue:[args valueForKey:@"animated"] def:NO];
[_tableView setContentOffset:offset animated:animated];
}
- (void)setContentInsets_:(id)value withObject:(id)props
{
UIEdgeInsets insets = [TiUtils contentInsets:value];
BOOL animated = [TiUtils boolValue:@"animated" properties:props def:NO];
void (^setInset)(void) = ^{
[_tableView setContentInset:insets];
};
if (animated) {
double duration = [TiUtils doubleValue:@"duration" properties:props def:300] / 1000;
[UIView animateWithDuration:duration animations:setInset];
} else {
setInset();
}
}
- (void)setDictTemplates_:(id)args
{
ENSURE_TYPE_OR_NIL(args, NSDictionary);
[[self proxy] replaceValue:args forKey:@"dictTemplates" notification:NO];
[_templates release];
_templates = [args copy];
RELEASE_TO_NIL(_measureProxies);
_measureProxies = [[NSMutableDictionary alloc] init];
NSEnumerator *enumerator = [_templates keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
id template = [_templates objectForKey:key];
if (template != nil) {
TiUIListItemProxy *theProxy = [[TiUIListItemProxy alloc] initWithListViewProxy:self.listViewProxy inContext:self.listViewProxy.pageContext];
TiUIListItem *cell = [[TiUIListItem alloc] initWithProxy:theProxy reuseIdentifier:@"__measurementCell__"];
[theProxy unarchiveFromTemplate:template];
[_measureProxies setObject:cell forKey:key];
[theProxy setIndexPath:[NSIndexPath indexPathForRow:-1 inSection:-1]];
[cell release];
[theProxy release];
}
}
if (_tableView != nil) {
[_tableView reloadData];
}
}
- (TiUIView *)sectionView:(NSInteger)section forLocation:(NSString *)location section:(TiUIListSectionProxy **)sectionResult
{
TiUIListSectionProxy *proxy = [self.listViewProxy sectionForIndex:section];
//In the event that proxy is nil, this all flows out to returning nil safely anyways.
if (sectionResult != nil) {
*sectionResult = proxy;
}
TiViewProxy *viewproxy = [proxy valueForKey:location];
if (viewproxy != nil && [viewproxy isKindOfClass:[TiViewProxy class]]) {
#ifndef TI_USE_AUTOLAYOUT
LayoutConstraint *viewLayout = [viewproxy layoutProperties];
//If height is not dip, explicitly set it to SIZE
if (viewLayout->height.type != TiDimensionTypeDip) {
viewLayout->height = TiDimensionAutoSize;
}
#endif
TiUIView *theView = [viewproxy view];
if (![viewproxy viewAttached]) {
[viewproxy windowWillOpen];
[viewproxy willShow];
[viewproxy windowDidOpen];
}
return theView;
}
return nil;
}
- (void)add:(id)arg
{
NSLog(@"[ERROR] Cannot add sub-views to list views. Use \"appendSection\" instead.");
}
#pragma mark - Helper Methods
- (CGFloat)computeRowWidth:(UITableView *)tableView
{
if (tableView == nil) {
return 0;
}
CGFloat rowWidth = tableView.bounds.size.width;
// Apple does not provide a good way to get information about the index sidebar size
// in the event that it exists - it silently resizes row content which is "flexible width"
// but this is not useful for us. This is a problem when we have Ti.UI.SIZE/FILL behavior
// on row contents, which rely on the height of the row to be accurately precomputed.
//
// The following is unreliable since it uses a private API name, but one which has existed
// since iOS 3.0. The alternative is to grab a specific subview of the tableview itself,
// which is more fragile.
if ((sectionTitles == nil) || ([searchController isActive])) {
return rowWidth;
}
NSArray *subviews = [tableView subviews];
if ([subviews count] > 0) {
// Obfuscate private class name
Class indexview = NSClassFromString([@"UITableView" stringByAppendingString:@"Index"]);
for (UIView *view in subviews) {
if ([view isKindOfClass:indexview]) {
rowWidth -= [view frame].size.width;
}
}
}
return floorf(rowWidth);
}
- (id)valueWithKey:(NSString *)key atIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *item = [[self.listViewProxy sectionForIndex:indexPath.section] itemAtIndex:indexPath.row];
id propertiesValue = [item objectForKey:@"properties"];
NSDictionary *properties = ([propertiesValue isKindOfClass:[NSDictionary class]]) ? propertiesValue : nil;
id theValue = [properties objectForKey:key];
if (theValue == nil) {
id templateId = [item objectForKey:@"template"];
if (templateId == nil) {
templateId = _defaultItemTemplate;
}
if (![templateId isKindOfClass:[NSNumber class]]) {
TiViewTemplate *template = [_templates objectForKey:templateId];
theValue = [template.properties objectForKey:key];
}
}
return theValue;
}
- (void)buildResultsForSearchText
{
searchActive = ([self.searchString length] > 0);
RELEASE_TO_NIL(filteredIndices);
RELEASE_TO_NIL(filteredTitles);
if (searchActive) {
BOOL hasResults = NO;
//Initialize
if (_searchResults == nil) {
_searchResults = [[NSMutableArray alloc] init];
}
//Clear Out
[_searchResults removeAllObjects];
//Search Options
NSStringCompareOptions searchOpts = (caseInsensitiveSearch ? NSCaseInsensitiveSearch : 0);
NSUInteger maxSection = [[self.listViewProxy sectionCount] unsignedIntegerValue];
NSMutableArray *singleSection = keepSectionsInSearch ? nil : [[NSMutableArray alloc] init];
for (int i = 0; i < maxSection; i++) {
NSMutableArray *thisSection = keepSectionsInSearch ? [[NSMutableArray alloc] init] : nil;
NSUInteger maxItems = [[self.listViewProxy sectionForIndex:i] itemCount];
for (int j = 0; j < maxItems; j++) {
NSIndexPath *thePath = [NSIndexPath indexPathForRow:j inSection:i];
id theValue = [self valueWithKey:@"searchableText" atIndexPath:thePath];
BOOL alwaysInclude = [TiUtils boolValue:[self valueWithKey:@"filterAlwaysInclude" atIndexPath:thePath] def:NO];
if (alwaysInclude || (theValue != nil && [[TiUtils stringValue:theValue] rangeOfString:self.searchString options:searchOpts].location != NSNotFound)) {
(thisSection != nil) ? [thisSection addObject:thePath] : [singleSection addObject:thePath];
hasResults = YES;
}
}
if (thisSection != nil) {
[_searchResults addObject:thisSection];
if (sectionTitles != nil && sectionIndices != nil) {
NSNumber *theIndex = [NSNumber numberWithInt:i];
if ([sectionIndices containsObject:theIndex]) {
NSString *theTitle = [sectionTitles objectAtIndex:[sectionIndices indexOfObject:theIndex]];
if (filteredTitles == nil) {
filteredTitles = [[NSMutableArray alloc] init];
}
if (filteredIndices == nil) {
filteredIndices = [[NSMutableArray alloc] init];
}
[filteredTitles addObject:theTitle];
[filteredIndices addObject:NUMUINTEGER([_searchResults count] - 1)];
}
}
[thisSection release];
}
}
if (singleSection != nil) {
if ([singleSection count] > 0) {
[_searchResults addObject:singleSection];
}
[singleSection release];
}
if (!hasResults) {
if ([(TiViewProxy *)self.proxy _hasListeners:@"noresults" checkParent:NO]) {
[self.proxy fireEvent:@"noresults" withObject:nil propagate:NO reportSuccess:NO errorCode:0 message:nil];
}
}
} else {
RELEASE_TO_NIL(_searchResults);
}
}
- (BOOL)isSearchActive
{
return searchActive || [searchController isActive];
}
- (void)updateSearchResults:(id)unused
{
if (searchActive) {
[self buildResultsForSearchText];
}
[_tableView reloadData];
}
- (NSIndexPath *)pathForSearchPath:(NSIndexPath *)indexPath
{
if (_searchResults != nil && [_searchResults count] > indexPath.section) {
NSArray *sectionResults = [_searchResults objectAtIndex:indexPath.section];
if ([sectionResults count] > indexPath.row) {
return [sectionResults objectAtIndex:indexPath.row];
}
}
return indexPath;
}
- (NSInteger)sectionForSearchSection:(NSInteger)section
{
if (_searchResults != nil) {
NSArray *sectionResults = [_searchResults objectAtIndex:section];
NSIndexPath *thePath = [sectionResults objectAtIndex:0];
return thePath.section;
}
return section;
}
- (void)updateSearchControllerFrames
{
if (![searchController isActive]) {
return;
}
if (isSearchBarInNavigation) {
dimmingView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
} else {
dimmingView.frame = CGRectMake(0, self.safeAreaInsets.top + searchController.searchBar.frame.size.height, self.frame.size.width, self.frame.size.height - searchController.searchBar.frame.size.height);
CGPoint convertedOrigin = [self.superview convertPoint:self.frame.origin toView:searchControllerPresenter.view];
UIView *searchSuperView = [searchController.view superview];
searchSuperView.frame = CGRectMake(convertedOrigin.x, convertedOrigin.y, self.frame.size.width, self.frame.size.height);
CGFloat width = [_searchWrapper view].frame.size.width;
UIView *view = searchController.searchBar.superview;
view.frame = CGRectMake(0, 0, width, view.frame.size.height);
searchController.searchBar.frame = CGRectMake(0, 0, width, searchController.searchBar.frame.size.height);
[searchViewProxy ensureSearchBarHierarchy];
}
}
#pragma mark - Public API
- (void)setSeparatorInsets_:(id)arg
{
DEPRECATED_REPLACED(@"UI.ListView.separatorInsets", @"5.2.0", @"UI.ListView.listSeparatorInsets");
[self setListSeparatorInsets_:arg];
}
- (void)setTableSeparatorInsets_:(id)arg
{
DEPRECATED_REPLACED(@"UI.ListView.tableSeparatorInsets", @"5.4.0", @"UI.ListView.listSeparatorInsets");
[self setListSeparatorInsets_:arg];
}
- (void)setDimBackgroundForSearch_:(id)arg
{
ENSURE_SINGLE_ARG_OR_NIL(arg, NSNumber);
if (searchController && [TiUtils boolValue:arg def:YES]) {
[self createDimmingView];
} else {
_dimsBackgroundDuringPresentation = [TiUtils boolValue:arg def:YES];
}
}
- (void)setListSeparatorInsets_:(id)arg
{
[self tableView];
if ([arg isKindOfClass:[NSDictionary class]]) {
CGFloat left = [TiUtils floatValue:@"left" properties:arg def:_defaultSeparatorInsets.left];
CGFloat right = [TiUtils floatValue:@"right" properties:arg def:_defaultSeparatorInsets.right];
[[self tableView] setSeparatorInset:UIEdgeInsetsMake(0, left, 0, right)];
} else {
[[self tableView] setSeparatorInset:_defaultSeparatorInsets];
}
if (![searchController isActive]) {
[[self tableView] setNeedsDisplay];
}
}
- (void)setRowSeparatorInsets_:(id)arg
{
if ([arg isKindOfClass:[NSDictionary class]]) {
CGFloat left = [TiUtils floatValue:@"left" properties:arg def:_defaultSeparatorInsets.left];
CGFloat right = [TiUtils floatValue:@"right" properties:arg def:_defaultSeparatorInsets.right];
_rowSeparatorInsets = UIEdgeInsetsMake(0, left, 0, right);
}
if (![searchController isActive]) {
[[self tableView] setNeedsDisplay];
}
}
- (void)setPruneSectionsOnEdit_:(id)args
{
pruneSections = [TiUtils boolValue:args def:NO];
}
- (void)setCanScroll_:(id)args
{
UITableView *table = [self tableView];
[table setScrollEnabled:[TiUtils boolValue:args def:YES]];
}
- (void)setAllowsSelectionDuringEditing_:(id)arg
{
[[self tableView] setAllowsSelectionDuringEditing:[TiUtils boolValue:arg def:NO]];
}
- (void)setSeparatorStyle_:(id)arg
{
[[self tableView] setSeparatorStyle:[TiUtils intValue:arg]];
}
- (void)setSeparatorColor_:(id)arg
{
TiColor *color = [TiUtils colorValue:arg];
[[self tableView] setSeparatorColor:[color _color]];
}
- (void)setDefaultItemTemplate_:(id)args
{
if (![args isKindOfClass:[NSString class]] && ![args isKindOfClass:[NSNumber class]]) {
ENSURE_TYPE_OR_NIL(args, NSString);
}
[_defaultItemTemplate release];
_defaultItemTemplate = [args copy];
if (_tableView != nil) {
[_tableView reloadData];
}
}
- (void)setRowHeight_:(id)height
{
_rowHeight = [TiUtils dimensionValue:height];
if (TiDimensionIsDip(_rowHeight)) {
[_tableView setRowHeight:_rowHeight.value];
}
}
- (void)setBackgroundColor_:(id)arg
{
if (_tableView != nil) {
[[self class] setBackgroundColor:[TiUtils colorValue:arg] onTable:_tableView];
}
}
- (void)setHeaderTitle_:(id)args
{
[_headerWrapper removeAllChildren:nil];
TiViewProxy *theProxy = [[self class] titleViewForText:[TiUtils stringValue:args] inTable:[self tableView] footer:NO];
[_headerWrapper add:theProxy];
}
- (void)setFooterTitle_:(id)args
{
if (IS_NULL_OR_NIL(args)) {
[_footerViewProxy setProxyObserver:nil];
[_footerViewProxy windowWillClose];
[self.tableView setTableFooterView:nil];
[_footerViewProxy windowDidClose];
RELEASE_TO_NIL(_footerViewProxy);
[((TiUIListViewProxy *)[self proxy]) contentsWillChange];
} else {
[self configureFooter];
[_footerViewProxy removeAllChildren:nil];
TiViewProxy *theProxy = [[self class] titleViewForText:[TiUtils stringValue:args] inTable:[self tableView] footer:YES];
[_footerViewProxy add:theProxy];
}
}
- (void)setHeaderView_:(id)args
{
ENSURE_SINGLE_ARG_OR_NIL(args, TiViewProxy);
[self tableView];
[_headerWrapper removeAllChildren:nil];
if (args != nil) {
[_headerWrapper add:(TiViewProxy *)args];
}
}
- (void)setFooterView_:(id)args
{
ENSURE_SINGLE_ARG_OR_NIL(args, TiViewProxy);
if (IS_NULL_OR_NIL(args)) {
[_footerViewProxy setProxyObserver:nil];
[_footerViewProxy windowWillClose];
[self.tableView setTableFooterView:nil];
[_footerViewProxy windowDidClose];
RELEASE_TO_NIL(_footerViewProxy);
[((TiUIListViewProxy *)[self proxy]) contentsWillChange];
} else {
[self configureFooter];
[_footerViewProxy removeAllChildren:nil];
[_footerViewProxy add:(TiViewProxy *)args];
}
}
- (void)setRefreshControl_:(id)args
{
#ifdef USE_TI_UIREFRESHCONTROL
ENSURE_SINGLE_ARG_OR_NIL(args, TiUIRefreshControlProxy);
[[_refreshControlProxy control] removeFromSuperview];
RELEASE_TO_NIL(_refreshControlProxy);
[[self proxy] replaceValue:args forKey:@"refreshControl" notification:NO];
if (args != nil) {
_refreshControlProxy = [args retain];
[[self tableView] setRefreshControl:_refreshControlProxy.control];
}
#endif
}
- (void)setPullView_:(id)args
{
ENSURE_SINGLE_ARG_OR_NIL(args, TiViewProxy);
if (args == nil) {
[_pullViewProxy setProxyObserver:nil];
[_pullViewProxy windowWillClose];
[_pullViewWrapper removeFromSuperview];
[_pullViewProxy windowDidClose];
RELEASE_TO_NIL(_pullViewWrapper);
RELEASE_TO_NIL(_pullViewProxy);
} else {
if ([self tableView].bounds.size.width == 0) {
[self performSelector:@selector(setPullView_:) withObject:args afterDelay:0.1];
return;
}
if (_pullViewProxy != nil) {
[_pullViewProxy setProxyObserver:nil];
[_pullViewProxy windowWillClose];
[_pullViewProxy windowDidClose];
RELEASE_TO_NIL(_pullViewProxy);
}
if (_pullViewWrapper == nil) {
_pullViewWrapper = [[UIView alloc] init];
[_tableView addSubview:_pullViewWrapper];
}
CGSize refSize = _tableView.bounds.size;
[_pullViewWrapper setFrame:CGRectMake(0.0, 0.0 - refSize.height, refSize.width, refSize.height)];
_pullViewProxy = [args retain];
TiColor *pullBgColor = [TiUtils colorValue:[_pullViewProxy valueForUndefinedKey:@"pullBackgroundColor"]];
_pullViewWrapper.backgroundColor = ((pullBgColor == nil) ? [UIColor lightGrayColor] : [pullBgColor color]);
#ifndef TI_USE_AUTOLAYOUT
LayoutConstraint *viewLayout = [_pullViewProxy layoutProperties];
// If height is not dip, explicitly set it to SIZE
if (viewLayout->height.type != TiDimensionTypeDip) {
viewLayout->height = TiDimensionAutoSize;
}
// If bottom is not dip set it to 0
if (viewLayout->bottom.type != TiDimensionTypeDip) {
viewLayout->bottom = TiDimensionZero;
}
// Remove other vertical positioning constraints
viewLayout->top = TiDimensionUndefined;
viewLayout->centerY = TiDimensionUndefined;
#endif
[_pullViewProxy setProxyObserver:self];
[_pullViewProxy windowWillOpen];
[_pullViewWrapper addSubview:[_pullViewProxy view]];
_pullViewProxy.parentVisible = YES;
[_pullViewProxy refreshSize];
[_pullViewProxy willChangeSize];
[_pullViewProxy windowDidOpen];
}
}
- (void)setKeepSectionsInSearch_:(id)args
{
if (searchViewProxy == nil) {
keepSectionsInSearch = [TiUtils boolValue:args def:NO];
if (searchActive) {
[self buildResultsForSearchText];
}
[_tableView reloadData];
} else {
keepSectionsInSearch = NO;
}
}
- (void)setScrollIndicatorStyle_:(id)value
{
[self.tableView setIndicatorStyle:[TiUtils intValue:value def:UIScrollViewIndicatorStyleDefault]];
}
- (void)setWillScrollOnStatusTap_:(id)value
{
[self.tableView setScrollsToTop:[TiUtils boolValue:value def:YES]];
}
- (void)setShowVerticalScrollIndicator_:(id)value
{
[self.tableView setShowsVerticalScrollIndicator:[TiUtils boolValue:value]];
}
- (void)setAllowsSelection_:(id)value
{
[[self tableView] setAllowsSelection:[TiUtils boolValue:value]];
}
- (void)setKeyboardDismissMode_:(id)value
{
ENSURE_TYPE(value, NSNumber);
[[self tableView] setKeyboardDismissMode:[TiUtils intValue:value def:UIScrollViewKeyboardDismissModeNone]];
[[self proxy] replaceValue:value forKey:@"keyboardDismissMode" notification:NO];
}
- (void)setEditing_:(id)args
{
if ([TiUtils boolValue:args def:NO] != editing) {
editing = !editing;
[[self tableView] beginUpdates];
[_tableView setEditing:editing animated:YES];
[_tableView endUpdates];
}
}
- (void)setDisableBounce_:(id)value
{
[[self tableView] setBounces:![TiUtils boolValue:value def:NO]];
}
#if IS_SDK_IOS_15
- (void)setSectionHeaderTopPadding_:(id)value
{
if (![TiUtils isIOSVersionOrGreater:@"15.0"]) {
return;
}
self.tableView.sectionHeaderTopPadding = [TiUtils floatValue:value def:UITableViewAutomaticDimension];
}
#endif
- (void)setAllowsMultipleSelectionDuringEditing_:(id)value
{
ENSURE_TYPE(value, NSNumber);
[[self proxy] replaceValue:value forKey:@"allowsMultipleSelectionDuringEditing" notification:NO];
[[self tableView] beginUpdates];
[[self tableView] setAllowsMultipleSelectionDuringEditing:[TiUtils boolValue:value]];
[[self tableView] endUpdates];
}
#pragma mark - Search Support
- (void)setCaseInsensitiveSearch_:(id)args
{
caseInsensitiveSearch = [TiUtils boolValue:args def:YES];
if (searchActive) {
[self buildResultsForSearchText];
[_tableView reloadData];
}
}
- (void)setSearchText_:(id)args
{
id searchView = [self.proxy valueForKey:@"searchView"];
if (!IS_NULL_OR_NIL(searchView)) {
DebugLog(@"Can not use searchText with searchView. Ignoring call.");
return;
}
self.searchString = [TiUtils stringValue:args];
[self buildResultsForSearchText];
[_tableView reloadData];
}
- (void)setSearchView_:(id)args
{
ENSURE_TYPE_OR_NIL(args, TiUISearchBarProxy);
[self tableView];
[searchViewProxy setDelegate:nil];
RELEASE_TO_NIL(searchViewProxy);
RELEASE_TO_NIL(searchController);
[_searchWrapper removeAllChildren:nil];
if (args != nil) {
searchViewProxy = [args retain];
[searchViewProxy setDelegate:self];
[_searchWrapper add:searchViewProxy];
NSString *curPlaceHolder = [[searchViewProxy searchBar] placeholder];
if (curPlaceHolder == nil) {
[[searchViewProxy searchBar] setPlaceholder:NSLocalizedString(@"Search", @"Search")];
}
self.searchString = [[searchViewProxy searchBar] text];
[self initSearchController:self];
if (self.searchString) {
[self buildResultsForSearchText];
[_tableView reloadData];
}
keepSectionsInSearch = NO;
} else {
keepSectionsInSearch = [TiUtils boolValue:[self.proxy valueForKey:@"keepSectionsInSearch"] def:NO];
}
}
- (void)cleanup:(id)unused
{
if (_headerViewProxy != nil) {
[_headerViewProxy windowWillClose];
}
if (_footerViewProxy != nil) {
[_footerViewProxy windowWillClose];
}
if (searchController.isActive) {
searchController.active = NO;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismissSearchController) object:nil];
}
#pragma mark - SectionIndexTitle Support
- (void)setSectionIndexTitles_:(id)args
{
ENSURE_TYPE_OR_NIL(args, NSArray);
RELEASE_TO_NIL(sectionTitles);
RELEASE_TO_NIL(sectionIndices);
RELEASE_TO_NIL(filteredTitles);
RELEASE_TO_NIL(filteredIndices);
NSArray *theIndex = args;
if ([theIndex count] > 0) {
sectionTitles = [[NSMutableArray alloc] initWithCapacity:[theIndex count]];
sectionIndices = [[NSMutableArray alloc] initWithCapacity:[theIndex count]];
for (NSDictionary *entry in theIndex) {
ENSURE_DICT(entry);