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
import SwiftUI
struct SavedPostsView: View {
@Environment(AppModel.self) private var model
@Environment(\.dismiss) private var dismiss
@State private var path: [AppRoute] = []
@State private var composeRequest: ComposeRequest?
var isActive = true
var showsDoneButton = false
var openPadRoute: ((AppRoute) -> Void)?
var body: some View {
NavigationStack(path: $path) {
content
.appNavigationTitle("Saved Posts")
.toolbar {
if showsDoneButton {
ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } }
}
}
.navigationDestination(for: AppRoute.self) { route in
AppRouteDestination(route: route, openRoute: openRoute, openComposer: showComposer)
}
}
.sheet(item: $composeRequest) { ComposerSheet(replyTo: $0.replyTo, quotePostID: $0.quotePostID) }
.task(id: isActive && model.contentPreferences != nil ? model.contentRevision : nil) {
guard isActive, model.contentPreferences != nil, !model.hasLoadedSavedPosts else { return }
await model.loadSavedPosts()
}
}
@ViewBuilder
private var content: some View {
if let error = model.contentPreferencesError {
failureView("Could not load content preferences", error: error) {
Task { await model.reloadContentPreferences() }
}
} else if model.contentPreferences == nil {
ProgressView("Loading content preferences")
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
let posts = model.visibleSavedPosts
List {
ForEach(posts) { post in
PostListItem(
post: post, openPost: { openRoute(.post($0)) }, openProfile: { openRoute(.profile($0)) },
openComposer: showComposer
)
.listRowInsets(EdgeInsets())
.listRowSeparator(.hidden)
}
if let error = model.savedPostsError, !posts.isEmpty {
VStack(alignment: .leading) {
Text(error).foregroundStyle(.secondary)
Button("Retry", action: retry)
}
} else if model.savedPostsCursor != nil, model.savedPostsError == nil {
if model.isLoadingSavedPosts {
ProgressView("Loading saved posts")
} else {
Button("Load more saved posts") { Task { await model.loadSavedPosts(loadingMore: true) } }
}
}
}
.listStyle(.plain)
.refreshable { await model.loadSavedPosts() }
.overlay {
if posts.isEmpty {
if let error = model.savedPostsError {
failureView("Could not load saved posts", error: error, retry: retry)
} else if !model.hasLoadedSavedPosts {
ProgressView("Loading saved posts")
} else if model.savedPostsCursor == nil {
ContentUnavailableView(
"No saved posts", systemImage: "bookmark",
description: Text("Save a post from its three-dot menu.")
)
.allowsHitTesting(false)
}
}
}
}
}
private func failureView(_ title: String, error: String, retry: @escaping () -> Void) -> some View {
ContentUnavailableView {
Label(title, systemImage: "exclamationmark.triangle")
} description: {
Text(error)
} actions: {
Button("Retry", action: retry)
}
}
private func retry() {
Task { await model.loadSavedPosts(loadingMore: model.savedPostsFailedLoadingMore) }
}
private func openRoute(_ route: AppRoute) {
if let openPadRoute { openPadRoute(route) } else { path.append(route) }
}
private func showComposer(replyTo: String?, quotePostID: String?) {
composeRequest = ComposeRequest(replyTo: replyTo, quotePostID: quotePostID)
}
}