react-native-keyboard-controller 1.9.0 → 1.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -110,6 +110,7 @@ class KeyboardAnimationCallback(
110
110
  * - we dispatch `keyboardDidShow` (onEnd).
111
111
  */
112
112
  override fun onApplyWindowInsets(v: View, insets: WindowInsetsCompat): WindowInsetsCompat {
113
+ val keyboardHeight = getCurrentKeyboardHeight()
113
114
  // when keyboard appears values will be (false && true)
114
115
  // when keyboard disappears values will be (true && false)
115
116
  val isKeyboardShown = isKeyboardVisible && isKeyboardVisible()
@@ -121,10 +122,17 @@ class KeyboardAnimationCallback(
121
122
  // `InteractiveKeyboardProvider.isInteractive` detect case when keyboard moves
122
123
  // because of the gesture
123
124
  val isMoving = isTransitioning || InteractiveKeyboardProvider.isInteractive
124
- if (isKeyboardShown && !isMoving && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
125
- val keyboardHeight = getCurrentKeyboardHeight()
126
- val duration = DEFAULT_ANIMATION_TIME.toInt()
125
+ val isKeyboardFullyVisible = isKeyboardShown && !isMoving
126
+ // when keyboard is opened and we trigger a transition from screen A to screen B,
127
+ // then this method is getting called and we start dispatching events, and later original
128
+ // `onStart`/`onProgress`/`onEnd` emitting their events (since keyboard is closing) and we
129
+ // are getting race conditions.
130
+ //
131
+ // but in general this check is a must because we are detecting keyboard size changes
132
+ // in this method
133
+ val isKeyboardSizeEqual = this.persistentKeyboardHeight == keyboardHeight
127
134
 
135
+ if (isKeyboardFullyVisible && !isKeyboardSizeEqual && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
128
136
  layoutObserver?.syncUpLayout()
129
137
  this.emitEvent("KeyboardController::keyboardWillShow", getEventParams(keyboardHeight))
130
138
  context.dispatchEvent(
@@ -135,7 +143,7 @@ class KeyboardAnimationCallback(
135
143
  "topKeyboardMoveStart",
136
144
  keyboardHeight,
137
145
  1.0,
138
- duration,
146
+ DEFAULT_ANIMATION_TIME,
139
147
  viewTagFocused,
140
148
  ),
141
149
  )
@@ -152,7 +160,7 @@ class KeyboardAnimationCallback(
152
160
  "topKeyboardMove",
153
161
  toValue.toDouble(),
154
162
  toValue.toDouble() / keyboardHeight,
155
- duration,
163
+ DEFAULT_ANIMATION_TIME,
156
164
  viewTagFocused,
157
165
  ),
158
166
  )
@@ -167,12 +175,12 @@ class KeyboardAnimationCallback(
167
175
  "topKeyboardMoveEnd",
168
176
  keyboardHeight,
169
177
  1.0,
170
- duration,
178
+ DEFAULT_ANIMATION_TIME,
171
179
  viewTagFocused,
172
180
  ),
173
181
  )
174
182
  }
175
- animation.setDuration(DEFAULT_ANIMATION_TIME).startDelay = 0
183
+ animation.setDuration(DEFAULT_ANIMATION_TIME.toLong()).startDelay = 0
176
184
  animation.start()
177
185
 
178
186
  this.persistentKeyboardHeight = keyboardHeight
@@ -344,6 +352,6 @@ class KeyboardAnimationCallback(
344
352
  }
345
353
 
346
354
  companion object {
347
- private const val DEFAULT_ANIMATION_TIME = 250L
355
+ private const val DEFAULT_ANIMATION_TIME = 250
348
356
  }
349
357
  }
@@ -25,6 +25,8 @@ let noFocusedInputEvent: [String: Any] = [
25
25
  public class FocusedInputLayoutObserver: NSObject {
26
26
  // class members
27
27
  var onEvent: (NSDictionary) -> Void
28
+ // state variables
29
+ private var isMounted = false
28
30
  // input tracking
29
31
  private var currentInput: UIView?
30
32
  private var hasKVObserver = false
@@ -37,6 +39,12 @@ public class FocusedInputLayoutObserver: NSObject {
37
39
  }
38
40
 
39
41
  @objc public func mount() {
42
+ if isMounted {
43
+ return
44
+ }
45
+
46
+ isMounted = true
47
+
40
48
  NotificationCenter.default.addObserver(
41
49
  self,
42
50
  selector: #selector(keyboardWillShow),
@@ -52,6 +60,7 @@ public class FocusedInputLayoutObserver: NSObject {
52
60
  }
53
61
 
54
62
  @objc public func unmount() {
63
+ isMounted = false
55
64
  // swiftlint:disable:next notification_center_detachment
56
65
  NotificationCenter.default.removeObserver(self)
57
66
  }
@@ -31,6 +31,7 @@ public class KeyboardMovementObserver: NSObject {
31
31
  private var prevKeyboardPosition = 0.0
32
32
  private var displayLink: CADisplayLink?
33
33
  private var hasKVObserver = false
34
+ private var isMounted = false
34
35
  // state variables
35
36
  private var keyboardHeight: CGFloat = 0.0
36
37
  private var duration = 0
@@ -45,6 +46,12 @@ public class KeyboardMovementObserver: NSObject {
45
46
  }
46
47
 
47
48
  @objc public func mount() {
49
+ if isMounted {
50
+ return
51
+ }
52
+
53
+ isMounted = true
54
+
48
55
  NotificationCenter.default.addObserver(
49
56
  self,
50
57
  selector: #selector(keyboardWillDisappear),
@@ -138,6 +145,7 @@ public class KeyboardMovementObserver: NSObject {
138
145
  }
139
146
 
140
147
  @objc public func unmount() {
148
+ isMounted = false
141
149
  // swiftlint:disable:next notification_center_detachment
142
150
  NotificationCenter.default.removeObserver(self)
143
151
  }
@@ -156,17 +156,36 @@ using namespace facebook::react;
156
156
 
157
157
  if (newViewProps.enabled != oldViewProps.enabled) {
158
158
  if (newViewProps.enabled) {
159
- [inputObserver mount];
160
- [keyboardObserver mount];
159
+ [self mount];
161
160
  } else {
162
- [inputObserver unmount];
163
- [keyboardObserver unmount];
161
+ [self unmount];
164
162
  }
165
163
  }
166
164
 
167
165
  [super updateProps:props oldProps:oldProps];
168
166
  }
169
167
 
168
+ - (void)willMoveToSuperview:(UIView *)newSuperview
169
+ {
170
+ if (newSuperview == nil) {
171
+ [self unmount];
172
+ } else {
173
+ [self mount];
174
+ }
175
+ }
176
+
177
+ - (void)mount
178
+ {
179
+ [inputObserver mount];
180
+ [keyboardObserver mount];
181
+ }
182
+
183
+ - (void)unmount
184
+ {
185
+ [inputObserver unmount];
186
+ [keyboardObserver unmount];
187
+ }
188
+
170
189
  Class<RCTComponentViewProtocol> KeyboardControllerViewCls(void)
171
190
  {
172
191
  return KeyboardControllerView.class;
@@ -24,11 +24,9 @@ class KeyboardControllerView: UIView {
24
24
  @objc var enabled: ObjCBool = true {
25
25
  didSet {
26
26
  if enabled.boolValue {
27
- inputObserver?.mount()
28
- keyboardObserver?.mount()
27
+ mount()
29
28
  } else {
30
- inputObserver?.unmount()
31
- keyboardObserver?.unmount()
29
+ unmount()
32
30
  }
33
31
  }
34
32
  }
@@ -37,6 +35,8 @@ class KeyboardControllerView: UIView {
37
35
  self.bridge = bridge
38
36
  eventDispatcher = bridge.eventDispatcher()
39
37
  super.init(frame: frame)
38
+ inputObserver = FocusedInputLayoutObserver(handler: onInput)
39
+ keyboardObserver = KeyboardMovementObserver(handler: onEvent, onNotify: onNotify)
40
40
  }
41
41
 
42
42
  @available(*, unavailable)
@@ -44,21 +44,17 @@ class KeyboardControllerView: UIView {
44
44
  fatalError("init(coder:) has not been implemented")
45
45
  }
46
46
 
47
- override func willMove(toWindow newWindow: UIWindow?) {
48
- if newWindow == nil {
49
- // Will be removed from a window
50
- inputObserver?.unmount()
51
- keyboardObserver?.unmount()
52
- }
53
- }
47
+ // for mounting/unmounting observers for lifecycle events we're using willMove(toSuperview) method
48
+ // not willMove(toWindow)
49
+ // see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/271
50
+ override func willMove(toSuperview newSuperview: UIView?) {
51
+ super.willMove(toSuperview: newSuperview)
54
52
 
55
- override func didMoveToWindow() {
56
- if window != nil {
57
- // Added to a window
58
- inputObserver = FocusedInputLayoutObserver(handler: onInput)
59
- inputObserver?.mount()
60
- keyboardObserver = KeyboardMovementObserver(handler: onEvent, onNotify: onNotify)
61
- keyboardObserver?.mount()
53
+ if newSuperview == nil {
54
+ // The view is about to be removed from its superview (destroyed)
55
+ unmount()
56
+ } else {
57
+ mount()
62
58
  }
63
59
  }
64
60
 
@@ -90,4 +86,14 @@ class KeyboardControllerView: UIView {
90
86
  func onNotify(event: String, data: Any) {
91
87
  KeyboardController.shared()?.sendEvent(event, body: data)
92
88
  }
89
+
90
+ private func mount() {
91
+ inputObserver?.mount()
92
+ keyboardObserver?.mount()
93
+ }
94
+
95
+ private func unmount() {
96
+ inputObserver?.unmount()
97
+ keyboardObserver?.unmount()
98
+ }
93
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-keyboard-controller",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "description": "Keyboard manager which works in identical way on both iOS and Android",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",