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

import Foundation
import UIKit

class SettingsDNSTextCell: SettingsCell, UITextFieldDelegate {

    var isValidInput: Bool = 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.translatesAutoresizingMaskIntoConstraints = false
        textField.font = UIFont.systemFont(ofSize: 17)
        textField.backgroundColor = .clear
        textField.textColor = UIColor.TextField.textColor
        textField.textMargins = UIMetrics.settingsCellLayoutMargins
        textField.placeholder = NSLocalizedString("DNS_TEXT_CELL_PLACEHOLDER", tableName: "Settings", value: "Enter IP", comment: "")
        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)

        if #available(iOS 13.0, *) {
            overrideUserInterfaceStyle = .light
        }

        NSLayoutConstraint.activate([
            textField.topAnchor.constraint(equalTo: contentView.topAnchor),
            textField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            textField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            textField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
        ])

        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 {
            if isValidInput {
                textField.textColor = UIColor.TextField.textColor
            } else {
                textField.textColor = UIColor.TextField.invalidInputTextColor
            }

            backgroundView?.backgroundColor = UIColor.TextField.backgroundColor
        } else {
            textField.textColor = .white

            backgroundView?.backgroundColor = UIColor.SubCell.backgroundColor
        }
    }

    // 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
            return string.unicodeScalars.allSatisfy { scalar in
                return charset.contains(scalar)
            }
        }
    }

}