Faster trending / fixed search

This commit is contained in:
Thomas Ricouard 2023-01-10 21:22:05 +01:00
parent a77ee6b7af
commit 77ad9afb85

View file

@ -46,12 +46,10 @@ class ExploreViewModel: ObservableObject {
suggestedToken = [.user, .statuses] suggestedToken = [.user, .statuses]
} else if searchQuery.starts(with: "#") { } else if searchQuery.starts(with: "#") {
suggestedToken = [.tag] suggestedToken = [.tag]
} else if !tokens.isEmpty {
suggestedToken = []
search()
} else { } else {
search() suggestedToken = []
} }
search()
} }
} }
@Published var results: [String: SearchResults] = [:] @Published var results: [String: SearchResults] = [:]
@ -67,15 +65,11 @@ class ExploreViewModel: ObservableObject {
func fetchTrending() async { func fetchTrending() async {
guard let client else { return } guard let client else { return }
do { do {
async let suggestedAccounts: [Account] = client.get(endpoint: Accounts.suggestions) let data = try await fetchTrendingsData(client: client)
async let trendingTags: [Tag] = client.get(endpoint: Trends.tags) self.suggestedAccounts = data.suggestedAccounts
async let trendingStatuses: [Status] = client.get(endpoint: Trends.statuses(offset: nil)) self.trendingTags = data.trendingTags
async let trendingLinks: [Card] = client.get(endpoint: Trends.links) self.trendingStatuses = data.trendingStatuses
self.trendingLinks = data.trendingLinks
self.suggestedAccounts = try await suggestedAccounts
self.trendingTags = try await trendingTags
self.trendingStatuses = try await trendingStatuses
self.trendingLinks = try await trendingLinks
self.suggestedAccountsRelationShips = try await client.get(endpoint: Accounts.relationships(ids: self.suggestedAccounts.map{ $0.id })) self.suggestedAccountsRelationShips = try await client.get(endpoint: Accounts.relationships(ids: self.suggestedAccounts.map{ $0.id }))
@ -85,6 +79,24 @@ class ExploreViewModel: ObservableObject {
} }
} }
private struct TrendingData {
let suggestedAccounts: [Account]
let trendingTags: [Tag]
let trendingStatuses: [Status]
let trendingLinks: [Card]
}
private func fetchTrendingsData(client: Client) async throws -> TrendingData {
async let suggestedAccounts: [Account] = client.get(endpoint: Accounts.suggestions)
async let trendingTags: [Tag] = client.get(endpoint: Trends.tags)
async let trendingStatuses: [Status] = client.get(endpoint: Trends.statuses(offset: nil))
async let trendingLinks: [Card] = client.get(endpoint: Trends.links)
return try await .init(suggestedAccounts: suggestedAccounts,
trendingTags: trendingTags,
trendingStatuses: trendingStatuses,
trendingLinks: trendingLinks)
}
func search() { func search() {
guard !searchQuery.isEmpty else { return } guard !searchQuery.isEmpty else { return }
searchTask?.cancel() searchTask?.cancel()