-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathRCTCustomInputController.m
397 lines (320 loc) · 13 KB
/
RCTCustomInputController.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
//
// RCTCustomInputController.m
//
// Created by Leo Natan (Wix) on 13/12/2016.
// Copyright © 2016 Leo Natan. All rights reserved.
//
#import "RCTCustomInputController.h"
#import "RCTCustomKeyboardViewController.h"
#import <React/RCTUIManager.h>
#import <objc/runtime.h>
#import "LNAnimator.h"
#define kHlperViewTag 0x1f1f1f
NSString *const RCTCustomInputControllerKeyboardResigendEvent = @"kbdResigned";
@protocol _WXInputHelperViewDelegate <NSObject>
-(void)_WXInputHelperViewResignFirstResponder:(UIView*)wxInputHelperView;
@end
@interface _WXInputHelperView : UIView
@property (nullable, nonatomic, readwrite, strong) UIInputViewController *inputViewController;
@property (nonatomic, weak) id<_WXInputHelperViewDelegate> delegate;
@property (nullable, readwrite, strong) UIView *inputAccessoryView;
@property (nonatomic) BOOL keepInSuperviewOnResign;
@end
@implementation _WXInputHelperView
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (BOOL)resignFirstResponder
{
BOOL rv = [super resignFirstResponder];
if (!_keepInSuperviewOnResign)
{
[self removeFromSuperview];
if(self.delegate && [self.delegate respondsToSelector:@selector(_WXInputHelperViewResignFirstResponder:)])
{
[self.delegate _WXInputHelperViewResignFirstResponder:self];
}
}
return rv;
}
@end
@interface RCTCustomInputController () <_WXInputHelperViewDelegate> {
UIWindow *_fullScreenWindow;
BOOL _performingExpandTransition;
}
@property(nonatomic) BOOL customInputComponentPresented;
@end
@implementation RCTCustomInputController
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
- (NSArray<NSString *> *)supportedEvents
{
return @[RCTCustomInputControllerKeyboardResigendEvent];
}
RCT_EXPORT_MODULE(CustomInputController)
- (instancetype)init
{
self = [super init];
if (self)
{
self.customInputComponentPresented = NO;
}
return self;
}
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
-(UIView*)getFirstResponder:(UIView*)view
{
if (view == nil || [view isFirstResponder])
{
return view;
}
for (UIView *subview in view.subviews)
{
UIView *firstResponder = [self getFirstResponder:subview];
if(firstResponder != nil)
{
return firstResponder;
}
}
return nil;
}
RCT_EXPORT_METHOD(presentCustomInputComponent:(nonnull NSNumber*)inputFieldTag params:(nonnull NSDictionary*)params)
{
RCTBridge* bridge = [self.bridge valueForKey:@"parentBridge"];
if(bridge == nil)
{
return;
}
UIView* inputField = [self.bridge.uiManager viewForReactTag:inputFieldTag];
NSDictionary *initialProps = params[@"initialProps"];
RCTRootView* rv = [[RCTRootView alloc] initWithBridge:bridge moduleName:params[@"component"] initialProperties:initialProps];
if(initialProps != nil && initialProps[@"backgroundColor"] != nil)
{
UIColor *backgroundColor = [RCTConvert UIColor:initialProps[@"backgroundColor"]];
if(backgroundColor != nil)
{
rv.backgroundColor = backgroundColor;
}
}
self.customInputComponentPresented = NO;
RCTCustomKeyboardViewController* customKeyboardController = [[RCTCustomKeyboardViewController alloc] init];
customKeyboardController.rootView = rv;
_WXInputHelperView* helperView = [[_WXInputHelperView alloc] initWithFrame:CGRectZero];
helperView.tag = kHlperViewTag;
helperView.delegate = self;
if ([inputField isKindOfClass:NSClassFromString(@"RCTTextView")])
{
UITextView *textView = nil;
Ivar backedTextInputIvar = class_getInstanceVariable([inputField class], "_backedTextInput");
if (backedTextInputIvar != NULL)
{
textView = [inputField valueForKey:@"_backedTextInput"];
}
else if([inputField isKindOfClass:[UITextView class]])
{
textView = (UITextView*)inputField;
}
if (textView != nil)
{
helperView.inputAccessoryView = textView.inputAccessoryView;
}
}
else if ([inputField isKindOfClass:NSClassFromString(@"RCTUITextView")] && [inputField isKindOfClass:[UITextView class]])
{
UITextView *textView = (UITextView*)inputField;
helperView.inputAccessoryView = textView.inputAccessoryView;
}
else if([inputField isKindOfClass:NSClassFromString(@"RCTMultilineTextInputView")])
{
Ivar backedTextInputIvar = class_getInstanceVariable([inputField class], "_backedTextInputView");
if (backedTextInputIvar != NULL)
{
id textViewObj = [inputField valueForKey:@"_backedTextInputView"];
if ([textViewObj isKindOfClass:[UITextView class]])
{
UITextView *textView = (UITextView*)textViewObj;
helperView.inputAccessoryView = textView.inputAccessoryView;
}
}
}
else
{
UIView *firstResponder = [self getFirstResponder:inputField];
helperView.inputAccessoryView = firstResponder.inputAccessoryView;
}
[helperView reloadInputViews];
helperView.backgroundColor = [UIColor clearColor];
[inputField.superview addSubview:helperView];
[inputField.superview sendSubviewToBack:helperView];
helperView.inputViewController = customKeyboardController;
[helperView reloadInputViews];
[helperView becomeFirstResponder];
self.customInputComponentPresented = YES;
}
RCT_EXPORT_METHOD(resetInput:(nonnull NSNumber*)inputFieldTag)
{
self.customInputComponentPresented = NO;
UIView* inputField = [self.bridge.uiManager viewForReactTag:inputFieldTag];
if(inputField != nil)
{
_WXInputHelperView* helperView = [inputField.superview viewWithTag:kHlperViewTag];
if(helperView != nil && [helperView isFirstResponder])
{//restore the first responder only if it was already the first responder to prevent the keyboard from opening again if not necessary
[inputField reactFocus];
}
}
}
RCT_EXPORT_METHOD(dismissKeyboard)
{
UIView *firstResponder = [self getFirstResponder:[UIApplication sharedApplication].delegate.window];
if(firstResponder != nil)
{
[firstResponder resignFirstResponder];
}
}
-(void)changeKeyboardHeightForInput:(nonnull NSNumber*)inputFieldTag newHeight:(CGFloat)newHeight
{
UIView* inputField = [self.bridge.uiManager viewForReactTag:inputFieldTag];
if(inputField != nil)
{
_WXInputHelperView* helperView = [inputField.superview viewWithTag:kHlperViewTag];
if(helperView != nil)
{
[((RCTCustomKeyboardViewController*)helperView.inputViewController) setAllowsSelfSizing:YES];
((RCTCustomKeyboardViewController*)helperView.inputViewController).heightConstraint.constant = newHeight;
UIInputView *inputView = helperView.inputViewController.inputView;
[inputView setNeedsUpdateConstraints];
[UIView animateWithDuration:0.55
delay:0
usingSpringWithDamping:500.0
initialSpringVelocity:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{ [inputView layoutIfNeeded]; }
completion:nil];
}
}
}
-(UIColor*)reactViewAvgColor:(RCTRootView*)rootView
{
if (rootView.frame.size.width == 0 || rootView.frame.size.height == 0)
{
return [UIColor clearColor];
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), YES, 0);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -(rootView.frame.size.height - 1));
[rootView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
const UInt8* data = CFDataGetBytePtr(pixelData);
CFRelease(pixelData);
//after scale defaults to bgr
CGFloat red = data[2] / 255.0f,
green = data[1] / 255.0f,
blue = data[0] / 255.0f,
alpha = data[3] / 255.0f;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return color;
}
RCT_EXPORT_METHOD(expandFullScreenForInput:(nonnull NSNumber*)inputFieldTag)
{
if (_fullScreenWindow != nil || _performingExpandTransition)
{
return;
}
UIView* inputField = [self.bridge.uiManager viewForReactTag:inputFieldTag];
if(inputField != nil)
{
_WXInputHelperView* helperView = [inputField.superview viewWithTag:kHlperViewTag];
if(helperView != nil)
{
_performingExpandTransition = YES;
helperView.keepInSuperviewOnResign = YES;
RCTCustomKeyboardViewController *customKeyboardViewController = (RCTCustomKeyboardViewController*)helperView.inputViewController;
RCTRootView *rv = customKeyboardViewController.rootView;
UIInputView *inputView = helperView.inputViewController.inputView;
_fullScreenWindow = [[UIWindow alloc] initWithFrame:[inputView.window convertRect:inputView.bounds fromView:inputView]];
UIColor *originalBackgroundColor = rv.backgroundColor;
rv.backgroundColor = [self reactViewAvgColor:rv];
customKeyboardViewController.rootView = nil;
UIViewController *vc = [UIViewController new];
vc.view = rv;
inputView.window.hidden = YES;
[UIView performWithoutAnimation:^{
_fullScreenWindow.hidden = NO;
_fullScreenWindow.rootViewController = vc;
[_fullScreenWindow layoutIfNeeded];
}];
[[LNAnimator animatorWithDuration:0.5
animations:@[[LNViewAnimation animationWithView:_fullScreenWindow keyPath:@"frame" toValue:[NSValue valueWithCGRect:[UIScreen mainScreen].bounds]]]
completionHandler:^(BOOL completed)
{
[UIView performWithoutAnimation:^{
inputView.window.hidden = NO;
[helperView resignFirstResponder];
[_fullScreenWindow makeKeyAndVisible];
rv.backgroundColor = originalBackgroundColor;
}];
_performingExpandTransition = NO;
}] start];
}
}
}
RCT_EXPORT_METHOD(resetSizeForInput:(nonnull NSNumber*)inputFieldTag)
{
if (_fullScreenWindow == nil || _performingExpandTransition)
{
return;
}
UIView* inputField = [self.bridge.uiManager viewForReactTag:inputFieldTag];
if(inputField != nil)
{
_WXInputHelperView* helperView = [inputField.superview viewWithTag:kHlperViewTag];
if(helperView != nil)
{
_performingExpandTransition = YES;
__block CGRect keyboardTargetFrame;
UIInputView *inputView = helperView.inputViewController.inputView;
[UIView performWithoutAnimation:^{
[helperView.window makeKeyWindow];
[helperView becomeFirstResponder];
[helperView layoutIfNeeded];
keyboardTargetFrame = [inputView.window convertRect:inputView.bounds fromView:inputView];
}];
_fullScreenWindow.windowLevel = inputView.window.windowLevel + 1;
[_fullScreenWindow layoutIfNeeded];
[_fullScreenWindow endEditing:YES];
[[LNAnimator animatorWithDuration:0.5
animations:@[[LNViewAnimation animationWithView:_fullScreenWindow keyPath:@"frame" toValue:[NSValue valueWithCGRect:keyboardTargetFrame]]]
completionHandler:^(BOOL completed)
{
RCTCustomKeyboardViewController *customKeyboardViewController = (RCTCustomKeyboardViewController*)helperView.inputViewController;
RCTRootView *rv = (RCTRootView*)_fullScreenWindow.rootViewController.view;
[UIView performWithoutAnimation:^{
_fullScreenWindow.rootViewController.view = [UIView new];
customKeyboardViewController.rootView = rv;
_fullScreenWindow.hidden = YES;
_fullScreenWindow = nil;
}];
helperView.keepInSuperviewOnResign = NO;
_performingExpandTransition = NO;
}] start];
}
}
}
#pragma mark - _WXInputHelperViewDelegate methods
-(void)_WXInputHelperViewResignFirstResponder:(UIView*)wxInputHelperView
{
if(self.customInputComponentPresented)
{
[self sendEventWithName:RCTCustomInputControllerKeyboardResigendEvent body:nil];
}
self.customInputComponentPresented = NO;
}
@end