[ABANDONED] Mastodon iOS client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.1 KiB

4 years ago
  1. //
  2. // SearchTableViewController.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 11/30/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import Kingfisher
  9. import UIKit
  10. class SearchTableViewController: UITableViewController {
  11. @IBOutlet var searchBar: UISearchBar!
  12. @IBOutlet var searchSegmentedControl: UISegmentedControl!
  13. @IBOutlet var noResultsView: UIView!
  14. var accounts: [AccountMO]?
  15. var hashtags: [TagMO]?
  16. var statuses: [StatusMO]?
  17. func clearResults() {
  18. self.accounts = []
  19. self.hashtags = []
  20. self.accounts = []
  21. tableView.reloadData()
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. tableView.backgroundView = noResultsView
  26. searchBar.delegate = self
  27. }
  28. @IBAction func searchSegmentedControlTapped(_ sender: Any) {
  29. tableView.reloadData()
  30. if tableView.numberOfRows(inSection: 0) == 0 {
  31. tableView.backgroundView = noResultsView
  32. } else {
  33. tableView.backgroundView = nil
  34. }
  35. }
  36. override func numberOfSections(in tableView: UITableView) -> Int {
  37. return 1
  38. }
  39. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  40. switch searchSegmentedControl.selectedSegmentIndex {
  41. case 0: return accounts?.count ?? 0
  42. case 1: return hashtags?.count ?? 0
  43. case 2: return statuses?.count ?? 0
  44. default: return 0
  45. }
  46. }
  47. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  48. switch searchSegmentedControl.selectedSegmentIndex {
  49. case 0:
  50. guard let cell = tableView.dequeueReusableCell(withIdentifier: "AccountSearchTableViewCell", for: indexPath) as? AccountSearchTableViewCell else {
  51. fatalError("Unable to find reusable cell")
  52. }
  53. if let account = self.accounts?[indexPath.row] {
  54. cell.avatarImageView.setRoundedCorners()
  55. cell.avatarImageView.kf.setImage(with: account.avatarURL)
  56. cell.displayNameLabel.text = account.displayName
  57. cell.usernameLabel.text = "@\(account.acct!)"
  58. }
  59. return cell
  60. case 1:
  61. guard let cell = tableView.dequeueReusableCell(withIdentifier: "HashtagSearchTableViewCell", for: indexPath) as? HashtagSearchTableViewCell else {
  62. fatalError("Unable to find reusable cell")
  63. }
  64. if let hashtag = self.hashtags?[indexPath.row] {
  65. cell.hashtagLabel.text = "#\(hashtag.name!)"
  66. }
  67. return cell
  68. case 2:
  69. guard let cell = tableView.dequeueReusableCell(withIdentifier: "StatusSearchTableViewCell", for: indexPath) as? StatusSearchTableViewCell else {
  70. fatalError("Unable to find reusable cell")
  71. }
  72. if let status = self.statuses?[indexPath.row] {
  73. cell.statusView.update(withStatus: status)
  74. }
  75. return cell
  76. default:
  77. fatalError("")
  78. }
  79. }
  80. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  81. switch searchSegmentedControl.selectedSegmentIndex {
  82. case 0:
  83. if let account = self.accounts?[indexPath.row] {
  84. if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AccountTableViewController") as? AccountTableViewController {
  85. controller.account = account
  86. self.navigationController?.pushViewController(controller, animated: true)
  87. }
  88. }
  89. default:
  90. return
  91. }
  92. }
  93. }
  94. extension SearchTableViewController: UISearchBarDelegate {
  95. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  96. if let search = searchBar.text, !search.isEmpty {
  97. searchBar.resignFirstResponder()
  98. MastodonAPI.search(content: search) { data, error in
  99. guard let data = data, error == nil else {
  100. return
  101. }
  102. if let accounts = data["accounts"] as? JSONObjectArray {
  103. self.accounts = accounts.compactMap { MastodonDataManager.upsertAccount($0) }
  104. }
  105. if let hashtags = data["hashtags"] as? JSONObjectArray {
  106. self.hashtags = hashtags.compactMap { MastodonDataManager.upsertTag($0) }
  107. }
  108. if let statuses = data["statuses"] as? JSONObjectArray {
  109. self.statuses = statuses.compactMap { MastodonDataManager.upsertStatus($0) }.map { $0.object }
  110. }
  111. self.tableView.reloadData()
  112. }
  113. } else {
  114. clearResults()
  115. searchBar.resignFirstResponder()
  116. }
  117. }
  118. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  119. clearResults()
  120. }
  121. }