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
import SwiftUI
struct ProfileConnectionsView: View {
@Environment(AppModel.self) private var model
@State private var accounts: [Post.Author] = []
@State private var cursor: String?
@State private var usedCursors: Set<String> = []
@State private var requestPending = true
@State private var loadFailed = false
let actorID: String
let kind: ProfileConnectionKind
let openProfile: (String) -> Void
var body: some View {
let visibleAccounts = accounts.filter(model.isAccountSummaryVisible)
List {
PeopleListRows(accounts: visibleAccounts, rowIdentifierPrefix: "profileConnection", openProfile: openProfile)
Group {
if requestPending {
ProgressView("Loading accounts")
} else if loadFailed {
ContentUnavailableView {
Label("Could not load accounts", systemImage: "exclamationmark.triangle")
} actions: {
Button("Retry") { requestPending = true }
.buttonStyle(.bordered)
}
} else if cursor != nil {
Button("Load more accounts") { requestPending = true }
.buttonStyle(.bordered)
} else if visibleAccounts.isEmpty {
ContentUnavailableView(
kind == .followers ? "No followers to show" : "No accounts to show",
systemImage: "person.2")
}
}
.frame(maxWidth: .infinity)
.listRowSeparator(.hidden)
}
.listStyle(.plain)
.appNavigationTitle(kind.title)
.accessibilityIdentifier("profileConnectionsList")
.task(id: requestPending) {
if requestPending { await loadPage() }
}
}
private func loadPage() async {
defer { if !Task.isCancelled { requestPending = false } }
do {
let page = try await model.loadProfileConnections(actorID: actorID, kind: kind, cursor: cursor)
var seen = Set(accounts.map(\.id))
accounts.append(contentsOf: page.accounts.filter { seen.insert($0.id).inserted })
if let cursor { usedCursors.insert(cursor) }
cursor = page.cursor.flatMap { $0.isEmpty || usedCursors.contains($0) ? nil : $0 }
loadFailed = false
} catch is CancellationError {
return
} catch {
if !Task.isCancelled { loadFailed = true }
}
}
}