Add following search fix #1846

This commit is contained in:
Thomas Ricouard 2024-01-09 13:28:51 +01:00
parent 3a173a8cae
commit e7864f7089
2 changed files with 144 additions and 79 deletions

View file

@ -18,7 +18,72 @@ public struct AccountsListView: View {
}
public var body: some View {
listView
#if !os(visionOS)
.scrollContentBackground(.hidden)
.background(theme.primaryBackgroundColor)
#endif
.listStyle(.plain)
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
Text(viewModel.mode.title)
.font(.headline)
if let count = viewModel.totalCount {
Text(String(count))
.font(.footnote)
.foregroundStyle(.secondary)
}
}
}
}
.navigationTitle(viewModel.mode.title)
.navigationBarTitleDisplayMode(.inline)
.task {
viewModel.client = client
guard !didAppear else { return }
didAppear = true
await viewModel.fetch()
}
}
@ViewBuilder
private var listView: some View {
if currentAccount.account?.id == viewModel.accountId {
searchableList
} else {
standardList
}
}
private var searchableList: some View {
List {
listContent
}
.searchable(text: $viewModel.searchQuery,
placement: .navigationBarDrawer(displayMode: .always))
.task(id: viewModel.searchQuery) {
if !viewModel.searchQuery.isEmpty {
await viewModel.search()
}
}
.onChange(of: viewModel.searchQuery) { _, newValue in
if newValue.isEmpty {
Task {
await viewModel.fetch()
}
}
}
}
private var standardList: some View {
List {
listContent
}
}
@ViewBuilder
private var listContent: some View {
switch viewModel.state {
case .loading:
ForEach(Account.placeholders()) { _ in
@ -97,33 +162,6 @@ public struct AccountsListView: View {
#endif
}
}
#if !os(visionOS)
.scrollContentBackground(.hidden)
.background(theme.primaryBackgroundColor)
#endif
.listStyle(.plain)
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
Text(viewModel.mode.title)
.font(.headline)
if let count = viewModel.totalCount {
Text(String(count))
.font(.footnote)
.foregroundStyle(.secondary)
}
}
}
}
.navigationTitle(viewModel.mode.title)
.navigationBarTitleDisplayMode(.inline)
.task {
viewModel.client = client
guard !didAppear else { return }
didAppear = true
await viewModel.fetch()
}
}
private var loadingRow: some View {
HStack {

View file

@ -47,6 +47,9 @@ public enum AccountsListMode {
var state = State.loading
var totalCount: Int?
var accountId: String?
var searchQuery: String = ""
private var nextPageId: String?
@ -66,6 +69,7 @@ public enum AccountsListMode {
(accounts, link) = try await client.getWithLink(endpoint: Accounts.followers(id: accountId,
maxId: nil))
case let .following(accountId):
self.accountId = accountId
let account: Account = try await client.get(endpoint: Accounts.accounts(id: accountId))
totalCount = account.followingCount
(accounts, link) = try await client.getWithLink(endpoint: Accounts.following(id: accountId,
@ -125,4 +129,27 @@ public enum AccountsListMode {
print(error)
}
}
func search() async {
guard let client, !searchQuery.isEmpty else { return }
do {
state = .loading
try await Task.sleep(for: .milliseconds(250))
var results: SearchResults = try await client.get(endpoint: Search.search(query: searchQuery,
type: "accounts",
offset: nil,
following: true),
forceVersion: .v2)
let relationships: [Relationship] =
try await client.get(endpoint: Accounts.relationships(ids: results.accounts.map(\.id)))
results.relationships = relationships
withAnimation {
state = .display(accounts: results.accounts,
relationships: relationships,
nextPageState: .none)
}
} catch {
}
}
}