ef0b892ac4f1838bd828e2ff2d54fdc1e74b8330
[mirrors/VoiceChatSdk_iOS.git] /
1 //
2 //  IQActiveConfiguration.swift
3 //  https://github.com/hackiftekhar/IQKeyboardManager
4 //  Copyright (c) 2013-24 Iftekhar Qurashi.
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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
22 // THE SOFTWARE.
23
24 import UIKit
25
26 @available(iOSApplicationExtension, unavailable)
27 @MainActor
28 internal final class IQActiveConfiguration {
29
30     private let keyboardListener: IQKeyboardListener = IQKeyboardListener()
31     private let textFieldViewListener: IQTextFieldViewListener = IQTextFieldViewListener()
32
33     private var changeObservers: [AnyHashable: ConfigurationCompletion] = [:]
34
35     @objc public enum Event: Int {
36         case hide
37         case show
38         case change
39
40         var name: String {
41             switch self {
42             case .hide:
43                 return "hide"
44             case .show:
45                 return "show"
46             case .change:
47                 return "change"
48             }
49         }
50     }
51
52     private var lastEvent: Event = .hide
53
54     var rootControllerConfiguration: IQRootControllerConfiguration?
55
56     var isReady: Bool {
57         if textFieldViewInfo != nil,
58            let rootControllerConfiguration = rootControllerConfiguration {
59             return rootControllerConfiguration.isReady
60         }
61         return false
62     }
63
64     init() {
65         addKeyboardListener()
66         addTextFieldViewListener()
67     }
68
69     private func sendEvent() {
70
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)
77                 } else {
78                     self.notify(event: .change, keyboardInfo: keyboardInfo, textFieldViewInfo: textFieldViewInfo)
79                 }
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()
87                     }, completion: nil)
88                 }
89             }
90         }
91     }
92
93     private func updateRootController(info: IQTextFieldViewInfo?) {
94
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()
101                 }, completion: nil)
102             }
103             rootControllerConfiguration = nil
104             return
105         }
106
107         let newConfiguration = IQRootControllerConfiguration(rootController: controller)
108
109         if newConfiguration.rootController.view.window != rootControllerConfiguration?.rootController.view.window ||
110             newConfiguration.beginOrientation != rootControllerConfiguration?.beginOrientation {
111
112             if rootControllerConfiguration?.rootController != newConfiguration.rootController {
113
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()
119                     }, completion: nil)
120                 }
121             }
122
123             rootControllerConfiguration = newConfiguration
124         }
125     }
126 }
127
128 @available(iOSApplicationExtension, unavailable)
129 extension IQActiveConfiguration {
130
131     var keyboardInfo: IQKeyboardInfo {
132         return keyboardListener.keyboardInfo
133     }
134
135     private func addKeyboardListener() {
136         keyboardListener.registerSizeChange(identifier: "IQActiveConfiguration", changeHandler: { [self] name, _ in
137
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)
144                     }
145                 } else {
146                     updateRootController(info: info)
147                 }
148             }
149
150             self.sendEvent()
151
152             if name == .didHide {
153                 updateRootController(info: nil)
154             }
155         })
156     }
157
158     public func animate(alongsideTransition transition: @escaping () -> Void, completion: (() -> Void)? = nil) {
159         keyboardListener.animate(alongsideTransition: transition, completion: completion)
160     }
161 }
162
163 @available(iOSApplicationExtension, unavailable)
164 extension IQActiveConfiguration {
165
166     var textFieldViewInfo: IQTextFieldViewInfo? {
167         return textFieldViewListener.textFieldViewInfo
168     }
169
170     private func addTextFieldViewListener() {
171         textFieldViewListener.registerTextFieldViewChange(identifier: "IQActiveConfiguration",
172                                                           changeHandler: { [self] info in
173             if info.name == .beginEditing {
174                 updateRootController(info: info)
175                 self.sendEvent()
176             }
177         })
178     }
179 }
180
181 @available(iOSApplicationExtension, unavailable)
182 extension IQActiveConfiguration {
183
184     typealias ConfigurationCompletion = (_ event: Event,
185                                          _ keyboardInfo: IQKeyboardInfo,
186                                          _ textFieldInfo: IQTextFieldViewInfo) -> Void
187
188     func registerChange(identifier: AnyHashable, changeHandler: @escaping ConfigurationCompletion) {
189         changeObservers[identifier] = changeHandler
190     }
191
192     func unregisterChange(identifier: AnyHashable) {
193         changeObservers[identifier] = nil
194     }
195
196     private func notify(event: Event, keyboardInfo: IQKeyboardInfo, textFieldViewInfo: IQTextFieldViewInfo) {
197         lastEvent = event
198
199         for block in changeObservers.values {
200             block(event, keyboardInfo, textFieldViewInfo)
201         }
202     }
203 }