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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import NukeUI
import SwiftUI
enum FollowListMode: Hashable {
case followers
case following
case knownFollowers
/// Everyone who favorited a gallery. `did` is unused in this mode — the
/// gallery URI carries the subject.
case galleryFavorites(String)
}
/// Navigation payload for a gallery's favorites list. A distinct type keeps it
/// from colliding with the other `String?` destinations a feed view registers.
struct FavoritesDestination: Hashable, Identifiable {
let galleryUri: String
var id: String {
galleryUri
}
}
struct FollowListView: View {
@Environment(AuthManager.self) private var auth
let client: XRPCClient
/// The profile whose graph is being listed. Unused for `.galleryFavorites`.
var did: String = ""
let mode: FollowListMode
@State private var items: [FollowListItem] = []
@State private var cursor: String?
@State private var totalCount: Int?
@State private var isLoading = false
@State private var hasLoaded = false
@State private var selectedProfileDid: String?
@State private var cardStoryAuthor: GrainStoryAuthor?
@Environment(StoryStatusCache.self) private var storyStatusCache
@Environment(ViewedStoryStorage.self) private var viewedStories
private var title: String {
switch mode {
case .followers: "Followers"
case .following: "Following"
case .knownFollowers: "Followers you know"
case .galleryFavorites: "Favorited by"
}
}
private var displayCount: String {
"\(totalCount ?? items.count) \(title.lowercased())"
}
private var emptyTitle: String {
switch mode {
case .followers, .knownFollowers: "No followers"
case .following: "No following"
case .galleryFavorites: "No favorites"
}
}
private var emptyIcon: String {
switch mode {
case .followers, .knownFollowers: "person.2"
case .following: "person.badge.plus"
case .galleryFavorites: "heart"
}
}
private var emptyDescription: String {
switch mode {
case .followers: "No one is following this account yet."
case .following: "This account isn't following anyone yet."
case .knownFollowers: "None of the people you follow are following this account."
case .galleryFavorites: "No one has favorited this gallery yet."
}
}
var body: some View {
Group {
if !hasLoaded, isLoading {
ProgressView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else if hasLoaded, items.isEmpty {
ContentUnavailableView(
emptyTitle,
systemImage: emptyIcon,
description: Text(emptyDescription)
)
} else {
List {
ForEach(items) { item in
Button {
selectedProfileDid = item.did
} label: {
rowContent(item: item)
}
.buttonStyle(.plain)
.listRowSeparator(.visible)
.onAppear {
if item.id == items.last?.id {
Task { await loadMore() }
}
}
}
if isLoading {
HStack {
Spacer()
ProgressView()
Spacer()
}
}
}
.listStyle(.plain)
.refreshable {
await reload()
}
}
}
.navigationTitle(title)
.navigationDestination(item: $selectedProfileDid) { did in
ProfileView(client: client, did: did)
}
.fullScreenCover(item: $cardStoryAuthor) { author in
StoryViewer(
authors: [author],
client: client,
onProfileTap: { did in
cardStoryAuthor = nil
selectedProfileDid = did
},
onDismiss: { cardStoryAuthor = nil }
)
.environment(auth)
}
.task {
await reload()
}
.onAppear {
if hasLoaded {
Task { await reload() }
}
}
}
private func rowContent(item: FollowListItem) -> some View {
HStack(alignment: .center, spacing: 14) {
StoryRingView(hasStory: storyStatusCache.hasStory(for: item.did), viewed: item.did != auth.userDID && viewedStories.hasViewedAll(did: item.did, storyStatusCache: storyStatusCache), size: 50) {
AvatarView(url: item.avatar, size: 50)
}
.onTapGesture {
if let author = storyStatusCache.author(for: item.did) {
cardStoryAuthor = author
} else {
selectedProfileDid = item.did
}
}
.profileContextMenu(
handle: item.handle,
hasStory: storyStatusCache.hasStory(for: item.did),
onViewProfile: { selectedProfileDid = item.did },
onViewStory: {
if let author = storyStatusCache.author(for: item.did) {
cardStoryAuthor = author
}
}
)
VStack(alignment: .leading, spacing: 2) {
HStack(spacing: 4) {
if let displayName = item.displayName, !displayName.isEmpty {
Text(displayName)
.font(.body.weight(.semibold))
.lineLimit(1)
}
if let handle = item.handle {
Text("@\(handle)")
.font(.subheadline)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
if let desc = item.description, !desc.isEmpty {
Text(desc)
.font(.subheadline)
.foregroundStyle(.secondary)
.lineLimit(2)
}
}
Spacer()
}
.padding(.vertical, 4)
}
private func reload() async {
cursor = nil
do {
switch mode {
case .followers:
let response = try await client.getFollowers(actor: did, cursor: nil, auth: auth.authContext())
items = (response.items ?? []).map { FollowListItem(from: $0) }
cursor = response.cursor
totalCount = response.totalCount
case .following:
let response = try await client.getFollowing(actor: did, cursor: nil, auth: auth.authContext())
items = (response.items ?? []).map { FollowListItem(from: $0) }
cursor = response.cursor
totalCount = response.totalCount
case .knownFollowers:
if auth.userDID != nil {
let response = try await client.getKnownFollowers(actor: did, auth: auth.authContext())
items = (response.items ?? []).map { FollowListItem(from: $0) }
totalCount = items.count
}
case let .galleryFavorites(uri):
let response = try await client.getGalleryFavorites(gallery: uri, cursor: nil, auth: auth.authContext())
items = (response.items ?? []).map { FollowListItem(from: $0) }
cursor = response.cursor
totalCount = response.totalCount
}
} catch {
// keep existing items on error
}
hasLoaded = true
}
private func loadMore() async {
guard !isLoading else { return }
if !items.isEmpty, cursor == nil {
return
}
isLoading = true
defer { isLoading = false }
do {
let existingIDs = Set(items.map(\.id))
switch mode {
case .followers:
let response = try await client.getFollowers(actor: did, cursor: cursor, auth: auth.authContext())
if let newItems = response.items {
let filtered = newItems.filter { !existingIDs.contains($0.did) }
items.append(contentsOf: filtered.map { FollowListItem(from: $0) })
}
cursor = response.cursor
case .following:
let response = try await client.getFollowing(actor: did, cursor: cursor, auth: auth.authContext())
if let newItems = response.items {
let filtered = newItems.filter { !existingIDs.contains($0.did) }
items.append(contentsOf: filtered.map { FollowListItem(from: $0) })
}
cursor = response.cursor
case .knownFollowers:
break // No pagination for known followers
case let .galleryFavorites(uri):
let response = try await client.getGalleryFavorites(gallery: uri, cursor: cursor, auth: auth.authContext())
if let newItems = response.items {
let filtered = newItems.filter { !existingIDs.contains($0.did) }
items.append(contentsOf: filtered.map { FollowListItem(from: $0) })
}
cursor = response.cursor
}
} catch {
// silently fail
}
}
private func toggleFollow(for targetDid: String) async {
guard let authContext = await auth.authContext(), let repo = auth.userDID else { return }
guard let index = items.firstIndex(where: { $0.did == targetDid }) else { return }
let item = items[index]
if let followUri = item.followingUri, followUri != "pending" {
let rkey = followUri.split(separator: "/").last.map(String.init) ?? ""
items[index].followingUri = nil
do {
try await client.deleteRecord(collection: "social.grain.graph.follow", rkey: rkey, auth: authContext)
if mode == .following, did == repo {
if let idx = items.firstIndex(where: { $0.did == targetDid }) {
items.remove(at: idx)
}
if let count = totalCount {
totalCount = max(0, count - 1)
}
}
} catch {
if let idx = items.firstIndex(where: { $0.did == targetDid }) {
items[idx].followingUri = followUri
}
}
} else if item.followingUri == nil {
items[index].followingUri = "pending"
do {
let record = AnyCodable([
"subject": item.did,
"createdAt": DateFormatting.nowISO(),
])
let result = try await client.createRecord(
collection: "social.grain.graph.follow",
repo: repo,
record: record,
auth: authContext
)
if let idx = items.firstIndex(where: { $0.did == targetDid }) {
items[idx].followingUri = result.uri
}
} catch {
if let idx = items.firstIndex(where: { $0.did == targetDid }) {
items[idx].followingUri = nil
}
}
}
}
}
struct FollowListItem: Identifiable {
let did: String
var handle: String?
var displayName: String?
var description: String?
var avatar: String?
var followingUri: String?
var id: String {
did
}
init(from follower: FollowerItem) {
did = follower.did
handle = follower.handle
displayName = follower.displayName
description = follower.description
avatar = follower.avatar
followingUri = follower.viewer?.following
}
init(from favorite: FavoriteItem) {
did = favorite.did
handle = favorite.handle
displayName = favorite.displayName
description = favorite.description
avatar = favorite.avatar
followingUri = favorite.viewer?.following
}
init(from following: FollowingItem) {
did = following.did
handle = following.handle
displayName = following.displayName
description = following.description
avatar = following.avatar
followingUri = following.viewer?.following
}
}
#Preview {
NavigationStack {
FollowListView(
client: .preview,
did: "did:plc:preview",
mode: .followers
)
}
.previewEnvironments()
.preferredColorScheme(.dark)
.tint(Color.accentColor)
.frame(maxHeight: .infinity, alignment: .top)
}