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
import SwiftUI
#if DEBUG
#Preview("Feed picker") {
PreviewHost { _ in FeedPickerView() }
}
#Preview("Add feed column") {
PreviewHost { _ in FeedPickerView(addsPadColumn: true) }
}
#endif
struct FeedPickerView: 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
@Environment(\.dismiss) private var dismiss
var addsPadColumn = false
var onSelectFeed: ((CustomFeed?) -> Void)?
var onAddPadFeed: ((CustomFeed) -> Void)?
var body: some View {
NavigationStack {
List {
if !addsPadColumn {
feedRow(name: "Following", avatarURL: nil, selected: model.selectedFeed == nil) {
choose(nil)
}
.accessibilityIdentifier("feedChoice-following")
}
ForEach(model.subscribedFeeds) { feed in
feedRow(
name: feed.name, avatarURL: feed.avatarURL,
selected: addsPadColumn
? model.padColumns.contains(where: { $0.feed?.id == feed.id })
: model.selectedFeed?.id == feed.id
) {
choose(feed)
}
.accessibilityIdentifier("feedChoice-\(feed.id)")
}
if model.isLoadingFeeds {
ProgressView("Loading your feeds")
} else if let error = model.feedsErrorMessage {
Text(error).foregroundStyle(.secondary)
Button("Retry") { Task { await model.loadSubscribedFeeds() } }
} else if model.subscribedFeeds.isEmpty {
Text("No saved custom feeds. Save a feed in Bluesky to see it here.")
.foregroundStyle(.secondary)
}
}
.appNavigationTitle("Feeds")
.toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } } }
.task { await model.loadSubscribedFeeds() }
}
}
private func choose(_ feed: CustomFeed?) {
dismiss()
if addsPadColumn {
if let feed { onAddPadFeed?(feed) }
} else if let onSelectFeed {
onSelectFeed(feed)
} else {
Task { await model.selectFeed(feed) }
}
}
private func feedRow(name: String, avatarURL: URL?, selected: Bool, action: @escaping () -> Void) -> some View {
Button(action: action) {
HStack(spacing: 12) {
if !displayPreferences.hideAvatars {
AsyncImage(url: avatarURL.map(AvatarImageURL.thumbnail)) { image in
image.resizable().scaledToFill()
} placeholder: {
Image(systemName: "rectangle.stack.fill")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.quaternary)
}
.frame(width: 40, height: 40)
.clipShape(RoundedRectangle(cornerRadius: 8))
.accessibilityHidden(true)
}
Text(name).foregroundStyle(.primary)
Spacer()
if selected { Image(systemName: "checkmark").accessibilityLabel("Selected") }
}
.contentShape(Rectangle())
}
.accessibilityAddTraits(selected ? [.isSelected] : [])
}
}
struct FeedNavigationTitle: ViewModifier {
@Environment(AppModel.self) private var model
@State private var feedIcons: [URL: UIImage] = [:]
let title: String
let feed: CustomFeed?
let showsFeedIcons: Bool
let changeAnimation: Animation?
let onSelectFeed: ((CustomFeed?) -> Void)?
func body(content: Content) -> some View {
if let onSelectFeed {
content
.navigationTitle(title)
.toolbarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .title) {
titleLabel
.accessibilityHidden(true)
.overlay {
FeedTitleMenuButton(
title: title, selection: TimelineFeedID(model.selectedFeed),
feeds: model.subscribedFeeds, icons: showsFeedIcons ? feedIcons : nil,
status: feedMenuStatus, onSelectFeed: onSelectFeed)
}
}
.sharedBackgroundVisibility(.hidden)
}
.task(id: feedIconURLs) { await loadFeedIcons(for: feedIconURLs) }
} else {
content.appNavigationTitle(title, changeAnimation: changeAnimation)
}
}
private var feedIconURLs: Set<URL> {
showsFeedIcons ? Set(model.subscribedFeeds.compactMap(\.avatarURL)) : []
}
private func loadFeedIcons(for urls: Set<URL>) async {
let avatars = model.avatars
await withTaskGroup(of: (URL, UIImage?).self) { group in
for url in urls {
group.addTask { (url, await avatars.image(for: url)) }
}
var loaded: [URL: UIImage] = [:]
for await (url, image) in group {
guard !Task.isCancelled else { return }
if let image { loaded[url] = feedMenuIcon(image) }
}
if !Task.isCancelled { feedIcons = loaded }
}
}
private func feedMenuIcon(_ image: UIImage) -> UIImage {
let size = CGSize(width: 22, height: 22)
let icon = UIGraphicsImageRenderer(size: size).image { _ in
UIBezierPath(roundedRect: CGRect(origin: .zero, size: size), cornerRadius: 5).addClip()
let scale = size.width / min(image.size.width, image.size.height)
let width = image.size.width * scale
let height = image.size.height * scale
image.draw(in: CGRect(
x: (size.width - width) / 2, y: (size.height - height) / 2, width: width, height: height))
}
return icon.withRenderingMode(.alwaysOriginal)
}
private var feedMenuStatus: String? {
if model.isLoadingFeeds { return "Loading your feeds" }
if model.feedsErrorMessage != nil { return "Could not load saved feeds. Pull to refresh." }
if model.subscribedFeeds.isEmpty { return "No saved custom feeds" }
return nil
}
private var titleLabel: some View {
HStack(spacing: 6) {
if feed?.avatarURL != nil {
FeedPickerIcon(feed: feed, size: 18, animation: changeAnimation)
}
Text(title)
.appFont(.headline)
.foregroundStyle(.primary)
.lineLimit(1)
.contentTransition(.opacity)
.layoutPriority(1)
Image(systemName: "chevron.down")
.font(.caption.weight(.semibold))
.foregroundStyle(.secondary)
}
.animation(changeAnimation, value: title)
.padding(.horizontal, 8)
.frame(minHeight: 44)
.contentShape(.rect)
}
}
private struct FeedTitleMenuButton: UIViewRepresentable {
let title: String
let selection: TimelineFeedID
let feeds: [CustomFeed]
let icons: [URL: UIImage]?
let status: String?
let onSelectFeed: (CustomFeed?) -> Void
func makeUIView(context: Context) -> UIButton {
let button = UIButton(type: .custom)
button.showsMenuAsPrimaryAction = true
button.preferredMenuElementOrder = .fixed
button.isAccessibilityElement = true
button.accessibilityLabel = "Feeds"
button.accessibilityIdentifier = "feedPickerButton"
return button
}
func updateUIView(_ button: UIButton, context: Context) {
button.accessibilityValue = title
var choices: [UIMenuElement] = [action(for: nil)]
choices.append(contentsOf: feeds.map { action(for: $0) })
if let status {
choices.append(UIMenu(options: .displayInline, children: [
UIAction(title: status, attributes: .disabled) { _ in }
]))
}
button.menu = UIMenu(children: choices)
}
func sizeThatFits(_ proposal: ProposedViewSize, uiView: UIButton, context: Context) -> CGSize? {
proposal.replacingUnspecifiedDimensions()
}
private func action(for feed: CustomFeed?) -> UIAction {
let image = icons.map { icons in
feed?.avatarURL.flatMap { icons[$0] }
?? UIImage(systemName: feed == nil ? "person.2.fill" : "rectangle.stack.fill")
} ?? nil
let action = UIAction(
title: feed?.name ?? "Following", image: image,
state: selection == TimelineFeedID(feed) ? .on : .off
) { [onSelectFeed] _ in
onSelectFeed(feed)
}
action.accessibilityIdentifier = "feedChoice-\(feed?.id ?? "following")"
return action
}
}
struct FeedPickerIcon: View {
let feed: CustomFeed?
let size: CGFloat
let animation: Animation?
var body: some View {
let id = TimelineFeedID(feed)
ZStack {
AsyncImage(url: feed?.avatarURL.map(AvatarImageURL.thumbnail)) { image in
image.resizable().scaledToFill()
} placeholder: {
Image(systemName: "rectangle.stack.fill")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.id(feed?.avatarURL)
.frame(width: size, height: size)
.clipShape(.rect(cornerRadius: size / 4))
.id(id)
.transition(.opacity)
}
.animation(animation, value: id)
.accessibilityHidden(true)
}
}