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
import SwiftUI
#if DEBUG
#Preview("Search") {
PreviewHost { _ in SearchView() }
}
#endif
struct SearchView: View {
var openPadRoute: ((AppRoute) -> Void)?
@Environment(AppModel.self) private var model
@State private var query = ""
@State private var kind: AppModel.SearchKind = .accounts
@State private var ownPostsOnly = false
@State private var requestID = UUID()
@State private var path: [AppRoute] = []
@State private var composeRequest: ComposeRequest?
var body: some View {
NavigationStack(path: $path) {
content
.appNavigationTitle("Search")
.toolbar {
ToolbarItem(placement: .topBarTrailing) { filterMenu }
}
.searchable(text: $query, prompt: "Your query")
.searchPresentationToolbarBehavior(.avoidHidingContent)
.onAppear {
query = model.searchQuery
kind = model.searchKind
ownPostsOnly = model.searchOwnPostsOnly
}
.onChange(of: query) { _, _ in restart() }
.onChange(of: kind) { _, newKind in
if newKind == .accounts { ownPostsOnly = false }
restart()
}
.task(id: requestID) {
do { try await Task.sleep(for: .milliseconds(300)) } catch { return }
await model.loadSearchPage()
}
.onChange(of: model.contentPreferences != nil) { _, ready in
if ready { restart() }
}
.navigationDestination(for: AppRoute.self) { route in
AppRouteDestination(route: route, openRoute: openRoute, openComposer: showComposer)
}
}
.sheet(item: $composeRequest) { ComposerSheet(replyTo: $0.replyTo, quotePostID: $0.quotePostID) }
}
@ViewBuilder
private var content: some View {
if let error = model.contentPreferencesError {
ContentUnavailableView {
Label("Could not load content preferences", systemImage: "exclamationmark.triangle")
} description: {
Text(error)
} actions: {
Button("Retry") { Task { await model.reloadContentPreferences() } }
}
} else if model.contentPreferences == nil {
ProgressView("Loading content preferences")
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else if model.searchQuery.isEmpty {
ContentUnavailableView(
"",
systemImage: "magnifyingglass",
description: Text("Look up your moots!")
)
} else {
results
}
}
private var filterMenu: some View {
Menu {
Picker("Search for", selection: $kind) {
ForEach(AppModel.SearchKind.allCases, id: \.self) { kind in
Text(kind.rawValue).tag(kind)
}
}
.pickerStyle(.inline)
.menuActionDismissBehavior(.disabled)
if kind == .posts {
Divider()
Picker(
"Posts from",
selection: Binding(
get: { ownPostsOnly },
set: {
ownPostsOnly = $0
restart()
}
)
) {
Text("All posts").tag(false)
Text("My posts").tag(true)
}
.pickerStyle(.inline)
}
} label: {
Label(searchFilterDescription, systemImage: "line.3.horizontal.decrease")
.labelStyle(.titleAndIcon)
}
.accessibilityLabel("Filter search: \(searchFilterDescription)")
.accessibilityIdentifier("searchFilterMenu")
}
private var searchFilterDescription: String {
kind == .accounts ? "Accounts" : ownPostsOnly ? "My posts" : "Posts"
}
private var results: some View {
List {
if kind == .accounts {
PeopleListRows(accounts: model.visibleSearchAccounts, rowIdentifierPrefix: "searchAccount") {
openRoute(.profile($0))
}
} else {
ForEach(model.visibleSearchPosts) { post in
PostListItem(
post: post,
openPost: { openRoute(.post($0)) }, openProfile: { openRoute(.profile($0)) },
openComposer: showComposer
)
.listRowInsets(EdgeInsets())
.listRowSeparator(.hidden)
}
}
if let error = model.searchError {
VStack(spacing: 12) {
Text(error).foregroundStyle(.secondary)
Button("Retry") { Task { await model.loadSearchPage() } }
.accessibilityIdentifier("retrySearchButton")
}
.frame(maxWidth: .infinity)
.listRowSeparator(.hidden)
} else if model.isSearching || !model.hasSearched {
ProgressView("Searching")
.frame(maxWidth: .infinity)
.listRowSeparator(.hidden)
} else if model.searchCursor != nil {
Button("Load more results") { Task { await model.loadSearchPage() } }
.buttonStyle(.bordered)
.frame(maxWidth: .infinity)
.accessibilityIdentifier("loadMoreSearchButton")
.listRowSeparator(.hidden)
} else if (kind == .accounts && model.visibleSearchAccounts.isEmpty)
|| (kind == .posts && model.visibleSearchPosts.isEmpty) {
ContentUnavailableView(
"No results", systemImage: "magnifyingglass", description: Text("Try another search.")
)
.accessibilityIdentifier("searchEmpty")
.listRowSeparator(.hidden)
} else {
Text("End of results")
.appFont(.footnote)
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity)
.listRowSeparator(.hidden)
}
}
.listStyle(.plain)
.scrollDismissesKeyboard(.interactively)
.accessibilityIdentifier("searchResults")
}
private func restart() {
model.resetSearch(query: query, kind: kind, ownPostsOnly: ownPostsOnly)
requestID = UUID()
}
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)
}
}