Skip to content

Commit

Permalink
refactor: reduce code duplication in KeyboardMovementObserver (#556)
Browse files Browse the repository at this point in the history
## 📜 Description

Reduce code duplication and repeated code.

## 💡 Motivation and Context

Reduce code duplication and repeated code by:
- moving creation of `AnyHashable` into separate function;
- working with notification data in separate method.

## 📢 Changelog

<!-- High level overview of important changes -->
<!-- For example: fixed status bar manipulation; added new types
declarations; -->
<!-- If your changes don't affect one of platform/language below - then
remove this platform/language -->

### iOS

- created `metaDataFromNotification` and started to use this function;
- created `getEventParams` and started to use this function;

## 🤔 How Has This Been Tested?

Tested via e2e tests.

## 📝 Checklist

- [x] CI successfully passed
- [x] I added new mocks and corresponding unit-tests if library API was
changed
  • Loading branch information
kirillzyusko authored Aug 20, 2024
1 parent c65f43f commit 66f3e42
Showing 1 changed file with 29 additions and 41 deletions.
70 changes: 29 additions & 41 deletions ios/observers/KeyboardMovementObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,73 +162,49 @@ public class KeyboardMovementObserver: NSObject {
}

@objc func keyboardWillAppear(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let (duration, frame) = metaDataFromNotification(notification)
if let keyboardFrame = frame {
tag = UIResponder.current.reactViewTag
let keyboardHeight = keyboardFrame.cgRectValue.size.height
let duration = Int(
(notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double ?? 0) * 1000
)
self.keyboardHeight = keyboardHeight
self.duration = duration

var data = [AnyHashable: Any]()
data["height"] = keyboardHeight
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp
data["target"] = tag

onRequestAnimation()
onEvent("onKeyboardMoveStart", Float(keyboardHeight) as NSNumber, 1, duration as NSNumber, tag)
onNotify("KeyboardController::keyboardWillShow", data)
onNotify("KeyboardController::keyboardWillShow", getEventParams(keyboardHeight, duration))

setupKeyboardWatcher()
initializeAnimation(fromValue: prevKeyboardPosition, toValue: keyboardHeight)
}
}

@objc func keyboardWillDisappear(_ notification: Notification) {
let duration = Int(
(notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double ?? 0) * 1000
)
let (duration, _) = metaDataFromNotification(notification)
tag = UIResponder.current.reactViewTag
self.duration = duration

var data = [AnyHashable: Any]()
data["height"] = 0
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp
data["target"] = tag

onRequestAnimation()
onEvent("onKeyboardMoveStart", 0, 0, duration as NSNumber, tag)
onNotify("KeyboardController::keyboardWillHide", data)
onNotify("KeyboardController::keyboardWillHide", getEventParams(0, duration))

setupKeyboardWatcher()
removeKVObserver()
initializeAnimation(fromValue: prevKeyboardPosition, toValue: 0)
}

@objc func keyboardDidAppear(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let (duration, frame) = metaDataFromNotification(notification)
if let keyboardFrame = frame {
let (position, _) = keyboardView.framePositionInWindow
let keyboardHeight = keyboardFrame.cgRectValue.size.height
tag = UIResponder.current.reactViewTag
self.keyboardHeight = keyboardHeight
let duration = Int(
(notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double ?? 0) * 1000
)
// always limit progress to the maximum possible value
let progress = min(position / self.keyboardHeight, 1.0)

var data = [AnyHashable: Any]()
data["height"] = position
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp
data["target"] = tag

onCancelAnimation()
onEvent("onKeyboardMoveEnd", position as NSNumber, progress as NSNumber, duration as NSNumber, tag)
onNotify("KeyboardController::keyboardDidShow", data)
onNotify("KeyboardController::keyboardDidShow", getEventParams(position, duration))

removeKeyboardWatcher()
setupKVObserver()
Expand All @@ -237,19 +213,12 @@ public class KeyboardMovementObserver: NSObject {
}

@objc func keyboardDidDisappear(_ notification: Notification) {
let duration = Int(
(notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double ?? 0) * 1000
)
let (duration, _) = metaDataFromNotification(notification)
tag = UIResponder.current.reactViewTag
var data = [AnyHashable: Any]()
data["height"] = 0
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp
data["target"] = tag

onCancelAnimation()
onEvent("onKeyboardMoveEnd", 0 as NSNumber, 0, duration as NSNumber, tag)
onNotify("KeyboardController::keyboardDidHide", data)
onNotify("KeyboardController::keyboardDidHide", getEventParams(0, duration))

removeKeyboardWatcher()
animation = nil
Expand Down Expand Up @@ -331,4 +300,23 @@ public class KeyboardMovementObserver: NSObject {
tag
)
}

private func metaDataFromNotification(_ notification: Notification) -> (Int, NSValue?) {
let duration = Int(
(notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double ?? 0) * 1000
)
let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue

return (duration, keyboardFrame)
}

private func getEventParams(_ height: Double, _ duration: Int) -> [AnyHashable: Any] {
var data = [AnyHashable: Any]()
data["height"] = height
data["duration"] = duration
data["timestamp"] = Date.currentTimeStamp
data["target"] = tag

return data
}
}

0 comments on commit 66f3e42

Please sign in to comment.