Refactor search

This commit is contained in:
Justin Mazzocchi 2021-02-07 22:11:32 -08:00
parent e420c6b934
commit 70de6dd475
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -572,19 +572,21 @@ public extension ContentDatabase {
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
// swiftlint:disable:next function_body_length
func publisher(results: Results, limit: Int?) -> AnyPublisher<[CollectionSection], Error> { func publisher(results: Results, limit: Int?) -> AnyPublisher<[CollectionSection], Error> {
let accountIds = results.accounts.map(\.id) let accountIds = results.accounts.map(\.id)
let statusIds = results.statuses.map(\.id) let statusIds = results.statuses.map(\.id)
let accountsPublisher = ValueObservation.tracking( return ValueObservation.tracking { db -> ([AccountAndRelationshipInfo], [StatusInfo]) in
AccountAndRelationshipInfo.request( (try AccountAndRelationshipInfo.request(
AccountRecord.filter(accountIds.contains(AccountRecord.Columns.id))) AccountRecord.filter(accountIds.contains(AccountRecord.Columns.id)))
.fetchAll) .fetchAll(db),
.removeDuplicates() try StatusInfo.request(
StatusRecord.filter(statusIds.contains(StatusRecord.Columns.id)))
.fetchAll(db))
}
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.map { infos -> [CollectionItem] in .map { accountAndRelationshipInfos, statusInfos in
var accounts = infos.sorted { var accounts = accountAndRelationshipInfos.sorted {
accountIds.firstIndex(of: $0.accountInfo.record.id) ?? 0 accountIds.firstIndex(of: $0.accountInfo.record.id) ?? 0
< accountIds.firstIndex(of: $1.accountInfo.record.id) ?? 0 < accountIds.firstIndex(of: $1.accountInfo.record.id) ?? 0
} }
@ -594,17 +596,7 @@ public extension ContentDatabase {
accounts.append(.moreResults(.init(scope: .accounts))) accounts.append(.moreResults(.init(scope: .accounts)))
} }
return accounts var statuses = statusInfos.sorted {
}
let statusesPublisher = ValueObservation.tracking(
StatusInfo.request(
StatusRecord.filter(statusIds.contains(StatusRecord.Columns.id)))
.fetchAll)
.removeDuplicates()
.publisher(in: databaseWriter)
.map { infos -> [CollectionItem] in
var statuses = infos.sorted {
statusIds.firstIndex(of: $0.record.id) ?? 0 statusIds.firstIndex(of: $0.record.id) ?? 0
< statusIds.firstIndex(of: $1.record.id) ?? 0 < statusIds.firstIndex(of: $1.record.id) ?? 0
} }
@ -620,18 +612,13 @@ public extension ContentDatabase {
statuses.append(.moreResults(.init(scope: .statuses))) statuses.append(.moreResults(.init(scope: .statuses)))
} }
return statuses
}
var hashtags = results.hashtags.map(CollectionItem.tag) var hashtags = results.hashtags.map(CollectionItem.tag)
if let limit = limit, hashtags.count >= limit { if let limit = limit, hashtags.count >= limit {
hashtags.append(.moreResults(.init(scope: .tags))) hashtags.append(.moreResults(.init(scope: .tags)))
} }
return accountsPublisher.combineLatest(statusesPublisher) return [.init(items: accounts, searchScope: .accounts),
.map { accounts, statuses in
[.init(items: accounts, searchScope: .accounts),
.init(items: statuses, searchScope: .statuses), .init(items: statuses, searchScope: .statuses),
.init(items: hashtags, searchScope: .tags)] .init(items: hashtags, searchScope: .tags)]
.filter { !$0.items.isEmpty } .filter { !$0.items.isEmpty }