react-native-keyboard-controller 1.9.1 → 1.9.3

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.
@@ -2,6 +2,7 @@ package com.reactnativekeyboardcontroller.extensions
2
2
 
3
3
  import android.graphics.Rect
4
4
  import android.os.Build
5
+ import android.util.Log
5
6
  import android.view.View
6
7
  import androidx.annotation.RequiresApi
7
8
 
@@ -37,15 +38,12 @@ private val tmpIntArr = IntArray(2)
37
38
  */
38
39
  @RequiresApi(Build.VERSION_CODES.KITKAT)
39
40
  fun View.copyBoundsInWindow(rect: Rect) {
40
- if (isLaidOut && isAttachedToWindow) {
41
+ if (isAttachedToWindow) {
41
42
  rect.set(0, 0, width, height)
42
43
  getLocationInWindow(tmpIntArr)
43
44
  rect.offset(tmpIntArr[0], tmpIntArr[1])
44
45
  } else {
45
- throw IllegalArgumentException(
46
- "Can not copy bounds as view is not laid out" +
47
- " or attached to window",
48
- )
46
+ Log.w("View.copyBoundsInWindow", "Can not copy bounds as view is not attached to window")
49
47
  }
50
48
  }
51
49
 
@@ -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.1",
3
+ "version": "1.9.3",
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",