summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Views/MullvadPrimaryTextField.swift
blob: 2af39bf158389f8586b428eea9d770f63b2c0b00 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import SwiftUI

struct MullvadPrimaryTextField: View {
    private let label: String
    private let placeholder: String
    @Binding private var text: String
    @Binding private var suggestion: String?
    private let validate: ((String) -> Bool)?
    private let keyboardType: UIKeyboardType?

    init(
        label: String,
        placeholder: String,
        text: Binding<String>,
        suggestion: Binding<String?>? = nil,
        validate: ((String) -> Bool)? = nil,
        keyboardType: UIKeyboardType? = nil
    ) {
        self.label = label
        self.placeholder = placeholder
        self._text = text
        self._suggestion = suggestion ?? .constant(nil)
        self.validate = validate
        self.keyboardType = keyboardType
    }

    var isValid: Bool {
        validate?(text) ?? true
    }

    @FocusState private var isFocused: Bool
    @Environment(\.isEnabled) private var isEnabled

    private var showSuggestion: Bool {
        if let suggestion,
            !suggestion.isEmpty,
            suggestion != text,
            isEnabled
        {
            return true
        }
        return false
    }

    private var textFieldComponent: some View {
        TextField(
            placeholder,
            text: $text,
            prompt: Text(placeholder)
                .foregroundColor(
                    isEnabled ? .MullvadTextField.inputPlaceholder : .MullvadTextField.textDisabled
                )
        )
        .focused($isFocused)
        .padding(.vertical, 12)
    }

    var body: some View {
        VStack(alignment: .leading) {
            Text(label)
                .foregroundColor(.MullvadTextField.label)
            VStack(spacing: 0) {
                HStack(spacing: 4) {
                    if let keyboardType {
                        textFieldComponent.keyboardType(keyboardType)
                    } else {
                        textFieldComponent
                    }
                    if !text.isEmpty && isEnabled {
                        Button {
                            withAnimation {
                                text = ""
                            }
                        } label: {
                            Image.mullvadIconCross
                        }
                        .padding(0)
                    }
                }
                .zIndex(1)
                .padding(.horizontal, 8)
                .background(
                    isEnabled
                        ? Color.MullvadTextField.background
                        : Color.MullvadTextField
                            .backgroundDisabled
                )
                .foregroundColor(isEnabled ? .MullvadTextField.textInput : .MullvadTextField.textDisabled)
                .overlay {
                    if isFocused {
                        RoundedCorner(
                            cornerRadius: 4,
                            corners: !showSuggestion
                                ? [.allCorners]
                                : [
                                    .topLeft,
                                    .topRight,
                                ]
                        )
                        .stroke(
                            isValid
                                ? Color.MullvadTextField.borderFocused
                                : Color.MullvadTextField.borderError,
                            lineWidth: 4
                        )
                    } else if isEnabled {
                        RoundedCorner(
                            cornerRadius: 4,
                            corners: !showSuggestion
                                ? [.allCorners]
                                : [
                                    .topLeft,
                                    .topRight,
                                ]
                        )
                        .stroke(
                            isValid
                                ? Color.MullvadTextField.border
                                : Color.MullvadTextField.borderError,
                            lineWidth: 2
                        )
                    }
                }
                .clipShape(
                    RoundedCorner(
                        cornerRadius: 4,
                        corners: !showSuggestion
                            ? [.allCorners]
                            : [
                                .topLeft,
                                .topRight,
                            ]
                    ))

                if showSuggestion,
                    let suggestion
                {
                    HStack {
                        Button {
                            withAnimation {
                                text = suggestion
                            }
                        } label: {
                            Text(suggestion)
                                .foregroundColor(.MullvadTextField.textInput)
                            Spacer()
                        }
                        Button {
                            withAnimation {
                                self.suggestion = nil
                            }
                        } label: {
                            Image.mullvadIconCross
                        }
                    }
                    .transition(.move(edge: .top))
                    .padding(.horizontal, 8)
                    .padding(.vertical, 12)
                    .background(Color.MullvadTextField.backgroundSuggestion)
                }
            }
            .clipShape(
                RoundedCorner(cornerRadius: 4)
            )
        }
        .transformEffect(.identity)
        .animation(.default, value: showSuggestion)
    }
}

private struct RoundedCorner: Shape {
    var cornerRadius: CGFloat = .infinity
    var corners: UIRectCorner = .allCorners
    var insertBy: CGFloat = 0

    func path(in rect: CGRect) -> Path {
        let insetRect = rect.insetBy(dx: insertBy, dy: insertBy)
        let path = UIBezierPath(
            roundedRect: insetRect,
            byRoundingCorners: corners,
            cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)
        )
        return Path(path.cgPath)
    }
}

@available(iOS 17.0, *)
#Preview {
    @Previewable @State var suggestion: String? = "1234"
    @Previewable @State var text = ""
    VStack {
        MullvadPrimaryTextField(
            label: "Label",
            placeholder: "Placeholder text",
            text: $text,
            suggestion: $suggestion
        )

        MullvadPrimaryTextField(
            label: "Label",
            placeholder: "Placeholder text",
            text: $text,
            suggestion: $suggestion,
            validate: { _ in
                false
            }
        )

        MullvadPrimaryTextField(
            label: "Label",
            placeholder: "Placeholder text",
            text: $text,
            suggestion: $suggestion
        )
        .disabled(true)
    }
    .padding()
    .background(Color.yellow)
}

class UIMullvadPrimaryTextField: UIHostingController<UIMullvadPrimaryTextField.Wrapper> {
    var text: String {
        get {
            rootView.text
        }
        set {
            rootView.text = newValue
        }
    }

    struct Wrapper: View {
        let label: String
        let placeholder: String
        @State var text = ""
        @State var suggestion: String?
        let validate: ((String) -> Bool)?
        var contentType: UITextContentType?
        var keyboardType: UIKeyboardType = .default
        var submitLabel: SubmitLabel?
        var body: some View {
            MullvadPrimaryTextField(
                label: label,
                placeholder: placeholder,
                text: $text,
                suggestion: $suggestion,
                validate: validate
            )
            .textContentType(contentType)
            .keyboardType(keyboardType)
            .apply {
                if let submitLabel {
                    $0.submitLabel(submitLabel)
                } else {
                    $0
                }
            }
        }
    }

    init(
        label: String,
        placeholder: String,
        validate: ((String) -> Bool)? = nil,
        contentType: UITextContentType? = nil,
        keyboardType: UIKeyboardType = .default
    ) {
        let rootView = Wrapper(
            label: label,
            placeholder: placeholder,
            validate: validate,
            contentType: contentType,
            keyboardType: keyboardType
        )

        super.init(rootView: rootView)
    }

    override func viewDidLoad() {
        view.backgroundColor = .clear
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("Not implemented")
    }
}

struct UIMullvadPrimaryTextFieldRepresentable: UIViewRepresentable {
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }

    func makeUIView(context: Context) -> UIView {
        let controller = UIMullvadPrimaryTextField(label: "Label", placeholder: "Placeholder")
        context.coordinator.controller = controller
        return controller.view
    }

    func updateUIView(_ uiView: UIView, context: Context) {}

    class Coordinator {
        var controller: UIMullvadPrimaryTextField?
    }
}

#Preview {
    UIMullvadPrimaryTextFieldRepresentable()
        .padding()
        .background(Color.yellow)
}