react-native-keyboard-controller 1.16.0 → 1.16.1

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.
@@ -49,7 +49,11 @@ public extension Optional where Wrapped == UIResponder {
49
49
  guard let superview = (self as? UIView)?.superview else { return nil }
50
50
 
51
51
  #if KEYBOARD_CONTROLLER_NEW_ARCH_ENABLED
52
- return (superview as NSObject).value(forKey: "nativeId") as? String
52
+ if superview.responds(to: Selector(("nativeId"))) == true {
53
+ return (superview as NSObject).value(forKey: "nativeId") as? String
54
+ }
55
+
56
+ return nil
53
57
  #else
54
58
  return superview.nativeID
55
59
  #endif
@@ -33,7 +33,7 @@ public class KeyboardMovementObserver: NSObject {
33
33
  private var _windowsCount: Int = 0
34
34
  private var prevKeyboardPosition = 0.0
35
35
  private var displayLink: CADisplayLink?
36
- private var hasKVObserver = false
36
+ private var interactiveKeyboardObserver: NSKeyValueObservation?
37
37
  private var isMounted = false
38
38
  // state variables
39
39
  private var _keyboardHeight: CGFloat = 0.0
@@ -93,73 +93,57 @@ public class KeyboardMovementObserver: NSObject {
93
93
  }
94
94
 
95
95
  private func setupKVObserver() {
96
- if hasKVObserver {
97
- return
98
- }
96
+ guard interactiveKeyboardObserver == nil, let view = keyboardView else { return }
97
+
98
+ interactiveKeyboardObserver = view.observe(\.center, options: .new) { [weak self] _, change in
99
+ guard let self = self, let changeValue = change.newValue else { return }
99
100
 
100
- if keyboardView != nil {
101
- hasKVObserver = true
102
- keyboardView?.addObserver(self, forKeyPath: "center", options: .new, context: nil)
101
+ self.keyboardDidMoveInteractively(changeValue: changeValue)
103
102
  }
104
103
  }
105
104
 
106
105
  private func removeKVObserver() {
107
- if !hasKVObserver {
106
+ interactiveKeyboardObserver?.invalidate()
107
+ interactiveKeyboardObserver = nil
108
+ }
109
+
110
+ private func keyboardDidMoveInteractively(changeValue: CGPoint) {
111
+ // if we are currently animating keyboard -> we need to ignore values from KVO
112
+ if displayLink != nil {
113
+ return
114
+ }
115
+ // if keyboard height is not equal to its bounds - we can ignore
116
+ // values, since they'll be invalid and will cause UI jumps
117
+ if floor(keyboardView?.bounds.size.height ?? 0) != floor(_keyboardHeight) {
108
118
  return
109
119
  }
110
120
 
111
- hasKVObserver = false
112
- _keyboardView?.removeObserver(self, forKeyPath: "center", context: nil)
113
- }
121
+ let keyboardFrameY = changeValue.y
122
+ let keyboardWindowH = keyboardView?.window?.bounds.size.height ?? 0
123
+ let keyboardPosition = keyboardWindowH - keyboardFrameY
114
124
 
115
- // swiftlint:disable:next block_based_kvo
116
- @objc override public func observeValue(
117
- forKeyPath keyPath: String?,
118
- of object: Any?,
119
- change: [NSKeyValueChangeKey: Any]?,
120
- context _: UnsafeMutableRawPointer?
121
- ) {
122
- if keyPath == "center", object as? NSObject == _keyboardView {
123
- // if we are currently animating keyboard -> we need to ignore values from KVO
124
- if displayLink != nil {
125
- return
126
- }
127
- // if keyboard height is not equal to its bounds - we can ignore
128
- // values, since they'll be invalid and will cause UI jumps
129
- if floor(keyboardView?.bounds.size.height ?? 0) != floor(_keyboardHeight) {
130
- return
131
- }
125
+ let position = CGFloat.interpolate(
126
+ inputRange: [_keyboardHeight / 2, -_keyboardHeight / 2],
127
+ outputRange: [_keyboardHeight, 0],
128
+ currentValue: keyboardPosition
129
+ ) - KeyboardAreaExtender.shared.offset
132
130
 
133
- guard let changeValue = change?[.newKey] as? NSValue else {
134
- return
135
- }
136
- let keyboardFrameY = changeValue.cgPointValue.y
137
- let keyboardWindowH = keyboardView?.window?.bounds.size.height ?? 0
138
- let keyboardPosition = keyboardWindowH - keyboardFrameY
139
-
140
- let position = CGFloat.interpolate(
141
- inputRange: [_keyboardHeight / 2, -_keyboardHeight / 2],
142
- outputRange: [_keyboardHeight, 0],
143
- currentValue: keyboardPosition
144
- ) - KeyboardAreaExtender.shared.offset
145
-
146
- if position == 0 {
147
- // it will be triggered before `keyboardWillDisappear` and
148
- // we don't need to trigger `onInteractive` handler for that
149
- // since it will be handled in `keyboardWillDisappear` function
150
- return
151
- }
131
+ if position == 0 {
132
+ // it will be triggered before `keyboardWillDisappear` and
133
+ // we don't need to trigger `onInteractive` handler for that
134
+ // since it will be handled in `keyboardWillDisappear` function
135
+ return
136
+ }
152
137
 
153
- prevKeyboardPosition = position
138
+ prevKeyboardPosition = position
154
139
 
155
- onEvent(
156
- "onKeyboardMoveInteractive",
157
- position as NSNumber,
158
- position / CGFloat(keyboardHeight) as NSNumber,
159
- -1,
160
- tag
161
- )
162
- }
140
+ onEvent(
141
+ "onKeyboardMoveInteractive",
142
+ position as NSNumber,
143
+ position / CGFloat(keyboardHeight) as NSNumber,
144
+ -1,
145
+ tag
146
+ )
163
147
  }
164
148
 
165
149
  @objc public func unmount() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-keyboard-controller",
3
- "version": "1.16.0",
3
+ "version": "1.16.1",
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",