Compare commits
No commits in common. "b51c1c03cb3cca49092430c37687a4cda12a27cd" and "421881d461a0b52467eedb6beb47962f6886e2e5" have entirely different histories.
b51c1c03cb
...
421881d461
|
@ -36,7 +36,6 @@ 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,22 +60,6 @@ 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)
|
||||||
|
@ -148,77 +131,57 @@ 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
|
||||||
switch response {
|
guard case let .success(results, _) = response else { fatalError() }
|
||||||
case let .success(results, _):
|
|
||||||
guard self.currentQuery == query else { return }
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.activityIndicator.isHidden = true
|
|
||||||
self.activityIndicator.stopAnimating()
|
|
||||||
}
|
|
||||||
self.showSearchResults(results)
|
|
||||||
case let .failure(error):
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.activityIndicator.isHidden = true
|
|
||||||
self.activityIndicator.stopAnimating()
|
|
||||||
|
|
||||||
self.showSearchError(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func showSearchResults(_ results: SearchResults) {
|
|
||||||
let oldSnapshot = self.dataSource.snapshot()
|
|
||||||
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
||||||
|
|
||||||
self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in
|
|
||||||
if oldSnapshot.indexOfSection(.accounts) != nil {
|
|
||||||
oldSnapshot.itemIdentifiers(inSection: .accounts).forEach { (item) in
|
|
||||||
guard case let .account(id) = item else { return }
|
|
||||||
self.mastodonController.persistentContainer.account(for: id, in: context)?.decrementReferenceCount()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if oldSnapshot.indexOfSection(.statuses) != nil {
|
|
||||||
oldSnapshot.itemIdentifiers(inSection: .statuses).forEach { (item) in
|
|
||||||
guard case let .status(id, _) = item else { return }
|
|
||||||
self.mastodonController.persistentContainer.status(for: id, in: context)?.decrementReferenceCount()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let resultTypes = self.resultTypes
|
|
||||||
if !results.accounts.isEmpty && (resultTypes == nil || resultTypes!.contains(.accounts)) {
|
|
||||||
snapshot.appendSections([.accounts])
|
|
||||||
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
|
|
||||||
addAccounts(results.accounts)
|
|
||||||
}
|
|
||||||
if !results.hashtags.isEmpty && (resultTypes == nil || resultTypes!.contains(.hashtags)) {
|
|
||||||
snapshot.appendSections([.hashtags])
|
|
||||||
snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags)
|
|
||||||
}
|
|
||||||
if !results.statuses.isEmpty && (resultTypes == nil || resultTypes!.contains(.statuses)) {
|
|
||||||
snapshot.appendSections([.statuses])
|
|
||||||
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
|
|
||||||
addStatuses(results.statuses)
|
|
||||||
}
|
|
||||||
}, completion: {
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.errorLabel.isHidden = true
|
self.activityIndicator.isHidden = true
|
||||||
self.dataSource.apply(snapshot)
|
self.activityIndicator.stopAnimating()
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
guard self.currentQuery == query else { return }
|
||||||
|
|
||||||
private func showSearchError(_ error: Client.Error) {
|
let oldSnapshot = self.dataSource.snapshot()
|
||||||
let snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
||||||
dataSource.apply(snapshot)
|
|
||||||
|
self.mastodonController.persistentContainer.performBatchUpdates({ (context, addAccounts, addStatuses) in
|
||||||
errorLabel.isHidden = false
|
if oldSnapshot.indexOfSection(.accounts) != nil {
|
||||||
errorLabel.text = error.localizedDescription
|
oldSnapshot.itemIdentifiers(inSection: .accounts).forEach { (item) in
|
||||||
|
guard case let .account(id) = item else { return }
|
||||||
|
self.mastodonController.persistentContainer.account(for: id, in: context)?.decrementReferenceCount()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if oldSnapshot.indexOfSection(.statuses) != nil {
|
||||||
|
oldSnapshot.itemIdentifiers(inSection: .statuses).forEach { (item) in
|
||||||
|
guard case let .status(id, _) = item else { return }
|
||||||
|
self.mastodonController.persistentContainer.status(for: id, in: context)?.decrementReferenceCount()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !results.accounts.isEmpty && (resultTypes == nil || resultTypes!.contains(.accounts)) {
|
||||||
|
snapshot.appendSections([.accounts])
|
||||||
|
snapshot.appendItems(results.accounts.map { .account($0.id) }, toSection: .accounts)
|
||||||
|
addAccounts(results.accounts)
|
||||||
|
}
|
||||||
|
if !results.hashtags.isEmpty && (resultTypes == nil || resultTypes!.contains(.hashtags)) {
|
||||||
|
snapshot.appendSections([.hashtags])
|
||||||
|
snapshot.appendItems(results.hashtags.map { .hashtag($0) }, toSection: .hashtags)
|
||||||
|
}
|
||||||
|
if !results.statuses.isEmpty && (resultTypes == nil || resultTypes!.contains(.statuses)) {
|
||||||
|
snapshot.appendSections([.statuses])
|
||||||
|
snapshot.appendItems(results.statuses.map { .status($0.id, .unknown) }, toSection: .statuses)
|
||||||
|
addStatuses(results.statuses)
|
||||||
|
}
|
||||||
|
}, completion: {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.dataSource.apply(snapshot)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Table view delegate
|
// MARK: - Table view delegate
|
||||||
|
|
|
@ -40,7 +40,6 @@ class SearchViewController: UIViewController {
|
||||||
searchController.searchResultsUpdater = resultsController
|
searchController.searchResultsUpdater = resultsController
|
||||||
searchController.searchBar.autocapitalizationType = .none
|
searchController.searchBar.autocapitalizationType = .none
|
||||||
searchController.searchBar.delegate = resultsController
|
searchController.searchBar.delegate = resultsController
|
||||||
searchController.hidesNavigationBarDuringPresentation = false
|
|
||||||
definesPresentationContext = true
|
definesPresentationContext = true
|
||||||
|
|
||||||
navigationItem.searchController = searchController
|
navigationItem.searchController = searchController
|
||||||
|
|
|
@ -30,7 +30,6 @@ class PollOptionView: UIView {
|
||||||
let label = EmojiLabel()
|
let label = EmojiLabel()
|
||||||
label.translatesAutoresizingMaskIntoConstraints = false
|
label.translatesAutoresizingMaskIntoConstraints = false
|
||||||
label.numberOfLines = 0
|
label.numberOfLines = 0
|
||||||
label.font = .preferredFont(forTextStyle: .callout)
|
|
||||||
label.text = option.title
|
label.text = option.title
|
||||||
label.setEmojis(poll.emojis, identifier: poll.id)
|
label.setEmojis(poll.emojis, identifier: poll.id)
|
||||||
addSubview(label)
|
addSubview(label)
|
||||||
|
@ -38,10 +37,7 @@ class PollOptionView: UIView {
|
||||||
let percentLabel = UILabel()
|
let percentLabel = UILabel()
|
||||||
percentLabel.translatesAutoresizingMaskIntoConstraints = false
|
percentLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||||
percentLabel.text = "100%"
|
percentLabel.text = "100%"
|
||||||
percentLabel.font = label.font
|
|
||||||
percentLabel.isHidden = true
|
percentLabel.isHidden = true
|
||||||
percentLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
|
|
||||||
percentLabel.setContentHuggingPriority(.required, for: .horizontal)
|
|
||||||
addSubview(percentLabel)
|
addSubview(percentLabel)
|
||||||
|
|
||||||
if (poll.voted ?? false) || poll.effectiveExpired,
|
if (poll.voted ?? false) || poll.effectiveExpired,
|
||||||
|
@ -80,7 +76,7 @@ class PollOptionView: UIView {
|
||||||
label.topAnchor.constraint(equalTo: topAnchor),
|
label.topAnchor.constraint(equalTo: topAnchor),
|
||||||
label.bottomAnchor.constraint(equalTo: bottomAnchor),
|
label.bottomAnchor.constraint(equalTo: bottomAnchor),
|
||||||
label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: 8),
|
label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: 8),
|
||||||
label.trailingAnchor.constraint(equalTo: percentLabel.leadingAnchor, constant: -4),
|
label.trailingAnchor.constraint(equalTo: percentLabel.leadingAnchor),
|
||||||
|
|
||||||
percentLabel.topAnchor.constraint(equalTo: topAnchor),
|
percentLabel.topAnchor.constraint(equalTo: topAnchor),
|
||||||
percentLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
|
percentLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
|
||||||
|
|
|
@ -23,7 +23,7 @@ class StatusPollView: UIView {
|
||||||
weak var mastodonController: MastodonController!
|
weak var mastodonController: MastodonController!
|
||||||
|
|
||||||
private var statusID: String!
|
private var statusID: String!
|
||||||
private(set) var poll: Poll?
|
private var poll: Poll!
|
||||||
|
|
||||||
private var optionsView: PollOptionsView!
|
private var optionsView: PollOptionsView!
|
||||||
private var voteButton: UIButton!
|
private var voteButton: UIButton!
|
||||||
|
@ -76,15 +76,12 @@ class StatusPollView: UIView {
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateUI(status: StatusMO, poll: Poll?) {
|
func updateUI(status: StatusMO, poll: Poll) {
|
||||||
self.statusID = status.id
|
self.statusID = status.id
|
||||||
self.poll = poll
|
self.poll = poll
|
||||||
|
|
||||||
// remove old options
|
|
||||||
options.forEach { $0.removeFromSuperview() }
|
options.forEach { $0.removeFromSuperview() }
|
||||||
|
|
||||||
guard let poll = poll else { return }
|
|
||||||
|
|
||||||
// poll.voted is nil if there is no user (e.g., public timeline), in which case the poll also cannot be voted upon
|
// poll.voted is nil if there is no user (e.g., public timeline), in which case the poll also cannot be voted upon
|
||||||
if (poll.voted ?? true) || poll.expired || status.account.id == mastodonController.account.id {
|
if (poll.voted ?? true) || poll.expired || status.account.id == mastodonController.account.id {
|
||||||
canVote = false
|
canVote = false
|
||||||
|
@ -141,7 +138,7 @@ class StatusPollView: UIView {
|
||||||
|
|
||||||
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
|
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
|
||||||
|
|
||||||
let request = Poll.vote(poll!.id, choices: optionsView.checkedOptionIndices)
|
let request = Poll.vote(poll.id, choices: optionsView.checkedOptionIndices)
|
||||||
mastodonController.run(request) { (response) in
|
mastodonController.run(request) { (response) in
|
||||||
switch response {
|
switch response {
|
||||||
case let .failure(error):
|
case let .failure(error):
|
||||||
|
|
|
@ -215,9 +215,13 @@ class BaseStatusTableViewCell: UITableViewCell, MenuPreviewProvider {
|
||||||
// keep menu in sync with changed states e.g. bookmarked, muted
|
// keep menu in sync with changed states e.g. bookmarked, muted
|
||||||
moreButton.menu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: actionsForStatus(status, sourceView: moreButton))
|
moreButton.menu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: actionsForStatus(status, sourceView: moreButton))
|
||||||
|
|
||||||
pollView.isHidden = status.poll == nil
|
if let poll = status.poll {
|
||||||
pollView.mastodonController = mastodonController
|
pollView.isHidden = false
|
||||||
pollView.updateUI(status: status, poll: status.poll)
|
pollView.mastodonController = mastodonController
|
||||||
|
pollView.updateUI(status: status, poll: poll)
|
||||||
|
} else {
|
||||||
|
pollView.isHidden = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateUI(account: AccountMO) {
|
func updateUI(account: AccountMO) {
|
||||||
|
@ -355,7 +359,6 @@ class BaseStatusTableViewCell: UITableViewCell, MenuPreviewProvider {
|
||||||
contentTextView.isHidden = collapsed
|
contentTextView.isHidden = collapsed
|
||||||
cardView.isHidden = cardView.card == nil || collapsed
|
cardView.isHidden = cardView.card == nil || collapsed
|
||||||
attachmentsView.isHidden = attachmentsView.attachments.count == 0 || collapsed
|
attachmentsView.isHidden = attachmentsView.attachments.count == 0 || collapsed
|
||||||
pollView.isHidden = pollView.poll == nil || collapsed
|
|
||||||
|
|
||||||
let buttonImage = UIImage(systemName: collapsed ? "chevron.down" : "chevron.up")!
|
let buttonImage = UIImage(systemName: collapsed ? "chevron.down" : "chevron.up")!
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue