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
import SwiftUI
struct NotificationRow: View {
@AppStorage("hideAvatars") private var hideAvatars = false
@AppStorage("handlesOnly") private var handlesOnly = false
private var displayPreferences: DisplayPreferences {
DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly)
}
@Environment(AppModel.self) private var model
let state: NotificationGroupState
private var group: NotificationGroup { state.group }
let openRoute: (AppRoute) -> Void
let openComposer: (String?, String?) -> Void
let showGroup: (NotificationGroup) -> Void
@ScaledMetric(relativeTo: .subheadline) private var notificationQuoteIndent = 7
var body: some View {
let _: Void = ViewUpdateMetrics.record("NotificationRow-\(group.id)")
let notification = group.notifications[0]
VStack(spacing: 0) {
if group.notifications.count > 1 {
groupedNotification(group)
} else {
VStack(alignment: .leading, spacing: 10) {
if notification.reason == .like || notification.reason == .repost,
let relatedPost = notification.relatedPost {
let post = model.post(id: relatedPost.id) ?? relatedPost
HStack(alignment: .top, spacing: 12) {
if !displayPreferences.hideAvatars {
notificationAvatar(notification.author, notificationID: notification.id)
}
VStack(alignment: .leading, spacing: 8) {
NotificationActivityHeader(
reason: notification.reason, date: notification.indexedAt,
isRead: notification.isRead
) {
HStack(spacing: 4) {
Text(displayPreferences.name(notification.author)).lineLimit(1).truncationMode(
.tail)
Text(notification.reason.message).fixedSize()
Spacer(minLength: 0)
}
}
QuoteContextRow(
quote: Post.Quote(postID: post.id),
fallbackPost: post,
respectsHideSetting: false,
showsAvatar: false,
title: "Your post",
accessibilityID: "notificationPost-\(notification.id)",
openProfile: { openRoute(.profile($0)) },
openActor: { openRoute(.profile($0)) },
openLinkedPost: { openRoute(.post($0)) },
openPost: { openRoute(.post(post.id)) }
)
.padding(.leading, notificationQuoteIndent)
}
}
} else if notification.reason == .reply, let relatedPost = notification.relatedPost {
let reply = model.post(id: relatedPost.id) ?? relatedPost
VStack(alignment: .leading, spacing: 8) {
NotificationActivityHeader(
reason: notification.reason, date: notification.indexedAt, isRead: notification.isRead
) {
HStack(spacing: 4) {
Text(
notification.replyIsToAccount(model.account?.id)
? "Reply to your post" : "Reply in a thread"
).fixedSize()
Spacer(minLength: 0)
}
}
ReplyContextPosts(
post: reply,
posts: notification.parentPost.map { [$0] } ?? [],
avatarIdentifierPrefix: "notificationReplyAvatar",
alwaysShowContext: true,
openPost: { openRoute(.post($0)) },
openProfile: { openRoute(.profile($0)) },
openComposer: { replyTo, quotePostID in
showComposer(replyTo: replyTo, quotePostID: quotePostID)
}
)
PostRow(
post: reply,
openPost: { openRoute(.post(reply.id)) },
openProfile: { openRoute(.profile(reply.author.id)) },
openActor: { openRoute(.profile($0)) },
openQuotedPost: { openRoute(.post($0)) },
openComposer: { replyTo, quotePostID in
showComposer(replyTo: replyTo, quotePostID: quotePostID)
}
)
}
} else if notification.reason == .mention || notification.reason == .quote
|| notification.reason == .repost, let relatedPost = notification.relatedPost {
let post = model.post(id: relatedPost.id) ?? relatedPost
VStack(alignment: .leading, spacing: 10) {
NotificationActivityHeader(
reason: notification.reason, date: notification.indexedAt, isRead: notification.isRead
) {
HStack(spacing: 4) {
Text(displayPreferences.name(notification.author)).lineLimit(1).truncationMode(
.tail)
Text(notification.reason.message).fixedSize()
Spacer(minLength: 0)
}
.accessibilityIdentifier("notificationActivity-\(notification.id)")
}
PostRow(
post: post,
openPost: { openRoute(.post(post.id)) },
openProfile: { openRoute(.profile(post.author.id)) },
openActor: { openRoute(.profile($0)) },
openQuotedPost: { openRoute(.post($0)) },
openComposer: { replyTo, quotePostID in
showComposer(replyTo: replyTo, quotePostID: quotePostID)
}
)
}
} else if notification.reason == .follow {
AccountSummaryRow(
author: notification.author, bio: notification.author.bio,
avatarIdentifier: "notificationAvatar-\(notification.id)",
contentIdentifier: "notification-\(notification.id)",
openProfile: { openRoute(.profile(notification.author.id)) },
header: {
NotificationActivityHeader(
reason: notification.reason, date: notification.indexedAt,
isRead: notification.isRead
) {
HStack(spacing: 4) {
Text(displayPreferences.name(notification.author)).lineLimit(1)
.truncationMode(.tail)
Text(notification.reason.message).fixedSize()
Spacer(minLength: 0)
}
}
}
)
} else {
HStack(alignment: .top, spacing: 12) {
if !displayPreferences.hideAvatars {
notificationAvatar(notification.author, notificationID: notification.id)
}
Button {
if notification.reason.includesPost, let postID = notification.postID {
openRoute(.post(postID))
} else {
openRoute(.profile(notification.author.id))
}
} label: {
HStack(alignment: .top, spacing: 12) {
VStack(alignment: .leading, spacing: 8) {
NotificationActivityHeader(
reason: notification.reason, date: notification.indexedAt,
isRead: notification.isRead
) {
HStack(spacing: 4) {
Text(displayPreferences.name(notification.author)).lineLimit(1)
.truncationMode(.tail)
Text(notification.reason.message).fixedSize()
Spacer(minLength: 0)
}
}
if let post = notification.relatedPost {
HStack(alignment: .top, spacing: 8) {
AccountAvatar(
account: Account(
handle: post.author.handle, displayName: post.author.name,
id: post.author.id, avatarURL: post.author.avatarURL),
size: 30
)
VStack(alignment: .leading, spacing: 3) {
Text(displayPreferences.name(post.author))
.appFont(.caption, weight: .semibold)
.foregroundStyle(.primary)
Text(post.text.isEmpty ? "Photo post" : post.text)
.appFont(.subheadline)
.foregroundStyle(.secondary)
.lineLimit(3)
.multilineTextAlignment(.leading)
}
.frame(maxWidth: .infinity, alignment: .leading)
}
.padding(10)
.background(
Color(uiColor: .secondarySystemGroupedBackground),
in: RoundedRectangle(cornerRadius: 10, style: .continuous))
} else if notification.postID != nil {
Text("View post")
.appFont(.caption, weight: .medium)
.foregroundStyle(.tint)
}
}
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityHint(
notification.reason.includesPost && notification.postID != nil
? "Opens the related post" : "Opens this account profile"
)
.accessibilityIdentifier("notification-\(notification.id)")
}
}
}
}
}
.modifier(ContentListRowStyle(openPost: backgroundPostAction))
}
private var backgroundPostAction: (() -> Void)? {
guard group.notifications.count == 1,
let relatedPost = group.notifications[0].relatedPost,
model.canShowTimelineContent(model.post(id: relatedPost.id) ?? relatedPost)
else { return nil }
return { openRoute(.post(relatedPost.id)) }
}
private func showComposer(replyTo: String?, quotePostID: String?) {
openComposer(replyTo, quotePostID)
}
}
extension NotificationRow {
private func groupedNotification(_ group: NotificationGroup) -> some View {
let notifications = group.notifications
let first = notifications[0]
return HStack(alignment: .top, spacing: 12) {
if !displayPreferences.hideAvatars {
groupedNotificationAvatar(group)
}
VStack(alignment: .leading, spacing: 8) {
NotificationActivityHeader(
reason: first.reason, date: first.indexedAt, isRead: !notifications.contains(where: { !$0.isRead })
) {
Button {
showGroup(group)
} label: {
Text(first.reason == .like ? "Many liked this post" : "Many reposted this post")
.lineLimit(1)
.frame(maxWidth: .infinity, alignment: .leading)
}
.buttonStyle(.plain)
}
if let relatedPost = first.relatedPost {
let post = model.post(id: relatedPost.id) ?? relatedPost
QuoteContextRow(
quote: Post.Quote(postID: post.id), fallbackPost: post, respectsHideSetting: false,
showsAvatar: false, title: "Your post", accessibilityID: "notificationPost-\(first.id)",
openProfile: { openRoute(.profile($0)) }, openActor: { openRoute(.profile($0)) },
openLinkedPost: { openRoute(.post($0)) }, openPost: { openRoute(.post(post.id)) }
)
.padding(.leading, notificationQuoteIndent)
} else if let postID = first.postID {
Button("View post") { openRoute(.post(postID)) }
}
}
}
}
private func groupedNotificationAvatar(_ group: NotificationGroup) -> some View {
let notifications = group.notifications
let first = notifications[0]
return Button {
showGroup(group)
} label: {
ZStack {
Circle().fill(Color(uiColor: .secondarySystemFill))
.frame(width: 30, height: 30)
ForEach(Array(notifications.prefix(3).enumerated()), id: \.element.id) { index, notification in
AuthorAvatar(author: notification.author, size: 20)
.shadow(color: .black.opacity(0.2), radius: 1.5, x: 0, y: 1)
.position(
x: notifications.count == 2
? (index == 0 ? 12 : 32) : (index == 0 ? 22 : index == 1 ? 12 : 32),
y: notifications.count == 2 ? 22 : (index == 0 ? 12 : 32)
)
}
}
.frame(width: 44, height: 44)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel(
first.reason == .like ? "View people who liked this post" : "View people who reposted this post")
}
private func notificationAvatar(_ author: Post.Author, notificationID: String) -> some View {
Button {
openRoute(.profile(author.id))
} label: {
AuthorAvatar(author: author, size: 42)
.frame(minWidth: 44, minHeight: 44)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel("View \(displayPreferences.name(author))'s profile")
.accessibilityIdentifier("notificationAvatar-\(notificationID)")
}
}