Skip to content

Commit

Permalink
refactor: create separate class to simplify KeyboardMovementObserver …
Browse files Browse the repository at this point in the history
…implementation
  • Loading branch information
kirillzyusko committed Nov 24, 2024
1 parent 3846dc8 commit 1d87523
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
31 changes: 31 additions & 0 deletions ios/observers/KeyboardEventsIgnorer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// KeyboardEventsIgnorer.swift
// Pods
//
// Created by Kiryl Ziusko on 24/11/2024.
//

import Foundation

class KeyboardEventsIgnorer {
var shouldIgnoreKeyboardEvents = false

init() {
NotificationCenter.default.addObserver(
self,
selector: #selector(handleIgnoreKeyboardEventsNotification),
name: .shouldIgnoreKeyboardEvents,
object: nil
)
}

@objc private func handleIgnoreKeyboardEventsNotification(_ notification: Notification) {
if let userInfo = notification.userInfo, let value = userInfo["ignore"] as? Bool {
shouldIgnoreKeyboardEvents = value
}
}

deinit {
NotificationCenter.default.removeObserver(self)
}
}
18 changes: 7 additions & 11 deletions ios/observers/KeyboardMovementObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public class KeyboardMovementObserver: NSObject {
private var tag: NSNumber = -1
private var animation: KeyboardAnimation?
private var didShowDeadline: Int64 = 0
private var shouldIgnoreKeyboardEvents = false
// class intances

Check failure on line 54 in ios/observers/KeyboardMovementObserver.swift

View workflow job for this annotation

GitHub Actions / 🔍 spellcheck

Unknown word (intances)
private let eventsIgnorer = KeyboardEventsIgnorer()

@objc public init(
handler: @escaping (NSString, NSNumber, NSNumber, NSNumber, NSNumber) -> Void,
Expand Down Expand Up @@ -96,11 +97,6 @@ public class KeyboardMovementObserver: NSObject {
name: UIResponder.keyboardDidHideNotification,
object: nil
)
NotificationCenter.default.addObserver(forName: .shouldIgnoreKeyboardEvents, object: nil, queue: .main) { notification in
if let userInfo = notification.userInfo, let value = userInfo["ignore"] as? Bool {
self.shouldIgnoreKeyboardEvents = value
}
}
}

private func setupKVObserver() {
Expand Down Expand Up @@ -183,7 +179,7 @@ public class KeyboardMovementObserver: NSObject {
}

@objc func keyboardWillAppear(_ notification: Notification) {
guard !shouldIgnoreKeyboardEvents else { return }
guard !eventsIgnorer.shouldIgnoreKeyboardEvents else { return }
print("keyboardWillAppear \(Date.currentTimeStamp)")
let (duration, frame) = notification.keyboardMetaData()
if let keyboardFrame = frame {
Expand All @@ -203,7 +199,7 @@ public class KeyboardMovementObserver: NSObject {
}

@objc func keyboardWillDisappear(_ notification: Notification) {
guard !shouldIgnoreKeyboardEvents else { return }
guard !eventsIgnorer.shouldIgnoreKeyboardEvents else { return }
print("keyboardWillDisappear \(Date.currentTimeStamp)")
let (duration, _) = notification.keyboardMetaData()
tag = UIResponder.current.reactViewTag
Expand All @@ -228,8 +224,8 @@ public class KeyboardMovementObserver: NSObject {
tag = UIResponder.current.reactViewTag
self.keyboardHeight = keyboardHeight

guard !shouldIgnoreKeyboardEvents else {
shouldIgnoreKeyboardEvents = false
guard !eventsIgnorer.shouldIgnoreKeyboardEvents else {
eventsIgnorer.shouldIgnoreKeyboardEvents = false
return
}

Expand All @@ -252,7 +248,7 @@ public class KeyboardMovementObserver: NSObject {
}

@objc func keyboardDidDisappear(_ notification: Notification) {
guard !shouldIgnoreKeyboardEvents else { return }
guard !eventsIgnorer.shouldIgnoreKeyboardEvents else { return }
print("keyboardDidDisappear \(Date.currentTimeStamp)")
let (duration, _) = notification.keyboardMetaData()
tag = UIResponder.current.reactViewTag
Expand Down

0 comments on commit 1d87523

Please sign in to comment.