Compare commits

..

No commits in common. "5cef76e4948d981fa4d8aaec93e7311de0cdcb9c" and "32b8d2794957d444d53a24815fa637bf4ddc0fb7" have entirely different histories.

2 changed files with 35 additions and 47 deletions

View File

@ -88,7 +88,7 @@ class MastodonSearchController: UISearchController {
if searchText.isEmpty || "from:me".contains(searchText) { if searchText.isEmpty || "from:me".contains(searchText) {
fromSuggestions.append("from:me") fromSuggestions.append("from:me")
} }
if searchText != "me" && searchText != "from:me", if searchText != "me",
let match = acctRegex.firstMatch(in: searchText, range: NSRange(location: 0, length: searchText.utf16.count)) { let match = acctRegex.firstMatch(in: searchText, range: NSRange(location: 0, length: searchText.utf16.count)) {
let matched = (searchText as NSString).substring(with: match.range) let matched = (searchText as NSString).substring(with: match.range)
fromSuggestions.append("from:\(matched)") fromSuggestions.append("from:\(matched)")

View File

@ -46,7 +46,6 @@ class SearchResultsViewController: UIViewController, CollectionViewController {
private let searchSubject = PassthroughSubject<String?, Never>() private let searchSubject = PassthroughSubject<String?, Never>()
private var searchCancellable: AnyCancellable? private var searchCancellable: AnyCancellable?
private var currentQuery: String? private var currentQuery: String?
private var currentSearchResults: SearchResults?
init(mastodonController: MastodonController, scope: Scope = .all) { init(mastodonController: MastodonController, scope: Scope = .all) {
self.mastodonController = mastodonController self.mastodonController = mastodonController
@ -261,14 +260,7 @@ class SearchResultsViewController: UIViewController, CollectionViewController {
switch response { switch response {
case let .success(results, _): case let .success(results, _):
guard self.currentQuery == query else { return } guard self.currentQuery == query else { return }
self.mastodonController.persistentContainer.performBatchUpdates { (context, addAccounts, addStatuses) in self.showSearchResults(results)
addAccounts(results.accounts)
addStatuses(results.statuses)
} completion: {
DispatchQueue.main.async {
self.showSearchResults(results)
}
}
case let .failure(error): case let .failure(error):
DispatchQueue.main.async { DispatchQueue.main.async {
self.showSearchError(error) self.showSearchError(error)
@ -277,29 +269,31 @@ class SearchResultsViewController: UIViewController, CollectionViewController {
} }
} }
@MainActor
private func showSearchResults(_ results: SearchResults) { private func showSearchResults(_ results: SearchResults) {
self.currentSearchResults = results
var snapshot = dataSource.snapshot() var snapshot = dataSource.snapshot()
snapshot.deleteSections([.loadingIndicator]) snapshot.deleteSections([.loadingIndicator])
removeResults(from: &snapshot)
let resultTypes = self.scope.resultTypes self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in
if !results.accounts.isEmpty && resultTypes.contains(.accounts) { let resultTypes = self.scope.resultTypes
snapshot.appendSections([.accounts]) if !results.accounts.isEmpty && resultTypes.contains(.accounts) {
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts) snapshot.appendSections([.accounts])
} snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
if !results.hashtags.isEmpty && resultTypes.contains(.hashtags) { addAccounts(results.accounts)
snapshot.appendSections([.hashtags]) }
snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags) if !results.hashtags.isEmpty && resultTypes.contains(.hashtags) {
} snapshot.appendSections([.hashtags])
if !results.statuses.isEmpty && resultTypes.contains(.statuses) { snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags)
snapshot.appendSections([.statuses]) }
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses) if !results.statuses.isEmpty && resultTypes.contains(.statuses) {
} snapshot.appendSections([.statuses])
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
dataSource.apply(snapshot) addStatuses(results.statuses)
}
}, completion: {
DispatchQueue.main.async {
self.dataSource.apply(snapshot)
}
})
} }
private func showSearchError(_ error: Client.Error) { private func showSearchError(_ error: Client.Error) {
@ -569,25 +563,19 @@ extension SearchResultsViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
let newQuery = searchBar.searchQueryWithOperators let newQuery = searchBar.searchQueryWithOperators
let newScope = Scope.allCases[selectedScope] let newScope = Scope.allCases[selectedScope]
if currentQuery == newQuery, if self.scope == .all && currentQuery == newQuery {
let currentSearchResults { self.scope = newScope
if self.scope == .all { var snapshot = dataSource.snapshot()
self.scope = newScope if snapshot.sectionIdentifiers.contains(.accounts) && scope != .people {
var snapshot = dataSource.snapshot() snapshot.deleteSections([.accounts])
if snapshot.sectionIdentifiers.contains(.accounts) && scope != .people {
snapshot.deleteSections([.accounts])
}
if snapshot.sectionIdentifiers.contains(.hashtags) && scope != .hashtags {
snapshot.deleteSections([.hashtags])
}
if snapshot.sectionIdentifiers.contains(.statuses) && scope != .posts {
snapshot.deleteSections([.statuses])
}
dataSource.apply(snapshot)
} else {
self.scope = newScope
showSearchResults(currentSearchResults)
} }
if snapshot.sectionIdentifiers.contains(.hashtags) && scope != .hashtags {
snapshot.deleteSections([.hashtags])
}
if snapshot.sectionIdentifiers.contains(.statuses) && scope != .posts {
snapshot.deleteSections([.statuses])
}
dataSource.apply(snapshot)
} else { } else {
self.scope = newScope self.scope = newScope
performSearch(query: newQuery) performSearch(query: newQuery)