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
import SwiftUI
struct PostRow: View {
@Environment(AppModel.self) private var model
@State private var revealedContent = false
let post: Post
let openPost: () -> Void
let openProfile: () -> Void
let openActor: (String) -> Void
let openQuotedPost: (String) -> Void
let openComposer: (String?, String?) -> Void
var accessibilityIdentifierPrefix = ""
var moderationContext: ModerationPolicy.Context = .contentList
var allowsPostNavigation = true
var contentInsets = EdgeInsets()
var threadPosition: ThreadPosition?
private func accessibilityIdentifier(_ identifier: String) -> String {
"\(accessibilityIdentifierPrefix)\(identifier)"
}
var body: some View {
let _: Void = ViewUpdateMetrics.record("PostRow-\(post.id)")
let state = model.postViewState(for: post)
Group {
if let decision = state.decision {
let presentation = decision.ui(moderationContext)
if decision.action == .hide {
Text("Post hidden by your content preferences")
} else if presentation.blur && (!revealedContent || presentation.noOverride) {
VStack(alignment: .leading, spacing: 8) {
Text("Content warning")
if !presentation.noOverride { Button("Show post") { revealedContent = true } }
}
} else {
postContent
}
} else {
VStack(alignment: .leading, spacing: 8) {
Text(model.contentPreferencesError ?? "Loading content preferences")
if model.contentPreferencesError != nil {
Button("Retry") { Task { await model.reloadContentPreferences() } }
Link("Manage preferences in Bluesky", destination: BlueskyLinks.moderation)
}
}
}
}
.modifier(
PostNavigationPadding(insets: contentInsets, action: allowsPostNavigation && canOpenPost ? openPost : nil)
)
.modifier(
PostNavigationTarget(
action: allowsPostNavigation && canOpenPost ? openPost : nil,
accessibilityID: accessibilityIdentifier("openPost-\(post.id)"),
label: "Open post by \(post.author.name)"
)
)
.onChange(of: post) { _, _ in resetReveals() }
.onChange(of: state.decision) { _, _ in resetReveals() }
.onChange(of: model.contentRevision) { _, _ in resetReveals() }
}
private var canOpenPost: Bool {
guard let decision = model.postViewState(for: post).decision, decision.action != .hide else { return false }
let presentation = decision.ui(moderationContext)
return !presentation.blur || (revealedContent && !presentation.noOverride)
}
private func resetReveals() {
revealedContent = false
}
private var postContent: some View {
let state = model.postViewState(for: post)
let author = model.profile(actorID: post.author.id)?.author ?? post.author
return PostContentStack(spacing: 12, openPost: allowsPostNavigation ? openPost : nil) {
PostAuthorHeader(
author: author,
postID: post.id,
date: post.createdAt,
threadPosition: threadPosition,
allowsAvatarImage: model.contentDecision(author: post.author)?.ui(.avatar).blur == false,
accessibilityID: accessibilityIdentifier("profileLink-\(post.author.id)"),
openProfile: openProfile
)
PostText(
text: post.text,
facets: post.facets,
preparedText: model.preparedPostText.renderedText(id: post.id, text: post.text, facets: post.facets),
accessibilityID: accessibilityIdentifier("postText-\(post.id)"),
openPost: openPost,
openActor: openActor,
openLinkedPost: openQuotedPost
)
PostMedia(
imageURLs: post.imageURLs,
imageThumbnailURLs: post.imageThumbnailURLs,
pendingImages: post.pendingImages,
gifURLs: post.gifURLs,
inlineGIFVideoURLs: post.inlineGIFVideoURLs,
videoURLs: post.videoURLs,
descriptions: post.imageDescriptions,
decision: state.decision,
accessibilityID: accessibilityIdentifier(post.id)
)
PostLinks(urls: post.links, openPost: openQuotedPost)
if let quote = post.quote {
QuoteContextRow(
quote: quote, fallbackPost: state.quotedPost, topPadding: 2, openActor: openActor,
openLinkedPost: openQuotedPost
) {
openQuotedPost(quote.postID)
}
}
PostActions(
post: post,
accessibilityIdentifierPrefix: accessibilityIdentifierPrefix,
openPost: allowsPostNavigation ? openPost : nil,
openComposer: openComposer
)
}
}
}
#if DEBUG
@MainActor
private enum PostRowPreviewData {
static var activePost: Post {
var post = PreviewContent.post
post.isLiked = true
post.isReposted = true
post.replyCount = 8
post.createdAt = PreviewContent.date
post.quote = Post.Quote(postID: "quoted", author: PreviewContent.author, text: "A view worth sharing.")
return post
}
}
#Preview("Post rows") {
PreviewHost { _ in
ScrollView {
VStack(alignment: .leading, spacing: 24) {
PostRow(
post: PreviewContent.post, openPost: {}, openProfile: {}, openActor: { _ in },
openQuotedPost: { _ in }, openComposer: { _, _ in })
PostDivider()
PostRow(
post: PostRowPreviewData.activePost, openPost: {}, openProfile: {}, openActor: { _ in },
openQuotedPost: { _ in }, openComposer: { _, _ in })
}
.padding()
}
}
}
#endif