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
import SwiftUI
struct ProfileLinkEditorView: View {
@Environment(\.dismiss) private var dismiss
@State private var title: String
@State private var address: String
private let link: ProfileEdit.Link?
private let commit: (ProfileEdit.Link) -> Void
init(link: ProfileEdit.Link?, commit: @escaping (ProfileEdit.Link) -> Void) {
self.link = link
self.commit = commit
_title = State(initialValue: link?.title ?? "")
_address = State(initialValue: link?.address ?? "")
}
var body: some View {
let trimmedTitle = title.trimmingCharacters(in: .whitespacesAndNewlines)
let trimmedAddress = address.trimmingCharacters(in: .whitespacesAndNewlines)
let updated = ProfileEdit.Link(id: link?.id ?? UUID(), title: trimmedTitle, address: trimmedAddress)
let hasChanges = trimmedTitle != link?.title || trimmedAddress != link?.address
Form {
Section("Address") {
TextField("https://example.com", text: $address)
.textContentType(.URL)
.keyboardType(.URL)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.accessibilityLabel("Link address")
.accessibilityIdentifier("profileLinkAddress")
}
Section("Title") {
TextField("Optional title", text: $title)
.accessibilityIdentifier("profileLinkTitle")
}
if !trimmedAddress.isEmpty, let error = updated.validationError {
Section { Text(error).foregroundStyle(.red) }
}
}
.appNavigationTitle(link == nil ? "Add Link" : "Edit Link")
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") {
commit(updated)
dismiss()
}
.disabled(!hasChanges || updated.validationError != nil)
}
}
}
}