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
import SwiftUI
import UIKit
struct PostActions: View {
@Environment(AppModel.self) private var model
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@State private var showingShare = false
@State private var reportSubject: ReportSheetSubject?
@State private var timelineAction: String?
@State private var showTimelineConfirmation = false
let post: Post
let accessibilityIdentifierPrefix: String
var openPost: (() -> Void)?
let openComposer: (String?, String?) -> Void
private func accessibilityIdentifier(_ identifier: String) -> String {
"\(accessibilityIdentifierPrefix)\(identifier)"
}
var body: some View {
let state = model.postViewState(for: post)
VStack(alignment: .leading, spacing: 12) {
if state.isUncertain {
Text("Like and repost status unknown")
.appFont(.footnote)
.foregroundStyle(.secondary)
}
HStack(spacing: 0) {
actionButton(
title: state.isUncertain ? "—" : "\(post.likeCount)",
label: state.isUncertain ? "Like status unknown" : (post.isLiked ? "Unlike" : "Like"),
icon: post.isLiked && !state.isUncertain ? "heart.fill" : "heart",
tint: post.isLiked && !state.isUncertain ? AppPalette.accent : .secondary,
active: post.isLiked && !state.isUncertain
) {
Task { await model.toggleLike(postID: post.id) }
}
.accessibilityValue(
state.isUncertain ? "Unknown" : "\(post.likeCount) \(post.likeCount == 1 ? "like" : "likes")"
)
.disabled(state.isBusy || state.isUncertain)
.sensoryFeedback(.success, trigger: post.isLiked) { _, liked in liked }
postGap()
actionButton(
title: state.isUncertain ? "—" : "\(post.repostCount)",
label: state.isUncertain ? "Repost status unknown" : (post.isReposted ? "Undo repost" : "Repost"),
icon: "arrow.2.squarepath",
tint: post.isReposted && !state.isUncertain ? AppPalette.reply : .secondary,
active: post.isReposted && !state.isUncertain
) {
Task { await model.toggleRepost(postID: post.id) }
}
.accessibilityValue(
state.isUncertain
? "Unknown" : "\(post.repostCount) \(post.repostCount == 1 ? "repost" : "reposts")"
)
.disabled(state.isBusy || state.isUncertain)
.sensoryFeedback(.success, trigger: post.isReposted) { _, reposted in reposted }
postGap()
actionButton(title: "\(max(0, post.replyCount))", label: "Reply", icon: "bubble") {
openComposer(post.id, nil)
}
.accessibilityValue("\(max(0, post.replyCount)) \(post.replyCount == 1 ? "reply" : "replies")")
.accessibilityIdentifier(accessibilityIdentifier("replyButton"))
postGap()
actionButton(title: "Quote", label: "Quote", icon: "quote.bubble", showsTitle: false) {
openComposer(nil, post.id)
}
.accessibilityIdentifier(accessibilityIdentifier("quoteButton"))
postGap(flexible: true)
if model.canDelete(post) {
Button(role: .destructive) {
timelineAction = "Delete"
showTimelineConfirmation = true
} label: {
Image(systemName: "trash")
.frame(width: 44, height: 44)
.contentShape(Rectangle())
}
.disabled(state.isBusy)
.accessibilityLabel("Delete post")
.accessibilityIdentifier(accessibilityIdentifier("deletePostButton-\(post.id)"))
postGap()
}
postMenu(state: state)
}
.buttonStyle(.plain)
.foregroundStyle(.secondary)
}
.sheet(item: $reportSubject) { PostReportView(subject: $0.subject) }
.alert("Confirm action", isPresented: $showTimelineConfirmation) {
if let action = timelineAction {
Button(action, role: .destructive) {
if action == "Delete" {
Task { await model.deletePost(id: post.id) }
} else {
Task { await model.hideAccountFromTimeline(actorID: post.author.id, block: action == "Block") }
}
}
}
Button("Cancel", role: .cancel) {}
} message: {
Text(
timelineAction == "Delete"
? "Delete this post? This cannot be undone." : "\(timelineAction ?? "Change") this account?")
}
}
private func postMenu(state: PostViewState) -> some View {
Menu {
Button(post.isSaved ? "Remove from Saved Posts" : "Save post", systemImage: post.isSaved ? "bookmark.slash" : "bookmark") {
Task { await model.toggleSavedPost(postID: post.id) }
}
.disabled(state.isBusy || model.isSigningOut)
if post.shareURL != nil {
Button("Share post", systemImage: "square.and.arrow.up") { showingShare = true }
.accessibilityIdentifier(accessibilityIdentifier("shareButton-\(post.id)"))
}
if post.author.id != model.account?.id {
Menu("Moderation", systemImage: "shield") {
moderationMenu
}
}
} label: {
Image(systemName: "ellipsis").frame(width: 44, height: 44).contentShape(Rectangle())
}
.menuOrder(.fixed)
.accessibilityLabel("Post controls")
.accessibilityIdentifier(accessibilityIdentifier("postControls-\(post.id)"))
.popover(isPresented: $showingShare) {
if let url = post.shareURL {
PostShareSheet(url: url)
.presentationDetents([.medium, .large])
.presentationCompactAdaptation(.sheet)
}
}
}
private var moderationMenu: some View {
Group {
Button("Mute account") {
timelineAction = "Mute"
showTimelineConfirmation = true
}
.disabled(
model.moderationBusy.contains(post.author.id)
|| model.uncertainModerationIDs[post.author.id]?.contains(.mute) == true)
Button("Block account", role: .destructive) {
timelineAction = "Block"
showTimelineConfirmation = true
}
.disabled(
model.moderationBusy.contains(post.author.id)
|| model.uncertainModerationIDs[post.author.id]?.contains(.block) == true)
Button("Report account") { reportSubject = ReportSheetSubject(subject: .account(post.author.id)) }
Button("Report post") {
reportSubject = ReportSheetSubject(subject: .post(uri: post.id, cid: post.cid))
}
}
}
private func postGap(flexible: Bool = false) -> some View {
Color.clear
.frame(minWidth: flexible ? 8 : nil, maxWidth: flexible ? .infinity : nil)
.frame(width: flexible ? nil : 8, height: 44)
.contentShape(Rectangle())
.onTapGesture { openPost?() }
.accessibilityHidden(true)
}
private func actionButton(
title: String, label: String, icon: String, tint: Color = .secondary, active: Bool = false,
showsTitle: Bool = true, action: @escaping () -> Void
) -> some View {
Button(action: action) {
HStack(spacing: 4) {
Image(systemName: icon)
if showsTitle { Text(title) }
}
.appFont(.subheadline)
.contentTransition(reduceMotion ? .identity : .symbolEffect(.replace))
.symbolEffect(.bounce, value: reduceMotion ? false : active)
.animation(reduceMotion ? nil : .easeInOut(duration: 0.18), value: active)
.foregroundStyle(tint)
.padding(.trailing, showsTitle ? 4 : 0)
.frame(minWidth: 44, minHeight: 44, alignment: .leading)
.contentShape(Rectangle())
}
.accessibilityLabel(label)
}
}
private struct ReportSheetSubject: Identifiable {
let id = UUID()
let subject: ReportSubject
}
private struct PostShareSheet: UIViewControllerRepresentable {
@Environment(\.dismiss) private var dismiss
let url: URL
func makeUIViewController(context: Context) -> UIActivityViewController {
let controller = UIActivityViewController(activityItems: [url], applicationActivities: nil)
controller.completionWithItemsHandler = { _, _, _, _ in dismiss() }
return controller
}
func updateUIViewController(_ controller: UIActivityViewController, context: Context) {}
}
#if DEBUG
#Preview("Post actions") {
PreviewHost { _ in
PostActions(
post: PreviewContent.post, accessibilityIdentifierPrefix: "", openComposer: { _, _ in }
)
.padding()
}
}
#endif