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
import SwiftUI
struct PostDetailView: View {
@Environment(AppModel.self) private var model
@State private var completedLoad: LoadRequest?
let postID: String
let openPost: (String) -> Void
let openProfile: (String) -> Void
let openComposer: (String?, String?) -> Void
private struct LoadRequest: Equatable {
let postID: String
let contentRevision: UUID
}
var body: some View {
let request = LoadRequest(postID: postID, contentRevision: model.contentRevision)
let hasFinishedLoading = completedLoad == request
Group {
if let error = model.contentPreferencesError {
ContentUnavailableView {
Label("Could not load content preferences", systemImage: "exclamationmark.triangle")
} description: {
Text(error)
} actions: {
Button("Retry") { Task { await model.reloadContentPreferences() } }
Link("Manage preferences in Bluesky", destination: BlueskyLinks.moderation)
}
} else if let post = model.post(id: postID), model.isHidden(post.author.id) {
ContentUnavailableView {
Label("Post hidden", systemImage: "eye.slash")
} description: {
Text("Unmute and unblock this account to see its posts.")
} actions: {
Button("View account controls") { openProfile(post.author.id) }
}
.accessibilityIdentifier("hiddenPost")
} else if hasFinishedLoading, model.threadContexts[postID] != nil, let post = model.post(id: postID) {
let ancestors = model.threadAncestors(for: post)
ThreadScrollView(anchorID: postID) { height in
VStack(alignment: .leading, spacing: 0) {
VStack(alignment: .leading, spacing: 0) {
ForEach(Array(ancestors.enumerated()), id: \.element.id) { index, ancestor in
row(for: ancestor, first: index == 0, last: false)
}
}
.background(Color(uiColor: .systemBackground))
VStack(alignment: .leading, spacing: 0) {
let continuation = model.threadContinuation(after: post)
let replies = model.directReplies(to: post, inThread: postID).filter {
$0.id != continuation.first?.id
}
let sideReplyPosts = postsWithSideReplies(in: continuation)
let hasReplies = !replies.isEmpty || !sideReplyPosts.isEmpty
let showsSeparator = !continuation.isEmpty || hasReplies
row(for: post, first: true, last: true)
.padding(.bottom, showsSeparator ? 0 : 24)
.modifier(
ContentListRowStyle(contentInsets: EdgeInsets(), showsSeparator: showsSeparator))
.background(alignment: .top) { ThreadScrollAnchor(id: postID) }
if !continuation.isEmpty {
replyHeading("Thread Replies")
VStack(alignment: .leading, spacing: 0) {
ForEach(continuation) { part in
row(
for: part, first: part.id == continuation.first?.id,
last: part.id == continuation.last?.id)
}
}
.modifier(ContentListRowStyle(contentInsets: EdgeInsets()))
}
replyHeading("Replies")
if !hasReplies {
Text(continuation.isEmpty ? "No replies yet." : "No other replies yet.")
.foregroundStyle(.secondary)
.padding(.horizontal, 16)
.padding(.bottom, 12)
}
if !sideReplyPosts.isEmpty {
VStack(alignment: .leading, spacing: 0) {
ForEach(sideReplyPosts) { part in
moreReplies(
for: part, last: part.id == sideReplyPosts.last?.id,
showThreadPosition: true)
}
}
.padding(.bottom, replies.isEmpty ? 24 : 0)
.modifier(
ContentListRowStyle(contentInsets: EdgeInsets(), showsSeparator: !replies.isEmpty))
}
ForEach(replies) { reply in
let isLastReply = reply.id == replies.last?.id
let nestedReplies = model.directReplies(to: reply, inThread: postID)
let replyCount = max(max(0, reply.replyCount), nestedReplies.count)
let preview = Array(nestedReplies.prefix(10))
VStack(alignment: .leading, spacing: 0) {
let hasMore = replyCount > preview.count
row(for: reply, first: true, last: preview.isEmpty && !hasMore)
ForEach(Array(preview.enumerated()), id: \.element.id) { index, nestedReply in
let showsMore =
nestedReply.replyCount > 0
|| !model.directReplies(to: nestedReply, inThread: postID).isEmpty
let isLast = index == preview.count - 1 && !hasMore
row(for: nestedReply, first: false, last: isLast && !showsMore)
if showsMore {
moreReplies(for: nestedReply, last: isLast)
}
}
if hasMore {
moreReplies(for: reply, last: true)
}
}
.padding(.bottom, isLastReply ? 24 : 0)
.modifier(
ContentListRowStyle(contentInsets: EdgeInsets(), showsSeparator: !isLastReply))
}
}
// Keep the minimum scroll height from stretching tappable post padding.
.fixedSize(horizontal: false, vertical: true)
.frame(minHeight: height, alignment: .topLeading)
}
.background(Color(uiColor: .systemGroupedBackground))
}
} else if model.deletedPostIDs.contains(postID) {
ContentUnavailableView(
"Post deleted", systemImage: "text.bubble", description: Text("This post is no longer available."))
} else if hasFinishedLoading, model.failedPostIDs.contains(postID) {
ContentUnavailableView {
Label("Could not load post", systemImage: "text.bubble")
} description: {
Text(model.postErrors[postID] ?? "Try loading this post again.")
} actions: {
Button("Try again") { Task { await model.loadPost(id: postID) } }
}
} else if !hasFinishedLoading || model.isLoadingPost || model.threadContexts[postID] == nil {
PostLoadingPlaceholder(label: "Loading post", count: 1)
.accessibilityIdentifier("postDetailLoading")
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.background(Color(uiColor: .systemGroupedBackground))
} else {
ContentUnavailableView(
"Post not found", systemImage: "text.bubble", description: Text("This post is not available."))
}
}
.task(id: request) {
guard model.contentPreferences != nil else { return }
await model.loadPost(id: request.postID)
if !Task.isCancelled { completedLoad = request }
}
.appNavigationTitle("Post")
.toolbarBackground(.visible, for: .navigationBar)
.toolbar {
if hasFinishedLoading, let post = model.post(id: postID), model.hasEarlierPosts(for: post) {
ToolbarItem(placement: .topBarTrailing) {
Label("More above", systemImage: "arrow.up")
.appFont(.caption)
.accessibilityIdentifier("earlierPostsCue")
}
}
}
}
private func replyHeading(_ title: LocalizedStringKey) -> some View {
Text(title)
.appFont(.headline)
.accessibilityAddTraits(.isHeader)
.padding(.horizontal, 16)
.padding(.vertical, 12)
}
private func postsWithSideReplies(in posts: [Post]) -> [Post] {
posts.enumerated().compactMap { index, post in
let nextID = index + 1 < posts.count ? posts[index + 1].id : nil
let continuationCount = nextID == nil ? 0 : 1
let hasSideReplies = post.replyCount > continuationCount
|| model.directReplies(to: post, inThread: postID).contains { $0.id != nextID }
return hasSideReplies ? post : nil
}
}
private func moreReplies(for post: Post, last: Bool, showThreadPosition: Bool = false) -> some View {
Button {
openPost(post.id)
} label: {
Group {
if showThreadPosition, let position = model.threadPositions[postID]?[post.id] {
Label("View replies to post \(position.index) of \(position.count)", systemImage: "chevron.right")
} else {
Label("View more replies", systemImage: "chevron.right")
}
}
.frame(maxWidth: .infinity, minHeight: 44, alignment: .leading)
.padding(rowInsets(first: false, last: last))
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.foregroundStyle(.tint)
.accessibilityIdentifier("viewReplies-\(post.id)")
}
private func rowInsets(first: Bool, last: Bool) -> EdgeInsets {
EdgeInsets(top: first ? 20 : 12, leading: 16, bottom: last ? 16 : 12, trailing: 16)
}
private func row(for post: Post, first: Bool, last: Bool) -> some View {
PostRow(
post: post,
openPost: { if post.id != postID { openPost(post.id) } },
openProfile: { openProfile(post.author.id) },
openActor: openProfile,
openQuotedPost: openPost,
openComposer: openComposer,
moderationContext: post.id == postID ? .contentView : .contentList,
allowsPostNavigation: post.id != postID,
contentInsets: rowInsets(first: first, last: last),
threadPosition: model.threadPositions[postID]?[post.id]
)
}
}
#if DEBUG
#Preview("Post detail and thread") {
PreviewHost { _ in
NavigationStack {
PostDetailView(
postID: "thread-selected", openPost: { _ in }, openProfile: { _ in }, openComposer: { _, _ in })
}
}
}
#Preview("Post detail loading") {
PreviewHost(signedIn: false) { _ in
NavigationStack {
PostDetailView(postID: "post-1", openPost: { _ in }, openProfile: { _ in }, openComposer: { _, _ in })
}
}
}
#endif