forked from johanneslumpe/react-native-keyboardevents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNKeyboardEventsManager.m
131 lines (111 loc) · 5.37 KB
/
RNKeyboardEventsManager.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
//
// RNKeyboardEventsManager.m
// RNKeyboardEventsManager
//
// Created by Johannes Lumpe on 12/04/15.
// Copyright (c) 2015 Johannes Lumpe. All rights reserved.
//
#import "RNKeyboardEventsManager.h"
#import "RCTConvert.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
static NSString* RNKeyboardEventsDidShow = @"keyboardDidShow";
static NSString* RNKeyboardEventsDidHide = @"keyboardDidHide";
static NSString* RNKeyboardEventsWillShow = @"keyboardWillShow";
static NSString* RNKeyboardEventsWillHide = @"keyboardWillHide";
static NSString* RNKeyboardEventsWillChangeFrame = @"keyboardWillChangeFrame";
static NSString* RNKeyboardEventsDidChangeFrame = @"keyboardDidChangeFrame";
@implementation RNKeyboardEventsManager
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
- (instancetype)init {
if (self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidChangeFrame:)
name:UIKeyboardDidChangeFrameNotification
object:nil];
}
return self;
}
- (NSDictionary*)getDictionaryForRect:(CGRect)rect {
CGSize size = rect.size;
CGPoint origin = rect.origin;
int h = MIN(size.height, size.width);
int w = MAX(size.height, size.width);
return @{
@"width": [NSNumber numberWithInt:w],
@"height": [NSNumber numberWithInt:h],
@"x": [NSNumber numberWithInt:origin.x],
@"y": [NSNumber numberWithInt:origin.y]
};
}
- (NSDictionary*) makeBodyFromNotification:(NSNotification*)notification {
CGRect keyboardBeginRect = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect keyboardEndRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSNumber *durationValue = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSDictionary *body = @{
@"begin": [self getDictionaryForRect:keyboardBeginRect],
@"end": [self getDictionaryForRect:keyboardEndRect],
@"duration": durationValue
};
return body;
}
- (void)keyboardWillHide:(NSNotification*)notification {
[_bridge.eventDispatcher sendDeviceEventWithName:RNKeyboardEventsWillHide
body:[self makeBodyFromNotification:notification]];
}
- (void)keyboardDidHide:(NSNotification*)notification {
[_bridge.eventDispatcher sendDeviceEventWithName:RNKeyboardEventsDidHide
body:[self makeBodyFromNotification:notification]];
}
- (void)keyboardWillShow:(NSNotification*)notification {
[_bridge.eventDispatcher sendDeviceEventWithName:RNKeyboardEventsWillShow
body:[self makeBodyFromNotification:notification]];
}
- (void)keyboardDidShow:(NSNotification*)notification {
[_bridge.eventDispatcher sendDeviceEventWithName:RNKeyboardEventsDidShow
body:[self makeBodyFromNotification:notification]];
}
- (void)keyboardWillChangeFrame:(NSNotification*)notification {
[_bridge.eventDispatcher sendDeviceEventWithName:RNKeyboardEventsWillChangeFrame
body:[self makeBodyFromNotification:notification]];
}
- (void)keyboardDidChangeFrame:(NSNotification*)notification {
[_bridge.eventDispatcher sendDeviceEventWithName:RNKeyboardEventsDidChangeFrame
body:[self makeBodyFromNotification:notification]];
}
- (NSDictionary *)constantsToExport
{
return @{
@"KeyboardDidShow": RNKeyboardEventsDidShow,
@"KeyboardDidHide": RNKeyboardEventsDidHide,
@"KeyboardWillShow": RNKeyboardEventsWillShow,
@"KeyboardWillHide": RNKeyboardEventsWillHide,
@"KeyboardWillChangeFrame": RNKeyboardEventsWillChangeFrame,
@"KeyboardDidChangeFrame": RNKeyboardEventsDidChangeFrame,
};
}
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end