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
import Foundation
struct EditableProfile: Equatable, Sendable {
let accountID: String
let recordCID: String?
let avatarURL: URL?
let draft: ProfileEdit
}
struct ProfileEditResult {
let account: Account
let linkIDs: [String]
}
struct ProfileEdit: Equatable, Sendable {
enum Avatar: Equatable, Sendable {
case keep
case remove
case replace(Data)
}
struct Link: Equatable, Identifiable, Sendable {
var id = UUID()
var title: String
var address: String
let editedAt = Date()
let revision = UUID()
var label: String {
if !title.isEmpty { return title }
return URL(string: address)?.host() ?? (address.isEmpty ? "Unavailable link" : address)
}
var validationError: String? {
guard title.count <= 40, title.utf8.count <= 320 else { return "Use a shorter link title." }
guard let url = URL(string: address, encodingInvalidCharacters: false),
url.scheme?.lowercased() == "https", let host = url.host(), !host.isEmpty,
url.user() == nil, url.password() == nil
else { return "Enter an address that starts with https://." }
return nil
}
}
static let linkLimit = 10
var displayName: String
var bio: String
var links: [Link]
var avatar: Avatar = .keep
func validationError(comparedTo original: Self) -> String? {
if displayName != original.displayName, displayName.count > 64 || displayName.utf8.count > 640 {
return "Use a shorter display name."
}
if bio != original.bio, bio.count > 256 || bio.utf8.count > 2560 {
return "Use a shorter bio."
}
if links.count > Self.linkLimit, links != original.links { return "Use up to 10 profile links." }
return links.lazy.filter { !original.links.contains($0) }.compactMap(\.validationError).first
}
}
enum ProfileEditError: LocalizedError {
case changedOnServer
case invalidInput(String)
var errorDescription: String? {
switch self {
case .changedOnServer: "Your profile changed on Bluesky. Reopen the editor to load the latest version."
case .invalidInput(let message): message
}
}
}