-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathCustomListSaveAlert.swift
90 lines (80 loc) · 3.12 KB
/
CustomListSaveAlert.swift
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
//
// CustomListSaveAlert.swift
// MullvadVPN
//
// Created by Jon Petersson on 2024-02-08.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import SwiftUI
class CustomListSaveAlertViewModel: ObservableObject {
@Published var inputIsValid: Bool
init(inputIsValid: Bool = true) {
self.inputIsValid = inputIsValid
}
}
struct CustomListSaveAlert: View {
@State private var inputText = ""
@ObservedObject var viewModel: CustomListSaveAlertViewModel
var didTapSave: ((String) -> Void)?
var didTapCancel: (() -> Void)?
var body: some View {
GeometryReader(content: { geometry in
ZStack {
VStack(alignment: .leading, spacing: 8) {
Text("Edit list name")
.font(.system(size: 12, weight: .semibold))
.foregroundColor(.white.opacity(0.8))
TextField("", text: $inputText)
.textFieldStyle(CustomTextFieldStyle())
ErrorLabel(inputIsValid: viewModel.inputIsValid)
VStack(spacing: 16) {
SUIAppButton(text: "Save", style: .success)
.frame(height: 42)
.onTapGesture { didTapSave?(inputText) }
SUIAppButton(text: "Cancel", style: .default)
.frame(height: 42)
.onTapGesture { didTapCancel?() }
}
}
.frame(
minWidth: 0,
maxWidth: geometry.size.width > UIMetrics.preferredFormSheetContentSize.width
? UIMetrics.preferredFormSheetContentSize.width
: .infinity
)
.padding(EdgeInsets(UIMetrics.CustomAlert.containerMargins))
.background(Color(.secondaryColor))
.cornerRadius(11)
}
.padding(EdgeInsets(UIMetrics.CustomAlert.containerMargins))
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.black.opacity(0.6))
.ignoresSafeArea()
})
}
}
struct CustomListSaveAlert_Previews: PreviewProvider {
static var previews: some View {
CustomListSaveAlert(viewModel: CustomListSaveAlertViewModel(inputIsValid: true))
CustomListSaveAlert(viewModel: CustomListSaveAlertViewModel(inputIsValid: false))
}
}
private struct CustomTextFieldStyle: TextFieldStyle {
func _body(configuration: TextField<Self._Label>) -> some View {
configuration
.padding(EdgeInsets(top: 6, leading: 8, bottom: 6, trailing: 8))
.background(Color.white)
.cornerRadius(4)
.font(.system(size: 15))
}
}
private struct ErrorLabel: View {
let inputIsValid: Bool
var body: some View {
Text("Name is already taken")
.font(.system(size: 12, weight: .semibold))
.foregroundColor(Color(.dangerColor))
.padding(.bottom, inputIsValid ? 0 : 4)
.opacity(inputIsValid ? 0 : 1)
}
}