import SwiftUI struct ContextCard: View { @AppStorage("hideAvatars") private var hideAvatars = false @AppStorage("handlesOnly") private var handlesOnly = false private var displayPreferences: DisplayPreferences { DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly) } enum Kind { case root, reply, quote var color: Color { switch self { case .root: AppPalette.root case .reply: AppPalette.reply case .quote: AppPalette.quote } } var symbol: String? { switch self { case .root: nil case .reply: "arrow.turn.down.right" case .quote: "quote.bubble" } } } let kind: Kind let title: String var author: Post.Author? var date: Date? var showsHeader = true var showsAvatar = true var headerIdentifier = "" var avatarIdentifier = "" var openProfile: (() -> Void)? let openPost: () -> Void @ViewBuilder let content: () -> Content var body: some View { PostContentStack(spacing: 8, openPost: openPost) { if showsHeader { header } content() } .padding(.leading, 12) .modifier(PostNavigationTarget(action: openPost, isAccessible: false)) .overlay(alignment: .leading) { Rectangle() .fill(kind.color) .frame(width: 3) .allowsHitTesting(false) .accessibilityHidden(true) } } private var header: some View { HStack(spacing: 8) { Button(action: openPost) { HStack(spacing: 8) { if let symbol = kind.symbol { Image(systemName: symbol) .accessibilityHidden(true) } Text(title) .lineLimit(1) .truncationMode(.tail) .frame(maxWidth: .infinity, alignment: .leading) if let date { PostTimestamp(date: date) .fixedSize() } } .appFont(.subheadline, weight: .medium) .foregroundStyle(kind.color) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityIdentifier(headerIdentifier) if showsAvatar, !displayPreferences.hideAvatars, let author { Button(action: openProfile ?? openPost) { AuthorAvatar(author: author, size: 24) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityLabel(openProfile == nil ? title : "View \(displayPreferences.name(author))'s profile") .accessibilityIdentifier(avatarIdentifier) } } } } #if DEBUG && !SHARE_EXTENSION #Preview("Context cards") { VStack(alignment: .leading, spacing: 20) { ContextCard( kind: .root, title: "Thread started by Mae", author: PreviewContent.author, date: PreviewContent.date, openPost: {}, content: { Text("The first post in a conversation.") }) ContextCard( kind: .reply, title: "Reply by Mae", author: PreviewContent.author, openPost: {}, content: { Text("A reply in the thread.") }) ContextCard( kind: .quote, title: "Quoted post", showsAvatar: false, openPost: {}, content: { Text("A quoted post.") }) } .padding() } #endif struct PostNavigationTarget: ViewModifier { var action: (() -> Void)? var accessibilityID = "" var label = "Open post" var isAccessible = true @ViewBuilder func body(content: Content) -> some View { if let action { content .contentShape(Rectangle()) .onTapGesture(perform: action) .background { if isAccessible { Button(action: action) { Color.clear.contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityLabel(label) .accessibilityIdentifier(accessibilityID) } } } else { content } } } // Give padding its own targets so nearby controls do not absorb the tap. struct PostNavigationPadding: ViewModifier { let insets: EdgeInsets var action: (() -> Void)? @ViewBuilder func body(content: Content) -> some View { if let action, insets != EdgeInsets() { VStack(spacing: 0) { if insets.top > 0 { space(height: insets.top, action: action) } HStack(spacing: 0) { if insets.leading > 0 { space(width: insets.leading, action: action) } content if insets.trailing > 0 { space(width: insets.trailing, action: action) } } if insets.bottom > 0 { space(height: insets.bottom, action: action) } } } else { content.padding(insets) } } private func space(width: CGFloat? = nil, height: CGFloat? = nil, action: @escaping () -> Void) -> some View { Color.clear .frame(width: width, height: height) .contentShape(Rectangle()) .onTapGesture(perform: action) .accessibilityHidden(true) } } // Resolve visible sections before adding a post target in each gap. struct PostContentStack: View { let spacing: CGFloat let openPost: (() -> Void)? @ViewBuilder let content: () -> Content var body: some View { if let openPost { Group(subviews: content()) { subviews in VStack(alignment: .leading, spacing: 0) { ForEach(subviews) { subview in if subview.id != subviews.first?.id { Color.clear .frame(height: spacing) .contentShape(Rectangle()) .onTapGesture(perform: openPost) .accessibilityHidden(true) } subview } } } } else { VStack(alignment: .leading, spacing: spacing, content: content) } } }