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
|
import SwiftUI
struct DomainEditView: View {
let domain: Domain
@ObservedObject var viewModel: DomainViewModel
let client: NjallaClient
@Environment(\.dismiss) private var dismiss
@State private var autorenew: Bool
@State private var autorenewDirty = false
@State private var mailforwarding: Bool
@State private var mailforwardingDirty = false
@State private var dnssec: Bool
@State private var dnssecDirty = false
@State private var lock: Bool
@State private var lockDirty = false
@State private var nameserversText: String
@State private var nameserversDirty = false
@State private var localErrorMessage: String?
init(domain: Domain, viewModel: DomainViewModel, client: NjallaClient) {
self.domain = domain
self.viewModel = viewModel
self.client = client
_autorenew = State(initialValue: domain.autorenew ?? false)
_mailforwarding = State(initialValue: domain.mailforwarding ?? false)
_dnssec = State(initialValue: domain.dnssec ?? false)
_lock = State(initialValue: domain.lock ?? false)
_nameserversText = State(initialValue: (domain.nameservers ?? []).joined(separator: "\n"))
}
var body: some View {
Form {
Section("Settings") {
Toggle("Autorenew", isOn: dirtyBinding(for: $autorenew, dirty: $autorenewDirty, original: originalAutorenew))
Toggle("Mail Forwarding", isOn: dirtyBinding(for: $mailforwarding, dirty: $mailforwardingDirty, original: originalMailForwarding))
Toggle("DNSSEC", isOn: dirtyBinding(for: $dnssec, dirty: $dnssecDirty, original: originalDNSSEC))
Toggle("Registrar Lock", isOn: dirtyBinding(for: $lock, dirty: $lockDirty, original: originalLock))
}
Section {
TextEditor(text: nameserversBinding)
.frame(minHeight: 120)
} header: {
Text("Nameservers")
} footer: {
Text("Enter one nameserver per line. Leave blank to use Njalla defaults.")
}
Section {
Button("Save") {
Task {
await save()
}
}
.disabled(viewModel.isSaving || !request.hasChanges)
}
}
.navigationTitle("Edit Domain")
.navigationBarTitleDisplayMode(.inline)
.overlay {
if viewModel.isSaving {
ProgressView()
.controlSize(.large)
}
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel", role: .cancel) {
dismiss()
}
.disabled(viewModel.isSaving)
}
}
.interactiveDismissDisabled(viewModel.isSaving)
.alert("Request Failed", isPresented: localErrorBinding) {
Button("OK", role: .cancel) {}
} message: {
Text(localErrorMessage ?? "")
}
}
private func save() async {
guard !viewModel.isSaving else {
return
}
let request = request
guard request.hasChanges else {
dismiss()
return
}
do {
try await viewModel.updateDomain(named: domain.name, request: request, client: client)
dismiss()
} catch is CancellationError {
return
} catch {
if (error as? URLError)?.code == .cancelled {
return
}
localErrorMessage = error.userFacingMessage
}
}
private var localErrorBinding: Binding<Bool> {
Binding(
get: { localErrorMessage != nil },
set: { newValue in
if !newValue {
localErrorMessage = nil
}
}
)
}
private var originalAutorenew: Bool {
domain.autorenew ?? false
}
private var originalMailForwarding: Bool {
domain.mailforwarding ?? false
}
private var originalDNSSEC: Bool {
domain.dnssec ?? false
}
private var originalLock: Bool {
domain.lock ?? false
}
private var originalNameservers: [String] {
normalizedNameservers(from: (domain.nameservers ?? []).joined(separator: "\n"))
}
private var request: DomainUpdateRequest {
DomainUpdateRequest(
autorenew: autorenewDirty ? autorenew : nil,
mailforwarding: mailforwardingDirty ? mailforwarding : nil,
dnssec: dnssecDirty ? dnssec : nil,
lock: lockDirty ? lock : nil,
nameservers: nameserversDirty ? normalizedNameservers(from: nameserversText) : nil
)
}
private var nameserversBinding: Binding<String> {
Binding(
get: { nameserversText },
set: { newValue in
nameserversText = newValue
nameserversDirty = normalizedNameservers(from: newValue) != originalNameservers
}
)
}
private func dirtyBinding(
for value: Binding<Bool>,
dirty: Binding<Bool>,
original: Bool
) -> Binding<Bool> {
Binding(
get: { value.wrappedValue },
set: { newValue in
value.wrappedValue = newValue
dirty.wrappedValue = newValue != original
}
)
}
private func normalizedNameservers(from text: String) -> [String] {
text
.split(whereSeparator: \.isNewline)
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
}
}
|