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
|
import SwiftUI
struct SettingsSectionProviderView: View {
@Environment(SettingsManager.self)
private var settings
@State private var showingCustomBooruSheet = false
@State private var newDomain: String = ""
@State private var newFlavor: BooruProviderFlavor = .danbooru
@State private var domainError: String?
var body: some View {
@Bindable var settings = settings
Form {
Section("Source") {
Picker("Website", selection: $settings.preferredBooru) {
ForEach(BooruProvider.allCases, id: \.self) { type in
Text(type.rawValue).tag(type)
}
ForEach(settings.customProviders, id: \.id) { provider in
Text(provider.domain)
.tag(BooruProvider.custom(provider))
}
}
}
if BooruProviderFlavor(provider: settings.preferredBooru) == .moebooru {
Section("Hidden Posts") {
Toggle("Show Hidden Posts", isOn: $settings.showHeldMoebooruPosts)
}
}
Section {
SecureField(
"API Key",
text: Binding(
get: { settings.providerAPIKeys[settings.preferredBooru] ?? "" },
set: { updateCredentials(apiKey: $0) }
)
)
.autocorrectionDisabled(true)
TextField(
isDanbooruProvider ? "Login" : "User ID",
text: Binding(
get: {
if isDanbooruProvider {
return settings.providerLogins[settings.preferredBooru] ?? ""
}
let userID = settings.providerUserIDs[settings.preferredBooru] ?? 0
return userID == 0 ? "" : String(userID)
},
set: { newValue in
if isDanbooruProvider {
updateCredentials(login: newValue.trimmingCharacters(in: .whitespacesAndNewlines))
return
}
let userID = Int(newValue) ?? 0
updateCredentials(userID: userID)
}
)
)
.autocorrectionDisabled(true)
#if os(iOS)
.keyboardType(isDanbooruProvider ? .default : .numberPad)
#endif
} header: {
Text("Account")
} footer: {
Text("Add credentials only if your selected source supports them.")
}
Section {
Toggle("Send User Agent", isOn: $settings.sendBooruUserAgent)
if settings.sendBooruUserAgent {
TextField("Custom User Agent", text: $settings.customBooruUserAgent)
.autocorrectionDisabled(true)
}
} header: {
Text("Advanced")
} footer: {
Text("Only change these options if a source requires them.")
}
Section {
Button("Add Source") {
showingCustomBooruSheet = true
}
.trailingFrame()
if case .custom(let provider) = settings.preferredBooru {
Button("Remove Current Source") {
settings.customProviders.removeAll { $0.id == provider.id }
settings.preferredBooru = .safebooru
}
.disabled(!settings.customProviders.contains { $0.id == provider.id })
}
Button("Remove All Custom Sources") {
if case .custom = settings.preferredBooru {
settings.preferredBooru = .safebooru
}
if !settings.customProviders.isEmpty {
settings.customProviders.removeAll()
}
}
.trailingFrame()
} header: {
Text("Custom Sources")
} footer: {
Text("Use custom sources when you want Sora to browse a compatible site.")
}
}
#if os(macOS)
.formStyle(.grouped)
#endif
.navigationTitle("Source")
#if !os(macOS)
.navigationBarTitleDisplayMode(.large)
#endif
.sheet(isPresented: $showingCustomBooruSheet) {
NavigationStack {
Form {
Section("Source Details") {
TextField("Website", text: $newDomain)
.autocorrectionDisabled(true)
Picker("Type", selection: $newFlavor) {
ForEach(BooruProviderFlavor.allCases, id: \.self) { flavor in
Text(flavor.rawValue).tag(flavor)
}
}
}
}
#if os(macOS)
.formStyle(.grouped)
#endif
.navigationTitle("Add Source")
#if !os(macOS)
.navigationBarTitleDisplayMode(.inline)
#endif
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
showingCustomBooruSheet = false
resetForm()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Add") {
if validateDomain() {
addCustomProvider()
showingCustomBooruSheet = false
}
}
.disabled(newDomain.isEmpty || domainError != nil)
}
}
}
}
.alert(
"Invalid Website",
isPresented: Binding(
get: { domainError != nil },
set: { if !$0 { domainError = nil } }
)
) {
Button("OK", role: .cancel) { () }
} message: {
if let domainError {
Text(domainError)
}
}
}
private func addCustomProvider() {
let customProvider = BooruProviderCustom(
domain: newDomain.lowercased(),
flavor: newFlavor
)
settings.customProviders.append(customProvider)
resetForm()
}
private func resetForm() {
newDomain = ""
newFlavor = .danbooru
domainError = nil
}
@discardableResult
private func validateDomain() -> Bool {
guard !newDomain.isEmpty else {
domainError = nil
return false
}
let domain = newDomain.lowercased().trimmingCharacters(in: .whitespaces)
let domainRegex =
"^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*\\.[a-z]{2,}$"
guard NSPredicate(format: "SELF MATCHES %@", domainRegex).evaluate(with: domain) else {
domainError = "Enter a valid website, such as yande.re."
return false
}
guard !domain.contains("://"), !domain.contains("/"), !domain.contains("?") else {
domainError = "Enter only the website name, without 'http://' or extra details."
return false
}
guard domain.count <= 253 else { // RFC 1035
domainError = "This website name is too long. It must be 253 characters or fewer."
return false
}
let labels = domain.split(separator: ".")
guard labels.allSatisfy({ $0.count <= 63 }) else {
domainError = "Each section of the website name must be 63 characters or fewer."
return false
}
domainError = nil
return true
}
private var isDanbooruProvider: Bool {
BooruProviderFlavor(provider: settings.preferredBooru) == .danbooru
}
private func updateCredentials(apiKey: String? = nil, userID: Int? = nil, login: String? = nil) {
var allCredentials = settings.providerCredentials
if let index = allCredentials.firstIndex(where: { $0.provider == settings.preferredBooru }) {
let credentials = allCredentials[index]
allCredentials[index] = BooruProviderCredentials(
provider: credentials.provider,
apiKey: apiKey ?? credentials.apiKey,
userID: userID ?? credentials.userID,
login: login ?? credentials.login,
id: credentials.id
)
} else {
allCredentials.append(
BooruProviderCredentials(
provider: settings.preferredBooru,
apiKey: apiKey ?? "",
userID: userID ?? 0,
login: login ?? ""
)
)
}
settings.providerCredentials = allCredentials
}
}
#Preview {
NavigationStack {
SettingsSectionProviderView()
.environment(SettingsManager())
}
}
|