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