summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/Settings/SettingsDNSTextCell.swift
blob: cf024f7c435b6e3dda7f8c9d3a67d58e3a1153c9 (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
//
//  SettingsDNSTextCell.swift
//  MullvadVPN
//
//  Created by pronebird on 05/10/2021.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import UIKit

class SettingsDNSTextCell: SettingsCell, UITextFieldDelegate {
    var isValidInput = true {
        didSet {
            updateCellAppearance(animated: false)
        }
    }

    let textField = CustomTextField()

    var onTextChange: ((SettingsDNSTextCell) -> Void)?
    var onReturnKey: ((SettingsDNSTextCell) -> Void)?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        textField.font = .mullvadSmall
        textField.adjustsFontForContentSizeCategory = true
        textField.backgroundColor = .clear
        textField.textColor = UIColor.TextField.textColor
        textField.textMargins = UIMetrics.SettingsCell.textFieldContentInsets
        textField.placeholder = NSLocalizedString("Enter IP", comment: "")
        textField.setAccessibilityIdentifier(.dnsSettingsEnterIPAddressTextField)
        textField.cornerRadius = 0
        textField.keyboardType = .numbersAndPunctuation
        textField.returnKeyType = .done
        textField.autocorrectionType = .no
        textField.smartInsertDeleteType = .no
        textField.smartDashesType = .no
        textField.smartQuotesType = .no
        textField.spellCheckingType = .no
        textField.autocapitalizationType = .none
        textField.delegate = self

        NotificationCenter.default.addObserver(
            self,
            selector: #selector(textDidChange),
            name: UITextField.textDidChangeNotification,
            object: textField
        )

        backgroundView?.backgroundColor = UIColor.TextField.backgroundColor
        contentView.addSubview(textField)

        contentView.addConstrainedSubviews([textField]) {
            textField.pinEdgesToSuperview()
        }

        updateCellAppearance(animated: false)
    }

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

    override func prepareForReuse() {
        super.prepareForReuse()

        onTextChange = nil
        onReturnKey = nil

        textField.text = ""
        isValidInput = true
    }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)

        updateCellAppearance(animated: animated)
    }

    @objc func textDidChange() {
        onTextChange?(self)
    }

    private func updateCellAppearance(animated: Bool) {
        if animated {
            UIView.animate(withDuration: 0.25) {
                self.updateCellAppearance()
            }
        } else {
            updateCellAppearance()
        }
    }

    private func updateCellAppearance() {
        textField.isEnabled = isEditing

        if isEditing {
            textField.textMargins.left = UIMetrics.SettingsCell.textFieldContentInsets.left

            if isValidInput {
                textField.textColor = UIColor.TextField.textColor
            } else {
                textField.textColor = UIColor.TextField.invalidInputTextColor
            }

            backgroundView?.backgroundColor = UIColor.TextField.backgroundColor
        } else {
            textField.textMargins.left = UIMetrics.SettingsCell.textFieldNonEditingContentInsetLeft

            textField.textColor = .white
            backgroundView?.backgroundColor = UIColor.Cell.Background.indentationLevelZero
        }
    }

    // MARK: - UITextFieldDelegate

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

    func textField(
        _ textField: UITextField,
        shouldChangeCharactersIn range: NSRange,
        replacementString string: String
    ) -> Bool {
        let ipv4AddressCharset = CharacterSet.ipv4AddressCharset
        let ipv6AddressCharset = CharacterSet.ipv6AddressCharset

        return [ipv4AddressCharset, ipv6AddressCharset].contains { charset in
            string.unicodeScalars.allSatisfy { scalar in
                charset.contains(scalar)
            }
        }
    }
}