f1a4efecaf1a64f51762df8947bd4a2e5df555d0
[mirrors/VoiceChatSdk_iOS.git] /
1 //
2 //  IQKeyboardReturnKeyHandler.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 /**
27 Manages the return key to work like next/done in a view hierarchy.
28 */
29 @available(iOSApplicationExtension, unavailable)
30 @MainActor
31 @objc public final class IQKeyboardReturnKeyHandler: NSObject {
32
33     // MARK: Settings
34
35     /**
36     Delegate of textField/textView.
37     */
38     @objc public weak var delegate: (UITextFieldDelegate & UITextViewDelegate)?
39
40     /**
41     Set the last textfield return key type. Default is UIReturnKeyDefault.
42     */
43     @objc public var lastTextFieldReturnKeyType: UIReturnKeyType = UIReturnKeyType.default {
44
45         didSet {
46             for model in textFieldInfoCache {
47                 if let view: UIView = model.textFieldView {
48                     updateReturnKeyTypeOnTextField(view)
49                 }
50             }
51         }
52     }
53
54     // MARK: Initialization/De-initialization
55
56     @objc public override init() {
57         super.init()
58     }
59
60     /**
61     Add all the textFields available in UIViewController's view.
62     */
63     @objc public init(controller: UIViewController) {
64         super.init()
65
66         addResponderFromView(controller.view)
67     }
68
69     deinit {
70
71 //        for model in textFieldInfoCache {
72 //            model.restore()
73 //        }
74
75         textFieldInfoCache.removeAll()
76     }
77
78     // MARK: Private variables
79     private var textFieldInfoCache: [IQTextFieldViewInfoModel] = []
80
81     // MARK: Private Functions
82     internal func textFieldViewCachedInfo(_ textField: UIView) -> IQTextFieldViewInfoModel? {
83
84         for model in textFieldInfoCache {
85
86             if let view: UIView = model.textFieldView {
87                 if view == textField {
88                     return model
89                 }
90             }
91         }
92
93         return nil
94     }
95
96     internal func updateReturnKeyTypeOnTextField(_ view: UIView) {
97         var superConsideredView: UIView?
98
99         // If find any consider responderView in it's upper hierarchy then will get deepResponderView. (Bug ID: #347)
100         for allowedClasse in IQKeyboardManager.shared.toolbarPreviousNextAllowedClasses {
101
102             superConsideredView = view.iq.superviewOf(type: allowedClasse)
103
104             if superConsideredView != nil {
105                 break
106             }
107         }
108
109         var textFields: [UIView] = []
110
111         // If there is a tableView in view's hierarchy, then fetching all it's subview that responds.
112         if let unwrappedTableView: UIView = superConsideredView {     //   (Enhancement ID: #22)
113             textFields = unwrappedTableView.iq.deepResponderViews()
114         } else {  // Otherwise fetching all the siblings
115
116             textFields = view.iq.responderSiblings()
117
118             // Sorting textFields according to behavior
119             switch IQKeyboardManager.shared.toolbarConfiguration.manageBehavior {
120                 // If needs to sort it by tag
121             case .byTag:        textFields = textFields.sortedByTag()
122                 // If needs to sort it by Position
123             case .byPosition:   textFields = textFields.sortedByPosition()
124             default:    break
125             }
126         }
127
128         if let lastView: UIView = textFields.last {
129
130             if let textField: UITextField = view as? UITextField {
131
132                 // If it's the last textField in responder view, else next
133                 textField.returnKeyType = (view == lastView)    ?   lastTextFieldReturnKeyType: UIReturnKeyType.next
134             } else if let textView: UITextView = view as? UITextView {
135
136                 // If it's the last textField in responder view, else next
137                 textView.returnKeyType = (view == lastView)    ?   lastTextFieldReturnKeyType: UIReturnKeyType.next
138             }
139         }
140     }
141
142     // MARK: Registering/Unregistering textFieldView
143
144     /**
145     Should pass UITextField/UITextView instance. Assign textFieldView delegate to self, change it's returnKeyType.
146
147     @param view UITextField/UITextView object to register.
148     */
149     @objc public func addTextFieldView(_ view: UIView) {
150
151         if let textField: UITextField = view as? UITextField {
152             let model = IQTextFieldViewInfoModel(textField: textField)
153             textFieldInfoCache.append(model)
154             textField.delegate = self
155
156         } else if let textView: UITextView = view as? UITextView {
157             let model = IQTextFieldViewInfoModel(textView: textView)
158             textFieldInfoCache.append(model)
159             textView.delegate = self
160         }
161     }
162
163     /**
164     Should pass UITextField/UITextView instance. Restore it's textFieldView delegate and it's returnKeyType.
165     
166     @param view UITextField/UITextView object to unregister.
167     */
168     @objc public func removeTextFieldView(_ view: UIView) {
169
170         if let model: IQTextFieldViewInfoModel = textFieldViewCachedInfo(view) {
171             model.restore()
172
173             if let index: Int = textFieldInfoCache.firstIndex(where: { $0.textFieldView == view}) {
174                 textFieldInfoCache.remove(at: index)
175             }
176         }
177     }
178
179     /**
180     Add all the UITextField/UITextView responderView's.
181     
182     @param view UIView object to register all it's responder subviews.
183     */
184     @objc public func addResponderFromView(_ view: UIView) {
185
186         let textFields: [UIView] = view.iq.deepResponderViews()
187
188         for textField in textFields {
189
190             addTextFieldView(textField)
191         }
192     }
193
194     /**
195     Remove all the UITextField/UITextView responderView's.
196     
197     @param view UIView object to unregister all it's responder subviews.
198     */
199     @objc public func removeResponderFromView(_ view: UIView) {
200
201         let textFields: [UIView] = view.iq.deepResponderViews()
202
203         for textField in textFields {
204
205             removeTextFieldView(textField)
206         }
207     }
208
209     @discardableResult
210     internal func goToNextResponderOrResign(_ view: UIView) -> Bool {
211
212         var superConsideredView: UIView?
213
214         // If find any consider responderView in it's upper hierarchy then will get deepResponderView. (Bug ID: #347)
215         for allowedClass in IQKeyboardManager.shared.toolbarPreviousNextAllowedClasses {
216
217             superConsideredView = view.iq.superviewOf(type: allowedClass)
218
219             if superConsideredView != nil {
220                 break
221             }
222         }
223
224         var textFields: [UIView] = []
225
226         // If there is a tableView in view's hierarchy, then fetching all it's subview that responds.
227         if let unwrappedTableView: UIView = superConsideredView {     //   (Enhancement ID: #22)
228             textFields = unwrappedTableView.iq.deepResponderViews()
229         } else {  // Otherwise fetching all the siblings
230
231             textFields = view.iq.responderSiblings()
232
233             // Sorting textFields according to behavior
234             switch IQKeyboardManager.shared.toolbarConfiguration.manageBehavior {
235                 // If needs to sort it by tag
236             case .byTag:        textFields = textFields.sortedByTag()
237                 // If needs to sort it by Position
238             case .byPosition:   textFields = textFields.sortedByPosition()
239             default:
240                 break
241             }
242         }
243
244         //  Getting index of current textField.
245         if let index: Int = textFields.firstIndex(of: view) {
246             //  If it is not last textField. then it's next object becomeFirstResponder.
247             if index < (textFields.count - 1) {
248
249                 let nextTextField: UIView = textFields[index+1]
250                 nextTextField.becomeFirstResponder()
251                 return false
252             } else {
253
254                 view.resignFirstResponder()
255                 return true
256             }
257         } else {
258             return true
259         }
260     }
261 }