7718e81b113a274faef295e0e490db35339758f1
[mirrors/VoiceChatSdk_iOS.git] /
1 //
2 //  IQTextFieldViewListener.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 public class IQTextFieldViewListener {
29
30     private var textFieldViewObservers: [AnyHashable: TextFieldViewCompletion] = [:]
31
32 #if swift(>=5.7)
33     private(set) var lastTextFieldViewInfo: IQTextFieldViewInfo?
34 #endif
35
36     private(set) var textFieldViewInfo: IQTextFieldViewInfo?
37
38     public var textFieldView: UIView? {
39         return textFieldViewInfo?.textFieldView
40     }
41
42     public init() {
43         //  Registering for keyboard notification.
44         NotificationCenter.default.addObserver(self, selector: #selector(self.didBeginEditing(_:)),
45                                                name: UITextField.textDidBeginEditingNotification, object: nil)
46         NotificationCenter.default.addObserver(self, selector: #selector(self.didEndEditing(_:)),
47                                                name: UITextField.textDidEndEditingNotification, object: nil)
48
49         NotificationCenter.default.addObserver(self, selector: #selector(self.didBeginEditing(_:)),
50                                                name: UITextView.textDidBeginEditingNotification, object: nil)
51         NotificationCenter.default.addObserver(self, selector: #selector(self.didEndEditing(_:)),
52                                                name: UITextView.textDidEndEditingNotification, object: nil)
53     }
54
55     @objc private func didBeginEditing(_ notification: Notification) {
56         guard let info: IQTextFieldViewInfo = IQTextFieldViewInfo(notification: notification,
57                                                                   name: .beginEditing) else {
58             return
59         }
60
61 #if swift(>=5.7)
62
63         if #available(iOS 16.0, *),
64            let lastTextFieldViewInfo = lastTextFieldViewInfo,
65            let textView: UITextView = lastTextFieldViewInfo.textFieldView as? UITextView,
66            textView.findInteraction?.isFindNavigatorVisible == true {
67             // // This means the this didBeginEditing call comes due to find interaction
68             textFieldViewInfo = lastTextFieldViewInfo
69             sendEvent(info: lastTextFieldViewInfo)
70         } else if textFieldViewInfo != info {
71             textFieldViewInfo = info
72             lastTextFieldViewInfo = nil
73             sendEvent(info: info)
74         } else {
75             lastTextFieldViewInfo = nil
76         }
77 #else
78         if textFieldViewInfo != info {
79             textFieldViewInfo = info
80             sendEvent(info: info)
81         }
82 #endif
83     }
84
85     @objc private func didEndEditing(_ notification: Notification) {
86         guard let info: IQTextFieldViewInfo = IQTextFieldViewInfo(notification: notification, name: .endEditing) else {
87             return
88         }
89
90         if textFieldViewInfo != info {
91 #if swift(>=5.7)
92             if #available(iOS 16.0, *),
93                let textView: UITextView = info.textFieldView as? UITextView,
94                 textView.isFindInteractionEnabled {
95                 lastTextFieldViewInfo = textFieldViewInfo
96             } else {
97                 lastTextFieldViewInfo = nil
98             }
99 #endif
100             textFieldViewInfo = info
101             sendEvent(info: info)
102             textFieldViewInfo = nil
103         }
104     }
105 }
106
107 @available(iOSApplicationExtension, unavailable)
108 public extension IQTextFieldViewListener {
109
110     typealias TextFieldViewCompletion = (_ info: IQTextFieldViewInfo) -> Void
111
112     func registerTextFieldViewChange(identifier: AnyHashable, changeHandler: @escaping TextFieldViewCompletion) {
113         textFieldViewObservers[identifier] = changeHandler
114     }
115
116     func unregisterSizeChange(identifier: AnyHashable) {
117         textFieldViewObservers[identifier] = nil
118     }
119
120     private func sendEvent(info: IQTextFieldViewInfo) {
121
122         for block in textFieldViewObservers.values {
123             block(info)
124         }
125     }
126 }