杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
//  IQTextFieldViewListener.swift
//  https://github.com/hackiftekhar/IQKeyboardManager
//  Copyright (c) 2013-24 Iftekhar Qurashi.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
import UIKit
 
@available(iOSApplicationExtension, unavailable)
@MainActor
public class IQTextFieldViewListener {
 
    private var textFieldViewObservers: [AnyHashable: TextFieldViewCompletion] = [:]
 
#if swift(>=5.7)
    private(set) var lastTextFieldViewInfo: IQTextFieldViewInfo?
#endif
 
    private(set) var textFieldViewInfo: IQTextFieldViewInfo?
 
    public var textFieldView: UIView? {
        return textFieldViewInfo?.textFieldView
    }
 
    public init() {
        //  Registering for keyboard notification.
        NotificationCenter.default.addObserver(self, selector: #selector(self.didBeginEditing(_:)),
                                               name: UITextField.textDidBeginEditingNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.didEndEditing(_:)),
                                               name: UITextField.textDidEndEditingNotification, object: nil)
 
        NotificationCenter.default.addObserver(self, selector: #selector(self.didBeginEditing(_:)),
                                               name: UITextView.textDidBeginEditingNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.didEndEditing(_:)),
                                               name: UITextView.textDidEndEditingNotification, object: nil)
    }
 
    @objc private func didBeginEditing(_ notification: Notification) {
        guard let info: IQTextFieldViewInfo = IQTextFieldViewInfo(notification: notification,
                                                                  name: .beginEditing) else {
            return
        }
 
#if swift(>=5.7)
 
        if #available(iOS 16.0, *),
           let lastTextFieldViewInfo = lastTextFieldViewInfo,
           let textView: UITextView = lastTextFieldViewInfo.textFieldView as? UITextView,
           textView.findInteraction?.isFindNavigatorVisible == true {
            // // This means the this didBeginEditing call comes due to find interaction
            textFieldViewInfo = lastTextFieldViewInfo
            sendEvent(info: lastTextFieldViewInfo)
        } else if textFieldViewInfo != info {
            textFieldViewInfo = info
            lastTextFieldViewInfo = nil
            sendEvent(info: info)
        } else {
            lastTextFieldViewInfo = nil
        }
#else
        if textFieldViewInfo != info {
            textFieldViewInfo = info
            sendEvent(info: info)
        }
#endif
    }
 
    @objc private func didEndEditing(_ notification: Notification) {
        guard let info: IQTextFieldViewInfo = IQTextFieldViewInfo(notification: notification, name: .endEditing) else {
            return
        }
 
        if textFieldViewInfo != info {
#if swift(>=5.7)
            if #available(iOS 16.0, *),
               let textView: UITextView = info.textFieldView as? UITextView,
                textView.isFindInteractionEnabled {
                lastTextFieldViewInfo = textFieldViewInfo
            } else {
                lastTextFieldViewInfo = nil
            }
#endif
            textFieldViewInfo = info
            sendEvent(info: info)
            textFieldViewInfo = nil
        }
    }
}
 
@available(iOSApplicationExtension, unavailable)
public extension IQTextFieldViewListener {
 
    typealias TextFieldViewCompletion = (_ info: IQTextFieldViewInfo) -> Void
 
    func registerTextFieldViewChange(identifier: AnyHashable, changeHandler: @escaping TextFieldViewCompletion) {
        textFieldViewObservers[identifier] = changeHandler
    }
 
    func unregisterSizeChange(identifier: AnyHashable) {
        textFieldViewObservers[identifier] = nil
    }
 
    private func sendEvent(info: IQTextFieldViewInfo) {
 
        for block in textFieldViewObservers.values {
            block(info)
        }
    }
}