mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-25 09:41:00 +00:00
Refactor search
This commit is contained in:
parent
e420c6b934
commit
70de6dd475
1 changed files with 45 additions and 58 deletions
|
@ -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 }
|
||||||
|
|
Loading…
Reference in a new issue