Skip to content

Commit e736db9

Browse files
authored
refactor: isolate keyboard view locator (#1052)
## 📜 Description Isolate keyboard view locating into separate class. ## 💡 Motivation and Context `KeyboardMovementObserver` almost reaches its cognitive complexity, so I decided to simplify that class a little bit and group a related logic for discovering keyboard into separate class. ## 📢 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 `KeyboardViewLocator` class; - use `KeyboardViewLocator` in `KeyboardMovementObserver` ## 🤔 How Has This Been Tested? Tested via e2e test. ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 88cfdf1 commit e736db9

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

ios/observers/KeyboardMovementObserver.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,7 @@ public class KeyboardMovementObserver: NSObject {
1818
var onRequestAnimation: () -> Void
1919
var onCancelAnimation: () -> Void
2020
// progress tracker
21-
private var _keyboardView: UIView?
22-
private var keyboardView: UIView? {
23-
let windowsCount = UIApplication.shared.windows.count
24-
25-
if _keyboardView == nil || windowsCount != _windowsCount {
26-
_keyboardView = KeyboardView.find()
27-
_windowsCount = windowsCount
28-
}
29-
30-
return _keyboardView
31-
}
32-
33-
private var _windowsCount: Int = 0
21+
private var keyboardView: UIView? { KeyboardViewLocator.shared.resolve() }
3422
private var prevKeyboardPosition = 0.0
3523
private var displayLink: CADisplayLink!
3624
private var interactiveKeyboardObserver: NSKeyValueObservation?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// KeyboardViewLocator.swift
3+
// Pods
4+
//
5+
// Created by Kiryl Ziusko on 25/07/2025.
6+
//
7+
8+
final class KeyboardViewLocator {
9+
static let shared = KeyboardViewLocator()
10+
private var cachedKeyboardView: UIView?
11+
private var cachedWindowsCount: Int = 0
12+
13+
func resolve() -> UIView? {
14+
let currentWindowsCount = UIApplication.shared.windows.count
15+
16+
if cachedKeyboardView == nil || currentWindowsCount != cachedWindowsCount {
17+
cachedKeyboardView = KeyboardView.find()
18+
cachedWindowsCount = currentWindowsCount
19+
}
20+
21+
return cachedKeyboardView
22+
}
23+
}

0 commit comments

Comments
 (0)