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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
import Nuke
import os
import SwiftUI
private let logger = Logger(subsystem: "social.grain.grain", category: "GalleryCard")
// MARK: - Self-animating heart with heap-backed state
@Observable
@MainActor
final class HeartAnimationState: Identifiable {
let id = UUID()
let position: CGPoint
let rotation: Double
var heartScale: CGFloat = 0
var ripple1Scale: CGFloat = 0.3
var ripple1Opacity: Double = 0
var ripple2Scale: CGFloat = 0.3
var ripple2Opacity: Double = 0
var ripple3Scale: CGFloat = 0.3
var ripple3Opacity: Double = 0
var isComplete = false
init(position: CGPoint) {
self.position = position
rotation = Double.random(in: -20 ... 20)
}
func start() {
withAnimation(.spring(response: 0.25, dampingFraction: 0.5)) {
heartScale = 1
}
ripple1Opacity = 0.6
withAnimation(.easeOut(duration: 0.35)) { ripple1Scale = 1.5 }
withAnimation(.easeIn(duration: 0.3).delay(0.2)) { ripple1Opacity = 0 }
ripple2Opacity = 0.4
withAnimation(.easeOut(duration: 0.4).delay(0.05)) { ripple2Scale = 2.0 }
withAnimation(.easeIn(duration: 0.3).delay(0.25)) { ripple2Opacity = 0 }
ripple3Opacity = 0.25
withAnimation(.easeOut(duration: 0.5).delay(0.1)) { ripple3Scale = 2.6 }
withAnimation(.easeIn(duration: 0.35).delay(0.3)) { ripple3Opacity = 0 }
Task {
try? await Task.sleep(for: .milliseconds(500))
withAnimation(.easeInOut(duration: 0.2)) { heartScale = 1.6 }
try? await Task.sleep(for: .milliseconds(400))
isComplete = true
}
}
}
struct DoubleTapHeartView: View {
let state: HeartAnimationState
var body: some View {
ZStack {
Image(systemName: "heart")
.font(.system(size: 80, weight: .light))
.foregroundStyle(Color.heart)
.scaleEffect(state.ripple3Scale)
.opacity(state.ripple3Opacity)
.rotationEffect(.degrees(state.rotation * 0.6))
Image(systemName: "heart")
.font(.system(size: 80, weight: .ultraLight))
.foregroundStyle(Color.heart)
.scaleEffect(state.ripple2Scale)
.opacity(state.ripple2Opacity)
.rotationEffect(.degrees(state.rotation * 0.8))
Image(systemName: "heart")
.font(.system(size: 80, weight: .thin))
.foregroundStyle(Color.heart)
.scaleEffect(state.ripple1Scale)
.opacity(state.ripple1Opacity)
.rotationEffect(.degrees(state.rotation * 0.9))
Image(systemName: "heart.fill")
.font(.system(size: 80))
.foregroundStyle(Color.heart)
.shadow(color: .pink.opacity(0.4), radius: 12)
.scaleEffect(state.heartScale)
.opacity(state.heartScale > 1.2 ? 0 : 1)
.rotationEffect(.degrees(state.rotation))
}
.position(x: state.position.x, y: state.position.y)
.allowsHitTesting(false)
.accessibilityHidden(true)
.onAppear { state.start() }
}
}
// MARK: - Subtle like-button particle burst
struct LikeParticleView: View {
let index: Int
/// Deterministic per slot — no random state stored in parent
private static let configs: [(x: CGFloat, y: CGFloat, scale: CGFloat)] = [
(x: -15, y: -30, scale: 0.85),
(x: -4, y: -38, scale: 1.00),
(x: 7, y: -32, scale: 0.90),
(x: 17, y: -26, scale: 0.80),
(x: 2, y: -43, scale: 0.95),
]
@State private var scale: CGFloat = 0.3
@State private var offset: CGSize = .zero
@State private var opacity: Double = 0.9
var body: some View {
let cfg = Self.configs[index]
Image(systemName: "heart.fill")
.font(.system(size: 10))
.foregroundStyle(Color.heart.opacity(0.9))
.scaleEffect(scale)
.offset(offset)
.opacity(opacity)
.onAppear {
let delay = Double(index) * 0.07
withAnimation(.easeOut(duration: 0.55).delay(delay)) {
scale = cfg.scale
offset = CGSize(width: cfg.x, height: cfg.y)
}
withAnimation(.easeIn(duration: 0.38).delay(delay + 0.32)) {
opacity = 0
}
}
}
}
struct PageIndicatorView: View {
let photos: [GrainPhoto]
let currentPage: Int
let hasPortrait: Bool
var body: some View {
if photos.count > 1 {
HStack(spacing: 5) {
let total = photos.count
let maxVisible = 5
let start = total <= maxVisible ? 0 : min(max(currentPage - 2, 0), total - maxVisible)
let end = total <= maxVisible ? total : start + maxVisible
ForEach(start ..< end, id: \.self) { index in
let distance = abs(index - currentPage)
let currentIsLandscape = photos[currentPage].aspectRatio.ratio >= 1
let dotColor: Color = hasPortrait && currentIsLandscape ? .secondary : .white
Circle()
.fill(dotColor.opacity(index == currentPage ? 1.0 : distance == 1 ? 0.5 : distance == 2 ? 0.3 : 0.2))
.frame(
width: distance <= 1 ? 6 : distance == 2 ? 4 : 3,
height: distance <= 1 ? 6 : distance == 2 ? 4 : 3
)
.animation(.easeInOut(duration: 0.2), value: currentPage)
}
}
.padding(.vertical, 8)
}
}
}
struct CopiedToastView: View {
var body: some View {
HStack(spacing: 6) {
Image(systemName: "doc.on.doc.fill")
.font(.caption)
Text("Link copied")
.font(.subheadline.weight(.medium))
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(.ultraThinMaterial, in: Capsule())
.shadow(color: .black.opacity(0.15), radius: 8, y: 4)
.transition(.scale.combined(with: .opacity))
}
}
private extension View {
/// Overlays a "Link copied" toast and drives its entrance/exit animation.
/// Bundles the overlay and animation so they can't be accidentally separated.
func copiedToast(isShowing: Bool) -> some View {
overlay {
if isShowing {
CopiedToastView()
}
}
.animation(.spring(response: 0.4, dampingFraction: 0.7), value: isShowing)
}
}
struct GalleryCardView: View {
@Environment(AuthManager.self) private var auth
@Environment(StoryStatusCache.self) private var storyStatusCache
@Environment(ViewedStoryStorage.self) private var viewedStories
@Environment(LabelDefinitionsCache.self) private var labelDefsCache
@Binding var gallery: GrainGallery
let client: XRPCClient
@Environment(Router.self) private var router
var onCommentTap: (() -> Void)?
var onStoryTap: ((GrainStoryAuthor) -> Void)?
var onReport: (() -> Void)?
var onDelete: (() -> Void)?
/// Whether the location line navigates.
///
/// False on `LocationFeedView`, where the destination is the screen you are
/// already on. This used to be expressed by omitting `onLocationTap`, which
/// left the reason implicit in the absence of an argument.
var showsLocationLink = true
@State private var isFavoriting = false
@State private var showCardActions = false
@State private var likeParticleBursts: [UUID] = []
@State private var currentPage = 0
@State private var showingAlt = false
@State private var hearts: [HeartAnimationState] = []
@State private var showCopiedToast = false
@State private var shareAnimating = false
@State private var prefetcher = ImagePrefetcher()
private var isFavorited: Bool {
gallery.viewer?.fav != nil
}
private var labelResult: LabelResolution {
resolveLabels(gallery.labels, definitions: labelDefsCache.definitions)
}
private var galleryShareURL: URL {
let rkey = gallery.uri.split(separator: "/").last.map(String.init) ?? ""
return URL(string: "https://grain.social/profile/\(gallery.creator.did)/gallery/\(rkey)") ?? URL(string: "https://grain.social")!
}
var body: some View {
let lr = labelResult
if lr.action == .hide || lr.action == .warnContent, !gallery.labelRevealed {
VStack(spacing: 0) {
ContentWarningOverlay(name: lr.name, action: lr.action) {
gallery.labelRevealed = true
}
.frame(height: 200)
}
} else {
cardContent(lr: lr)
}
}
private func cardContent(lr: LabelResolution) -> some View {
VStack(alignment: .leading, spacing: 0) {
cardHeader
if let photos = gallery.items, !photos.isEmpty {
photoCarousel(photos: photos, lr: lr)
}
engagementRow
favedByRow
captionSection(lr: lr)
}
.copiedToast(isShowing: showCopiedToast)
}
private var cardHeader: some View {
HStack(spacing: 8) {
let hasStory = storyStatusCache.hasStory(for: gallery.creator.did)
let allViewed = gallery.creator.did != auth.userDID && viewedStories.hasViewedAll(did: gallery.creator.did, storyStatusCache: storyStatusCache)
StoryRingView(hasStory: hasStory, viewed: allViewed, size: 32) {
AvatarView(url: gallery.creator.avatar, size: 32)
}
.onTapGesture {
if let author = storyStatusCache.author(for: gallery.creator.did) {
onStoryTap?(author)
} else {
router.push(.profile(did: gallery.creator.did))
}
}
.onLongPressGesture {
router.push(.profile(did: gallery.creator.did))
}
VStack(alignment: .leading, spacing: 3) {
HStack(spacing: 4) {
Text(gallery.creator.displayName ?? gallery.creator.handle)
.font(.subheadline.weight(.semibold))
.lineLimit(1)
Text("@\(gallery.creator.handle)")
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
Text("· \(DateFormatting.relativeTime(gallery.createdAt ?? gallery.indexedAt))")
.font(.caption)
.foregroundStyle(.secondary)
.fixedSize()
}
if let location = gallery.location,
let locationName = gallery.locationDisplay ?? location.name ?? gallery.address?.locality
{
Text(locationName)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
.onTapGesture {
guard showsLocationLink else { return }
router.push(.location(h3Index: location.value, name: locationName))
}
}
}
Spacer()
if onReport != nil || onDelete != nil {
Button { showCardActions = true } label: {
Image(systemName: "ellipsis")
.font(.system(size: 14, weight: .medium))
.foregroundStyle(.primary)
.frame(width: 44, height: 44)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel("More options")
.highPriorityGesture(TapGesture().onEnded { showCardActions = true })
}
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.contentShape(Rectangle())
.onTapGesture { router.push(.profile(did: gallery.creator.did)) }
.sheet(isPresented: $showCardActions) {
GalleryActionsSheet(onReport: onReport, onDelete: onDelete)
.presentationDetents([.height(200)])
}
}
}
// MARK: - Card sections
extension GalleryCardView {
@ViewBuilder
private func photoCarousel(photos: [GrainPhoto], lr: LabelResolution) -> some View {
let hasPortrait = photos.contains { $0.aspectRatio.ratio < 1 }
let hasMixedRatios = Set(photos.map { Int($0.aspectRatio.ratio * 100) }).count > 1
let carouselRatio = hasMixedRatios
? max(photos.map(\.aspectRatio.ratio).min() ?? 1, 0.56)
: photos[currentPage].aspectRatio.ratio
GeometryReader { geo in
let height = geo.size.width / carouselRatio
ZStack(alignment: .bottom) {
TabView(selection: $currentPage) {
ForEach(Array(photos.enumerated()), id: \.element.id) { index, photo in
ZStack {
ZoomableImage(
url: photo.fullsize,
thumbURL: photo.thumb,
aspectRatio: photo.aspectRatio.ratio,
onDoubleTap: { point in doubleTapLike(at: point) }
)
// Alt text overlay — always in the tree so opacity
// animates smoothly (conditional `if` inside TabView
// swallows transitions).
if let alt = photo.alt, !alt.isEmpty {
ZStack {
Color.black.opacity(0.6)
.onTapGesture {
withAnimation(.easeInOut(duration: 0.2)) { showingAlt = false }
}
GeometryReader { geo in
ScrollView {
Text(alt)
.font(.subheadline)
.foregroundStyle(.white)
.multilineTextAlignment(.center)
.padding(20)
.frame(maxWidth: .infinity)
.frame(minHeight: geo.size.height)
}
.scrollBounceBehavior(.basedOnSize)
}
}
.opacity(showingAlt && currentPage == index ? 1 : 0)
.allowsHitTesting(showingAlt && currentPage == index)
.animation(.easeInOut(duration: 0.2), value: showingAlt && currentPage == index)
}
}
.tag(index)
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
.overlay {
if lr.action == .warnMedia, !gallery.labelRevealed {
Rectangle().fill(Color(.secondarySystemBackground))
}
}
.allowsHitTesting(lr.action != .warnMedia || gallery.labelRevealed)
PageIndicatorView(photos: photos, currentPage: currentPage, hasPortrait: hasPortrait)
altButton(photos: photos)
// Double-tap heart animations
ForEach(hearts) { heart in
DoubleTapHeartView(state: heart)
.onChange(of: heart.isComplete) {
hearts.removeAll { $0.isComplete }
}
}
// Media warning overlay
if lr.action == .warnMedia, !gallery.labelRevealed {
MediaWarningOverlay(name: lr.name) {
withAnimation { gallery.labelRevealed = true }
}
}
}
.contentShape(Rectangle())
.onTapGesture {
let photo = currentPage < photos.count ? photos[currentPage] : nil
if let alt = photo?.alt, !alt.isEmpty {
withAnimation(.easeInOut(duration: 0.2)) { showingAlt.toggle() }
}
}
.frame(height: height)
}
.aspectRatio(carouselRatio, contentMode: .fit)
.onAppear {
prefetchCarousel(photos: photos, page: 0)
}
.onChange(of: currentPage) {
withAnimation(.easeInOut(duration: 0.2)) { showingAlt = false }
prefetchCarousel(photos: photos, page: currentPage)
}
.onDisappear {
prefetcher.stopPrefetching()
}
}
@ViewBuilder
private func altButton(photos: [GrainPhoto]) -> some View {
if let alt = photos[currentPage].alt, !alt.isEmpty {
VStack {
Spacer()
HStack {
Spacer()
Button {
withAnimation(.easeInOut(duration: 0.2)) {
showingAlt.toggle()
}
} label: {
Text("ALT")
.font(.caption2.weight(.bold))
.padding(.horizontal, 6)
.padding(.vertical, 3)
.background(.black.opacity(0.6), in: RoundedRectangle(cornerRadius: 4))
.foregroundStyle(.white)
}
.accessibilityLabel(showingAlt ? "Hide alt text" : "Show alt text")
}
.padding(8)
}
}
}
/// Count text sized to a fixed-width digit proxy so the row doesn't shift
/// when the tally changes.
private var favCountLabel: some View {
ZStack {
let count = gallery.favCount ?? 0
Text(count.compactCount.digitWidthProxy)
.hidden()
Text(count.compactCount)
}
}
private var engagementRow: some View {
HStack(spacing: 16) {
// Heart toggles; the count next to it opens the favorited-by list.
// They're separate hit targets so tapping the tally can't like.
HStack(spacing: 5) {
Button {
guard !isFavoriting else { return }
if !isFavorited {
addParticleBurst()
}
triggerFavoriteToggle()
} label: {
Image(systemName: isFavorited ? "heart.fill" : "heart")
.font(.system(size: 22))
.contentTransition(.symbolEffect(.replace.downUp.byLayer, options: .nonRepeating))
.animation(.spring(response: 0.3, dampingFraction: 0.6), value: isFavorited)
// The count used to be part of this button. Grow the hit
// target back to what it was, then negatively pad so the
// row lays out exactly as before.
.frame(width: 32, height: 32)
.contentShape(Rectangle())
.padding(.horizontal, -4)
.padding(.vertical, -5)
}
.accessibilityLabel(isFavorited ? "Remove favorite" : "Favorite")
.accessibilityValue("\(gallery.favCount ?? 0) favorites")
.overlay(alignment: .leading) {
ZStack {
ForEach(likeParticleBursts, id: \.self) { _ in
ForEach(0 ..< 5, id: \.self) { i in
LikeParticleView(index: i)
}
}
}
.offset(x: 11)
.allowsHitTesting(false)
.accessibilityHidden(true)
}
if (gallery.favCount ?? 0) > 0 {
Button { router.push(.galleryFavorites(uri: gallery.uri)) } label: {
favCountLabel
}
.accessibilityLabel("\(gallery.favCount ?? 0) favorites")
.accessibilityHint("Shows everyone who favorited this gallery")
} else {
favCountLabel
}
}
.foregroundStyle(isFavorited ? AnyShapeStyle(Color.heart) : AnyShapeStyle(.secondary))
Button {
onCommentTap?()
} label: {
HStack(spacing: 5) {
Image(systemName: "bubble")
.font(.system(size: 20))
Text((gallery.commentCount ?? 0).compactCount)
}
}
.foregroundStyle(.secondary)
.accessibilityLabel("Comments")
.accessibilityValue("\(gallery.commentCount ?? 0)")
ShareLink(item: galleryShareURL) {
Image(systemName: "paperplane")
.font(.system(size: 20))
.rotationEffect(.degrees(shareAnimating ? -15 : 0))
.animation(
shareAnimating
? .easeInOut(duration: 0.08).repeatCount(5, autoreverses: true)
: .default,
value: shareAnimating
)
}
.foregroundStyle(.secondary)
.accessibilityLabel("Share gallery")
.disabled(shareAnimating)
.simultaneousGesture(
LongPressGesture(minimumDuration: 0.5)
.onEnded { _ in
UIPasteboard.general.url = galleryShareURL
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
shareAnimating = true
showCopiedToast = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
shareAnimating = false
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
showCopiedToast = false
}
}
)
Spacer()
}
.font(.subheadline)
.animation(.spring(response: 0.3, dampingFraction: 0.7), value: gallery.favCount)
.padding(.horizontal, 12)
.padding(.top, 12)
.padding(.bottom, 4)
}
/// "Favorited by <people you follow>" facepile. The server caps the list,
/// so past two names we say "and others" rather than a count we don't have.
@ViewBuilder
private var favedByRow: some View {
let people = gallery.favedByFollowing ?? []
if !people.isEmpty {
let names = people.prefix(2).map { person -> String in
if let name = person.displayName, !name.isEmpty {
return name
}
return person.handle
}
HStack(spacing: 6) {
FacepileView(people: people, size: 20)
Group {
if names.count == 1 {
Text("Favorited by **\(names[0])**")
} else if people.count > 2 {
Text("Favorited by **\(names[0])**, **\(names[1])** and others you follow")
} else {
Text("Favorited by **\(names[0])** and **\(names[1])**")
}
}
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(2)
Spacer(minLength: 0)
}
.padding(.horizontal, 12)
.padding(.top, 6)
.contentShape(Rectangle())
.onTapGesture { router.push(.galleryFavorites(uri: gallery.uri)) }
.accessibilityElement(children: .combine)
.accessibilityAddTraits(.isButton)
}
}
@ViewBuilder
private func captionSection(lr: LabelResolution) -> some View {
// EXIF info — collapses when the current photo has no metadata,
// expands smoothly when swiping to a photo that does.
let allPhotos = gallery.items ?? []
if allPhotos.contains(where: { $0.exif?.hasDisplayableData ?? false }) {
let currentExif = allPhotos.indices.contains(currentPage)
? allPhotos[currentPage].exif : nil
let hasCurrentExif = currentExif?.hasDisplayableData ?? false
ExifInfoView(
exif: currentExif?.displayData,
reserveCameraRow: allPhotos.contains(where: { $0.exif?.cameraName != nil }),
reserveLensRow: allPhotos.contains(where: { $0.exif?.lensName != nil })
)
.transaction { $0.animation = nil }
.padding(.horizontal, 12)
.padding(.top, 8)
.frame(maxHeight: hasCurrentExif ? nil : 0, alignment: .top)
.clipped()
.animation(.easeInOut(duration: 0.2), value: hasCurrentExif)
}
// Title & description
VStack(alignment: .leading, spacing: 4) {
Text(gallery.title ?? "")
.font(.subheadline.weight(.semibold))
.lineLimit(3)
if let description = gallery.description, !description.isEmpty {
ExpandableDescriptionView(
text: description,
onMentionTap: { router.push(.profile(did: $0)) },
onHashtagTap: { router.push(.hashtag($0)) }
)
}
if lr.action == .badge {
LabelBadge(name: lr.name)
}
}
.padding(.horizontal, 12)
.padding(.top, 8)
.padding(.bottom, 16)
}
private func addParticleBurst() {
let id = UUID()
likeParticleBursts.append(id)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.3) {
likeParticleBursts.removeAll { $0 == id }
}
}
private func prefetchCarousel(photos: [GrainPhoto], page: Int) {
let input = photos.map { (thumb: $0.thumb, fullsize: $0.fullsize) }
let plan = ImagePrefetchPlanning.carouselPrefetchRequests(photos: input, currentPage: page)
prefetcher.startPrefetching(with: plan.all)
}
private func doubleTapLike(at point: CGPoint) {
hearts.append(HeartAnimationState(position: point))
addParticleBurst()
guard !isFavorited, !isFavoriting else { return }
triggerFavoriteToggle()
}
private func triggerFavoriteToggle() {
isFavoriting = true
Task {
await toggleFavorite()
isFavoriting = false
}
}
private func toggleFavorite() async {
guard let authContext = await auth.authContext() else {
logger.error("No auth context")
return
}
if let favUri = gallery.viewer?.fav {
gallery.viewer?.fav = nil
gallery.favCount = max((gallery.favCount ?? 1) - 1, 0)
do {
try await FavoriteService.delete(favoriteUri: favUri, client: client, auth: authContext)
} catch {
logger.error("Unfavorite failed: \(error)")
gallery.viewer?.fav = favUri
gallery.favCount = (gallery.favCount ?? 0) + 1
}
} else {
let prevViewer = gallery.viewer
let prevCount = gallery.favCount
gallery.viewer = GalleryViewerState(fav: "pending")
gallery.favCount = (gallery.favCount ?? 0) + 1
do {
let response = try await FavoriteService.create(subject: gallery.uri, client: client, auth: authContext)
gallery.viewer = GalleryViewerState(fav: response.uri)
} catch {
logger.error("Favorite failed: \(error)")
gallery.viewer = prevViewer
gallery.favCount = prevCount
}
}
}
}
#Preview {
@Previewable @State var gallery = PreviewData.gallery1
ScrollView {
GalleryCardView(
gallery: $gallery,
client: .preview
)
GalleryCardView(
gallery: .constant(PreviewData.gallery2),
client: .preview
)
}
.previewEnvironments()
.preferredColorScheme(.dark)
.tint(Color.accentColor)
.frame(maxHeight: .infinity, alignment: .top)
}
struct GalleryActionsSheet: View {
@Environment(\.dismiss) private var dismiss
var onReport: (() -> Void)?
var onDelete: (() -> Void)?
var body: some View {
List {
if let onReport {
Button {
dismiss()
onReport()
} label: {
Label("Report", systemImage: "flag")
.foregroundStyle(.primary)
}
}
if let onDelete {
Button {
dismiss()
onDelete()
} label: {
Label("Delete gallery", systemImage: "trash")
.foregroundStyle(.red)
}
}
}
.tint(.primary)
}
}