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
import SwiftUI
struct ContextCard<Content: View>: 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<Content: View>: 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)
}
}
}