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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import NukeUI
import PhotosUI
import SwiftUI
struct EditProfileView: View {
@Environment(AuthManager.self) private var auth
@Environment(\.dismiss) private var dismiss
let client: XRPCClient
var onSaved: (() -> Void)?
@State private var displayName = ""
@State private var bio = ""
@State private var existingAvatarURL: String?
@State private var existingAvatarBlob: [String: AnyCodable]?
@State private var selectedPhoto: PhotosPickerItem?
@State private var newAvatarData: Data?
@State private var newAvatarImage: UIImage?
@State private var removeAvatar = false
@State private var isLoading = true
@State private var isSaving = false
@State private var errorMessage: String?
@State private var mentionState = MentionAutocompleteState()
private let maxDisplayName = 64
private let maxBio = 256
var body: some View {
Form {
// Avatar section
Section {
HStack {
Spacer()
VStack(spacing: 12) {
ZStack(alignment: .bottomTrailing) {
if let newAvatarImage {
Image(uiImage: newAvatarImage)
.resizable()
.scaledToFill()
.frame(width: 100, height: 100)
.clipShape(Circle())
} else if !removeAvatar, let existingAvatarURL {
AvatarView(url: existingAvatarURL, size: 100)
} else {
ZStack {
Circle().fill(.quaternary)
Image(systemName: "person.fill")
.font(.system(size: 40))
.foregroundStyle(.tertiary)
}
.frame(width: 100, height: 100)
}
PhotosPicker(selection: $selectedPhoto, matching: .images) {
Image(systemName: "camera.fill")
.font(.system(size: 14))
.foregroundStyle(.white)
.frame(width: 32, height: 32)
.background(Color.accentColor, in: Circle())
}
.accessibilityLabel("Change profile photo")
}
if newAvatarImage != nil || (!removeAvatar && existingAvatarURL != nil) {
Button("Remove photo", role: .destructive) {
newAvatarData = nil
newAvatarImage = nil
selectedPhoto = nil
removeAvatar = true
}
.font(.caption)
}
}
Spacer()
}
.listRowBackground(Color.clear)
}
// Fields
Section {
VStack(alignment: .leading, spacing: 4) {
Text("Display name")
.font(.caption)
.foregroundStyle(.secondary)
TextField("Display name", text: $displayName)
.textInputAutocapitalization(.words)
Text("\(displayName.count)/\(maxDisplayName)")
.font(.caption2)
.foregroundStyle(displayName.count > maxDisplayName ? .red : .secondary)
.frame(maxWidth: .infinity, alignment: .trailing)
}
VStack(alignment: .leading, spacing: 4) {
Text("Bio")
.font(.caption)
.foregroundStyle(.secondary)
TextEditor(text: $bio)
.frame(minHeight: 100)
.onChange(of: bio) { mentionState.update(text: bio) }
Text("\(bio.count)/\(maxBio)")
.font(.caption2)
.foregroundStyle(bio.count > maxBio ? .red : .secondary)
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
if let errorMessage {
Section {
Text(errorMessage)
.foregroundStyle(.red)
.font(.caption)
}
}
}
.safeAreaInset(edge: .bottom) {
MentionSuggestionOverlay(state: mentionState) { suggestion in
mentionState.complete(handle: suggestion.handle, in: &bio)
}
}
.navigationTitle("Edit profile")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button {
dismiss()
} label: {
Image(systemName: "xmark")
}
.tint(.primary)
}
ToolbarItem(placement: .topBarTrailing) {
Button {
Task { await save() }
} label: {
if isSaving {
ProgressView()
} else {
Text("Save")
.fontWeight(.semibold)
}
}
.buttonStyle(.glassProminent)
.disabled(isSaving || displayName.count > maxDisplayName || bio.count > maxBio)
}
}
.onChange(of: selectedPhoto) {
Task { await loadSelectedPhoto() }
}
.task {
guard !isPreview else { return }
await loadProfile()
}
.overlay {
if isLoading {
ProgressView()
}
}
}
private func loadProfile() async {
guard let did = auth.userDID, let authContext = await auth.authContext() else { return }
do {
let profile = try await client.getActorProfile(actor: did, auth: authContext)
displayName = profile.displayName ?? ""
bio = profile.description ?? ""
existingAvatarURL = profile.avatar
// Fetch raw record to get avatar blob ref for preservation on save
let record = try await client.getRecord(uri: "at://\(did)/social.grain.actor.profile/self", auth: authContext)
if let avatar = record.value?.dictValue?["avatar"] {
existingAvatarBlob = avatar.dictValue
}
} catch {
errorMessage = "Failed to load profile"
}
isLoading = false
}
private func loadSelectedPhoto() async {
guard let selectedPhoto else { return }
do {
if let data = try await selectedPhoto.loadTransferable(type: Data.self),
let image = UIImage(data: data)
{
let resized = resizeImage(image, maxSize: 1000, maxBytes: 900_000)
newAvatarImage = UIImage(data: resized)
newAvatarData = resized
removeAvatar = false
}
} catch {
errorMessage = "Failed to load selected photo"
}
}
private func save() async {
guard let authContext = await auth.authContext() else { return }
isSaving = true
errorMessage = nil
do {
var avatarValue: AnyCodable?
if let newAvatarData {
// Upload new avatar
let response = try await client.uploadBlob(data: newAvatarData, mimeType: "image/jpeg", auth: authContext)
avatarValue = blobRefToAnyCodable(response.blob)
} else if removeAvatar {
avatarValue = nil
} else if let existingAvatarBlob {
// Preserve existing avatar
avatarValue = AnyCodable(existingAvatarBlob)
}
var record: [String: AnyCodable] = [
"createdAt": AnyCodable(DateFormatting.nowISO()),
]
let trimmedName = displayName.trimmingCharacters(in: .whitespacesAndNewlines)
if !trimmedName.isEmpty {
record["displayName"] = AnyCodable(trimmedName)
}
let trimmedBio = bio.trimmingCharacters(in: .whitespacesAndNewlines)
if !trimmedBio.isEmpty {
record["description"] = AnyCodable(trimmedBio)
}
if let avatarValue {
record["avatar"] = avatarValue
}
_ = try await client.putRecord(
collection: "social.grain.actor.profile",
rkey: "self",
record: AnyCodable(record),
auth: authContext
)
await auth.refreshAvatar()
onSaved?()
dismiss()
} catch {
errorMessage = "Failed to save: \(error.localizedDescription)"
}
isSaving = false
}
func blobRefToAnyCodable(_ blob: BlobRef) -> AnyCodable {
var dict: [String: AnyCodable] = [
"$type": AnyCodable("blob"),
]
if let mimeType = blob.mimeType {
dict["mimeType"] = AnyCodable(mimeType)
}
if let size = blob.size {
dict["size"] = AnyCodable(size)
}
if let ref = blob.ref {
dict["ref"] = AnyCodable(["$link": AnyCodable(ref.link)])
}
return AnyCodable(dict)
}
/// Shrink `image` to fit `maxSize` on its long edge and compress it under
/// `maxBytes`.
///
/// Both measurements are in pixels. `UIImage.size` is in points, and a
/// renderer left on its default scale draws at the screen's — so on a 3x
/// phone this used to hand back a 3000px JPEG for a `maxSize` of 1000, then
/// spend the whole byte budget compressing those extra pixels. The avatar
/// came out both larger and blurrier than asked for.
/// `ImageProcessing.resizeImage` works in pixels for the same reason.
func resizeImage(_ image: UIImage, maxSize: CGFloat, maxBytes: Int) -> Data {
let pixelWidth = image.size.width * image.scale
let pixelHeight = image.size.height * image.scale
let scale = min(maxSize / pixelWidth, maxSize / pixelHeight, 1.0)
let newSize = CGSize(width: round(pixelWidth * scale), height: round(pixelHeight * scale))
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let renderer = UIGraphicsImageRenderer(size: newSize, format: format)
let rendered = renderer.image { _ in
image.draw(in: CGRect(origin: .zero, size: newSize))
}
// Binary search for quality that fits under maxBytes
var low: CGFloat = 0.1
var high: CGFloat = 0.95
var result = rendered.jpegData(compressionQuality: high) ?? Data()
if result.count <= maxBytes {
return result
}
for _ in 0 ..< 8 {
let mid = (low + high) / 2
let data = rendered.jpegData(compressionQuality: mid) ?? Data()
if data.count <= maxBytes {
result = data
low = mid
} else {
high = mid
}
}
return result
}
}
#Preview {
EditProfileView(client: .preview)
.previewEnvironments()
}