summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/Login/AccountTextField.swift
blob: bb2d0e6415d8f03ca0ab8489cef7b8c815125e3c (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
//  AccountTextField.swift
//  MullvadVPN
//
//  Created by pronebird on 20/03/2019.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import UIKit

class AccountTextField: CustomTextField, UITextFieldDelegate {
    enum GroupingStyle: Int {
        case full
        case lastPart

        var size: UInt8 {
            switch self {
            case .full:
                return 4
            case .lastPart:
                return 1
            }
        }
    }

    private var groupSize: GroupingStyle = .full
    private lazy var inputFormatter = InputTextFormatter(
        configuration: InputTextFormatter.Configuration(
            allowedInput: .numeric,
            groupSeparator: " ",
            groupSize: 4,
            maxGroups: groupSize.size
        ))

    var onReturnKey: ((AccountTextField) -> Bool)?

    init(groupingStyle: GroupingStyle = .full) {
        self.groupSize = groupingStyle
        super.init(frame: .zero)
        commonInit()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func commonInit() {
        backgroundColor = .clear
        cornerRadius = 0

        delegate = self
        pasteDelegate = inputFormatter

        NotificationCenter.default.addObserver(
            self,
            selector: #selector(keyboardWillShow(_:)),
            name: UIWindow.keyboardWillShowNotification,
            object: nil
        )
    }

    var autoformattingText: String {
        get {
            inputFormatter.formattedString
        }
        set {
            inputFormatter.replace(with: newValue)
            inputFormatter.updateTextField(self)
        }
    }

    var parsedToken: String {
        inputFormatter.string
    }

    var enableReturnKey = true {
        didSet {
            updateKeyboardReturnKey()
        }
    }

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(captureTextFromCamera(_:)) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }

    // MARK: - UITextFieldDelegate

    func textField(
        _ textField: UITextField,
        shouldChangeCharactersIn range: NSRange,
        replacementString string: String
    ) -> Bool {
        inputFormatter.textField(
            textField,
            shouldChangeCharactersIn: range,
            replacementString: string
        )
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        onReturnKey?(self) ?? true
    }

    // MARK: - Notifications

    @objc private func keyboardWillShow(_ notification: Notification) {
        if isFirstResponder {
            updateKeyboardReturnKey()
        }
    }

    // MARK: - Keyboard

    private func updateKeyboardReturnKey() {
        setEnableKeyboardReturnKey(enableReturnKey)
    }

    private func setEnableKeyboardReturnKey(_ enableReturnKey: Bool) {
        let selector = NSSelectorFromString("setReturnKeyEnabled:")
        if let inputDelegate = inputDelegate as? NSObject, inputDelegate.responds(to: selector) {
            inputDelegate.setValue(enableReturnKey, forKey: "returnKeyEnabled")
        }
    }

    // MARK: - Accessibility

    override var accessibilityValue: String? {
        get {
            if text?.isEmpty ?? true {
                return ""
            } else {
                return super.accessibilityValue
            }
        }
        set {
            super.accessibilityValue = newValue
        }
    }
}