2 // IQActiveConfiguration.swift
3 // https://github.com/hackiftekhar/IQKeyboardManager
4 // Copyright (c) 2013-24 Iftekhar Qurashi.
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 @available(iOSApplicationExtension, unavailable)
28 internal final class IQActiveConfiguration {
30 private let keyboardListener: IQKeyboardListener = IQKeyboardListener()
31 private let textFieldViewListener: IQTextFieldViewListener = IQTextFieldViewListener()
33 private var changeObservers: [AnyHashable: ConfigurationCompletion] = [:]
35 @objc public enum Event: Int {
52 private var lastEvent: Event = .hide
54 var rootControllerConfiguration: IQRootControllerConfiguration?
57 if textFieldViewInfo != nil,
58 let rootControllerConfiguration = rootControllerConfiguration {
59 return rootControllerConfiguration.isReady
66 addTextFieldViewListener()
69 private func sendEvent() {
71 if let textFieldViewInfo = textFieldViewInfo,
72 let rootControllerConfiguration = rootControllerConfiguration,
73 rootControllerConfiguration.isReady {
74 if keyboardInfo.keyboardShowing {
75 if lastEvent == .hide {
76 self.notify(event: .show, keyboardInfo: keyboardInfo, textFieldViewInfo: textFieldViewInfo)
78 self.notify(event: .change, keyboardInfo: keyboardInfo, textFieldViewInfo: textFieldViewInfo)
80 } else if lastEvent != .hide {
81 if rootControllerConfiguration.beginOrientation == rootControllerConfiguration.currentOrientation {
82 self.notify(event: .hide, keyboardInfo: keyboardInfo, textFieldViewInfo: textFieldViewInfo)
83 self.rootControllerConfiguration = nil
84 } else if rootControllerConfiguration.hasChanged {
85 animate(alongsideTransition: {
86 rootControllerConfiguration.restore()
93 private func updateRootController(info: IQTextFieldViewInfo?) {
95 guard let info = info,
96 let controller: UIViewController = info.textFieldView.iq.parentContainerViewController() else {
97 if let rootControllerConfiguration = rootControllerConfiguration,
98 rootControllerConfiguration.hasChanged {
99 animate(alongsideTransition: {
100 rootControllerConfiguration.restore()
103 rootControllerConfiguration = nil
107 let newConfiguration = IQRootControllerConfiguration(rootController: controller)
109 if newConfiguration.rootController.view.window != rootControllerConfiguration?.rootController.view.window ||
110 newConfiguration.beginOrientation != rootControllerConfiguration?.beginOrientation {
112 if rootControllerConfiguration?.rootController != newConfiguration.rootController {
114 // If there was an old configuration but things are changed
115 if let rootControllerConfiguration = rootControllerConfiguration,
116 rootControllerConfiguration.hasChanged {
117 animate(alongsideTransition: {
118 rootControllerConfiguration.restore()
123 rootControllerConfiguration = newConfiguration
128 @available(iOSApplicationExtension, unavailable)
129 extension IQActiveConfiguration {
131 var keyboardInfo: IQKeyboardInfo {
132 return keyboardListener.keyboardInfo
135 private func addKeyboardListener() {
136 keyboardListener.registerSizeChange(identifier: "IQActiveConfiguration", changeHandler: { [self] name, _ in
138 if let info = textFieldViewInfo, keyboardInfo.keyboardShowing {
139 if let rootControllerConfiguration = rootControllerConfiguration {
140 let beginIsPortrait: Bool = rootControllerConfiguration.beginOrientation.isPortrait
141 let currentIsPortrait: Bool = rootControllerConfiguration.currentOrientation.isPortrait
142 if beginIsPortrait != currentIsPortrait {
143 updateRootController(info: info)
146 updateRootController(info: info)
152 if name == .didHide {
153 updateRootController(info: nil)
158 public func animate(alongsideTransition transition: @escaping () -> Void, completion: (() -> Void)? = nil) {
159 keyboardListener.animate(alongsideTransition: transition, completion: completion)
163 @available(iOSApplicationExtension, unavailable)
164 extension IQActiveConfiguration {
166 var textFieldViewInfo: IQTextFieldViewInfo? {
167 return textFieldViewListener.textFieldViewInfo
170 private func addTextFieldViewListener() {
171 textFieldViewListener.registerTextFieldViewChange(identifier: "IQActiveConfiguration",
172 changeHandler: { [self] info in
173 if info.name == .beginEditing {
174 updateRootController(info: info)
181 @available(iOSApplicationExtension, unavailable)
182 extension IQActiveConfiguration {
184 typealias ConfigurationCompletion = (_ event: Event,
185 _ keyboardInfo: IQKeyboardInfo,
186 _ textFieldInfo: IQTextFieldViewInfo) -> Void
188 func registerChange(identifier: AnyHashable, changeHandler: @escaping ConfigurationCompletion) {
189 changeObservers[identifier] = changeHandler
192 func unregisterChange(identifier: AnyHashable) {
193 changeObservers[identifier] = nil
196 private func notify(event: Event, keyboardInfo: IQKeyboardInfo, textFieldViewInfo: IQTextFieldViewInfo) {
199 for block in changeObservers.values {
200 block(event, keyboardInfo, textFieldViewInfo)