Fix crash when searching fails

This commit is contained in:
Shadowfacts 2021-05-22 11:22:01 -04:00
parent 421881d461
commit 9ec7177bfa
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 82 additions and 45 deletions

View File

@ -36,6 +36,7 @@ class SearchResultsViewController: EnhancedTableViewController {
var dataSource: UITableViewDiffableDataSource<Section, Item>! var dataSource: UITableViewDiffableDataSource<Section, Item>!
private var activityIndicator: UIActivityIndicatorView! private var activityIndicator: UIActivityIndicatorView!
private var errorLabel: UILabel!
/// Types of results to search for. `nil` means all results will be included. /// Types of results to search for. `nil` means all results will be included.
var resultTypes: [SearchResultType]? = nil var resultTypes: [SearchResultType]? = nil
@ -61,6 +62,22 @@ class SearchResultsViewController: EnhancedTableViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
errorLabel = UILabel()
errorLabel.translatesAutoresizingMaskIntoConstraints = false
errorLabel.font = .preferredFont(forTextStyle: .callout)
errorLabel.textColor = .secondaryLabel
errorLabel.numberOfLines = 0
errorLabel.textAlignment = .center
errorLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
tableView.addSubview(errorLabel)
NSLayoutConstraint.activate([
errorLabel.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor),
errorLabel.centerXAnchor.constraint(equalTo: tableView.centerXAnchor),
errorLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: tableView.leadingAnchor, multiplier: 1),
tableView.trailingAnchor.constraint(equalToSystemSpacingAfter: errorLabel.trailingAnchor, multiplier: 1),
])
tableView.register(UINib(nibName: "AccountTableViewCell", bundle: .main), forCellReuseIdentifier: accountCell) tableView.register(UINib(nibName: "AccountTableViewCell", bundle: .main), forCellReuseIdentifier: accountCell)
tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: statusCell) tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: statusCell)
tableView.register(UINib(nibName: "HashtagTableViewCell", bundle: .main), forCellReuseIdentifier: hashtagCell) tableView.register(UINib(nibName: "HashtagTableViewCell", bundle: .main), forCellReuseIdentifier: hashtagCell)
@ -131,19 +148,30 @@ class SearchResultsViewController: EnhancedTableViewController {
activityIndicator.isHidden = false activityIndicator.isHidden = false
activityIndicator.startAnimating() activityIndicator.startAnimating()
errorLabel.isHidden = true
let resultTypes = self.resultTypes
let request = Client.search(query: query, types: resultTypes, resolve: true, limit: 10) let request = Client.search(query: query, types: resultTypes, resolve: true, limit: 10)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
guard case let .success(results, _) = response else { fatalError() } switch response {
case let .success(results, _):
guard self.currentQuery == query else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
self.activityIndicator.isHidden = true self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating() self.activityIndicator.stopAnimating()
} }
self.showSearchResults(results)
case let .failure(error):
DispatchQueue.main.async {
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
guard self.currentQuery == query else { return } self.showSearchError(error)
}
}
}
}
private func showSearchResults(_ results: SearchResults) {
let oldSnapshot = self.dataSource.snapshot() let oldSnapshot = self.dataSource.snapshot()
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
@ -162,6 +190,7 @@ class SearchResultsViewController: EnhancedTableViewController {
} }
} }
let resultTypes = self.resultTypes
if !results.accounts.isEmpty && (resultTypes == nil || resultTypes!.contains(.accounts)) { if !results.accounts.isEmpty && (resultTypes == nil || resultTypes!.contains(.accounts)) {
snapshot.appendSections([.accounts]) snapshot.appendSections([.accounts])
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts) snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
@ -178,10 +207,18 @@ class SearchResultsViewController: EnhancedTableViewController {
} }
}, completion: { }, completion: {
DispatchQueue.main.async { DispatchQueue.main.async {
self.errorLabel.isHidden = true
self.dataSource.apply(snapshot) self.dataSource.apply(snapshot)
} }
}) })
} }
private func showSearchError(_ error: Client.Error) {
let snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
dataSource.apply(snapshot)
errorLabel.isHidden = false
errorLabel.text = error.localizedDescription
} }
// MARK: - Table view delegate // MARK: - Table view delegate