-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathNVSearchBarView.m
145 lines (123 loc) · 3.97 KB
/
NVSearchBarView.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
#import "NVSearchBarView.h"
#import "NVSearchResultsController.h"
#import <UIKit/UIKit.h>
#import <React/RCTBridge.h>
#import <React/RCTI18nUtil.h>
#import <React/RCTUIManager.h>
#import <React/UIView+React.h>
@implementation NVSearchBarView
{
__weak RCTBridge *_bridge;
UIView *_reactSubview;
NSInteger _nativeEventCount;
}
- (id)initWithBridge:(RCTBridge *)bridge
{
if (self = [super initWithFrame:CGRectZero]) {
_bridge = bridge;
self.tag = SEARCH_BAR;
NVSearchResultsController *viewController = [[NVSearchResultsController alloc] init];
self.searchController = [[NVSearchController alloc] initWithSearchResultsController:viewController];
self.searchController.searchBar.semanticContentAttribute = ![[RCTI18nUtil sharedInstance] isRTL] ? UISemanticContentAttributeForceLeftToRight : UISemanticContentAttributeForceRightToLeft;
self.searchController.searchResultsUpdater = self;
__weak typeof(self) weakSelf = self;
viewController.boundsDidChangeBlock = ^(CGRect newBounds) {
[weakSelf notifyForBoundsChange:newBounds];
};
}
return self;
}
- (void)setObscureBackground:(BOOL)obscureBackground
{
if (@available(iOS 9.1, *)) {
[self.searchController setObscuresBackgroundDuringPresentation:obscureBackground];
}
}
- (void)setHideNavigationBar:(BOOL)hideNavigationBar
{
[self.searchController setHidesNavigationBarDuringPresentation:hideNavigationBar];
}
- (void)setAutoCapitalize:(UITextAutocapitalizationType)autoCapitalize
{
[self.searchController.searchBar setAutocapitalizationType:autoCapitalize];
}
- (void)setPlaceholder:(NSString *)placeholder
{
[self.searchController.searchBar setPlaceholder:placeholder];
}
- (void)setText:(NSString *)text
{
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
if (eventLag == 0 && [self.searchController.searchBar text] != text) {
[self.searchController.searchBar setText:text];
}
}
- (void)setBarTintColor:(UIColor *)barTintColor
{
if (@available(iOS 13.0, *)) {
[self.searchController.searchBar.searchTextField setBackgroundColor:barTintColor];
}
}
- (void)didSetProps:(NSArray<NSString *> *)changedProps
{
if (@available(iOS 11.0, *)) {
[self.reactViewController.navigationItem setHidesSearchBarWhenScrolling:self.hideWhenScrolling];
}
}
- (void)notifyForBoundsChange:(CGRect)newBounds
{
if (_reactSubview) {
[_bridge.uiManager setSize:newBounds.size forView:_reactSubview];
}
}
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
{
[super insertReactSubview:subview atIndex:atIndex];
self.searchController.searchResultsController.view = subview;
_reactSubview = subview;
}
- (void)removeReactSubview:(UIView *)subview
{
[super removeReactSubview:subview];
_reactSubview = nil;
}
- (void)didUpdateReactSubviews
{
}
- (void)didMoveToWindow
{
[super didMoveToWindow];
if (@available(iOS 11.0, *)) {
[self.reactViewController.navigationItem setSearchController:_searchController];
}
}
- (void)willMoveToSuperview:(nullable UIView *)newSuperview
{
[super willMoveToSuperview:newSuperview];
if (!newSuperview) {
if (@available(iOS 11.0, *)) {
[self.reactViewController.navigationItem setSearchController:nil];
[self.searchController.searchResultsController dismissViewControllerAnimated:NO completion:nil];
}
}
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
_nativeEventCount++;
if (!!self.onChangeText) {
self.onChangeText(@{
@"text": searchController.searchBar.text,
@"eventCount": @(_nativeEventCount),
});
}
}
@end
@implementation NVSearchController
- (UIStatusBarStyle)preferredStatusBarStyle
{
return self.presentingViewController.preferredStatusBarStyle;
}
- (BOOL)prefersStatusBarHidden
{
return self.presentingViewController.prefersStatusBarHidden;
}
@end