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
312
313
314
315
316
317
318
319
320
321
322
323
324
import NukeUI
import SwiftUI
struct GalleryDetailView: View {
@Environment(AuthManager.self) private var auth
@State private var viewModel: GalleryDetailViewModel
@Environment(Router.self) private var router
@State private var showDeleteConfirmation = false
@State private var deleteErrorMessage: String?
@State private var showReportSheet = false
@State private var showCommentSheet = false
@State private var zoomState = ImageZoomState()
@State private var cardStoryAuthor: GrainStoryAuthor?
@Environment(\.dismiss) private var dismiss
let client: XRPCClient
let galleryUri: String
@Binding var deletedGalleryUri: String?
init(client: XRPCClient, galleryUri: String, deletedGalleryUri: Binding<String?> = .constant(nil)) {
self.client = client
_viewModel = State(initialValue: GalleryDetailViewModel(client: client))
self.galleryUri = galleryUri
_deletedGalleryUri = deletedGalleryUri
}
var body: some View {
ScrollView {
if viewModel.gallery != nil {
VStack(spacing: 0) {
GalleryCardView(gallery: Binding(
get: { viewModel.gallery! },
set: { viewModel.gallery = $0 }
), client: client, onCommentTap: {
showCommentSheet = true
}, onStoryTap: { author in
cardStoryAuthor = author
}, onReport: viewModel.gallery?.creator.did != auth.userDID ? { showReportSheet = true } : nil, onDelete: viewModel.gallery?.creator.did == auth.userDID ? { showDeleteConfirmation = true } : nil)
// View comments button
Button {
showCommentSheet = true
} label: {
HStack {
let count = viewModel.gallery?.commentCount ?? 0
if count > 0 {
Text("View all \(count) comments")
} else {
Text("Add a comment...")
}
Spacer()
}
.font(.subheadline)
.foregroundStyle(.secondary)
.padding(.horizontal, 12)
.padding(.vertical, 12)
}
.buttonStyle(.plain)
}
} else {
ProgressView()
.padding(.top, 100)
}
}
.environment(zoomState)
.modifier(ImageZoomOverlay(zoomState: zoomState))
.navigationBarTitleDisplayMode(.inline)
.toolbarTitleDisplayMode(.inline)
.alert("Delete gallery?", isPresented: $showDeleteConfirmation) {
Button("Delete", role: .destructive) {
Task { await deleteGallery() }
}
Button("Cancel", role: .cancel) {}
} message: {
Text("This will permanently delete this gallery and all its photos.")
}
.alert("Couldn't delete gallery", isPresented: Binding(
get: { deleteErrorMessage != nil },
set: {
if !$0 {
deleteErrorMessage = nil
}
}
)) {
Button("OK", role: .cancel) { deleteErrorMessage = nil }
} message: {
Text(deleteErrorMessage ?? "")
}
.sheet(isPresented: $showReportSheet) {
if let gallery = viewModel.gallery {
ReportView(client: client, subjectUri: gallery.uri, subjectCid: gallery.cid)
}
}
.fullScreenCover(item: $cardStoryAuthor) { author in
StoryViewer(
authors: [author],
client: client,
onProfileTap: { did in
cardStoryAuthor = nil
router.push(.profile(did: did))
},
onDismiss: { cardStoryAuthor = nil }
)
.environment(auth)
}
.sheet(isPresented: $showCommentSheet) {
CommentSheetView(
client: client,
galleryUri: galleryUri,
onDismiss: { showCommentSheet = false },
onProfileTap: { did in
showCommentSheet = false
router.push(.profile(did: did))
},
onHashtagTap: { tag in
showCommentSheet = false
router.push(.hashtag(tag))
},
onStoryTap: { author in
showCommentSheet = false
cardStoryAuthor = author
},
onCommentCountChanged: { count in
viewModel.gallery?.commentCount = count
}
)
}
.task {
guard !isPreview else {
#if DEBUG
viewModel.gallery = PreviewData.gallery1
viewModel.comments = PreviewData.comments
#endif
return
}
await viewModel.load(uri: galleryUri, auth: auth.authContext())
}
}
private func deleteGallery() async {
switch await GalleryService.delete(galleryUri: galleryUri, client: client, auth: auth) {
case .success:
deletedGalleryUri = galleryUri
dismiss()
case let .failure(error):
deleteErrorMessage = error.localizedDescription
}
}
}
/// CommentRow stays here since it's shared
struct CommentRow: View {
@Environment(StoryStatusCache.self) private var storyStatusCache
@Environment(ViewedStoryStorage.self) private var viewedStories
@Environment(AuthManager.self) private var auth
let comment: GrainComment
let client: XRPCClient
let userDID: String?
var isOwn: Bool = false
var isReply: Bool = false
var onProfileTap: ((String) -> Void)?
var onHashtagTap: ((String) -> Void)?
var onStoryTap: ((GrainStoryAuthor) -> Void)?
var onReply: (() -> Void)?
var onDelete: (() -> Void)?
@State private var expanded = false
@State private var favUri: String?
@State private var favCountOffset: Int = 0
@State private var isMutating = false
private var isFaved: Bool {
favUri != nil
}
private var displayFavCount: Int {
(comment.favCount ?? 0) + favCountOffset
}
var body: some View {
if comment.muted == true, !expanded {
Button {
expanded = true
} label: {
HStack(spacing: 6) {
Image(systemName: "speaker.slash")
.font(.caption)
Text("Muted comment")
.font(.caption)
}
.foregroundStyle(.secondary)
.padding(.leading, isReply ? 50 : 12)
.padding(.trailing, 12)
.padding(.vertical, 8)
}
.buttonStyle(.plain)
} else {
HStack(alignment: .top, spacing: 8) {
let avatarSize: CGFloat = isReply ? 24 : 28
StoryRingView(
hasStory: storyStatusCache.hasStory(for: comment.author.did),
viewed: comment.author.did != userDID && viewedStories.hasViewedAll(did: comment.author.did, storyStatusCache: storyStatusCache),
size: avatarSize
) {
AvatarView(url: comment.author.avatar, size: avatarSize)
}
.onTapGesture {
if let author = storyStatusCache.author(for: comment.author.did) {
onStoryTap?(author)
} else {
onProfileTap?(comment.author.did)
}
}
.onLongPressGesture {
onProfileTap?(comment.author.did)
}
VStack(alignment: .leading, spacing: 2) {
HStack(spacing: 4) {
Text(comment.author.displayName ?? comment.author.handle)
.font(.subheadline.weight(.semibold))
Text(DateFormatting.relativeTime(comment.createdAt))
.font(.caption)
.foregroundStyle(.secondary)
}
RichTextView(
text: comment.text,
facets: comment.facets,
onMentionTap: onProfileTap,
onHashtagTap: onHashtagTap
)
HStack(spacing: 16) {
if onReply != nil {
Button {
onReply?()
} label: {
Text("Reply")
.font(.caption.weight(.bold))
.foregroundStyle(.gray)
}
}
if isOwn {
Button {
onDelete?()
} label: {
Text("Delete")
.font(.caption.weight(.bold))
.foregroundStyle(.gray)
}
}
}
.padding(.top, 2)
}
Spacer(minLength: 0)
VStack(spacing: 2) {
Button {
Task { await toggleFav() }
} label: {
Image(systemName: isFaved ? "heart.fill" : "heart")
.font(.system(size: 16))
.foregroundStyle(isFaved ? Color.heart : .secondary)
}
.buttonStyle(.plain)
.disabled(isMutating)
if displayFavCount > 0 {
Text(displayFavCount.compactCount)
.font(.system(size: 11))
.foregroundStyle(.secondary)
}
}
.frame(maxHeight: .infinity)
}
.padding(.leading, isReply ? 50 : 12)
.padding(.trailing, 12)
.padding(.vertical, 8)
.task {
favUri = comment.viewer?.fav
}
}
}
private func toggleFav() async {
guard !isMutating else { return }
guard let authCtx = await auth.authContext() else { return }
isMutating = true
defer { isMutating = false }
if let uri = favUri {
// Optimistic unfavorite
favUri = nil
favCountOffset -= 1
do {
try await FavoriteService.delete(favoriteUri: uri, client: client, auth: authCtx)
} catch {
// Revert
favUri = uri
favCountOffset += 1
}
} else {
// Optimistic favorite
favUri = "pending"
favCountOffset += 1
do {
let result = try await FavoriteService.create(subject: comment.uri, client: client, auth: authCtx)
favUri = result.uri
} catch {
// Revert
favUri = nil
favCountOffset -= 1
}
}
}
}
#Preview {
GalleryDetailView(
client: .preview,
galleryUri: "at://did:plc:preview/social.grain.gallery/r1"
)
.previewEnvironments()
}