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
147 lines
5.1 KiB
//
|
|
// SearchTableViewController.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 11/30/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import Kingfisher
|
|
import UIKit
|
|
|
|
class SearchTableViewController: UITableViewController {
|
|
@IBOutlet var searchBar: UISearchBar!
|
|
@IBOutlet var searchSegmentedControl: UISegmentedControl!
|
|
@IBOutlet var noResultsView: UIView!
|
|
|
|
var accounts: [AccountMO]?
|
|
var hashtags: [TagMO]?
|
|
var statuses: [StatusMO]?
|
|
|
|
func clearResults() {
|
|
self.accounts = []
|
|
self.hashtags = []
|
|
self.accounts = []
|
|
|
|
tableView.reloadData()
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
tableView.backgroundView = noResultsView
|
|
searchBar.delegate = self
|
|
}
|
|
|
|
@IBAction func searchSegmentedControlTapped(_ sender: Any) {
|
|
tableView.reloadData()
|
|
|
|
if tableView.numberOfRows(inSection: 0) == 0 {
|
|
tableView.backgroundView = noResultsView
|
|
} else {
|
|
tableView.backgroundView = nil
|
|
}
|
|
}
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
return 1
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
switch searchSegmentedControl.selectedSegmentIndex {
|
|
case 0: return accounts?.count ?? 0
|
|
case 1: return hashtags?.count ?? 0
|
|
case 2: return statuses?.count ?? 0
|
|
default: return 0
|
|
}
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
switch searchSegmentedControl.selectedSegmentIndex {
|
|
case 0:
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "AccountSearchTableViewCell", for: indexPath) as? AccountSearchTableViewCell else {
|
|
fatalError("Unable to find reusable cell")
|
|
}
|
|
|
|
if let account = self.accounts?[indexPath.row] {
|
|
cell.avatarImageView.setRoundedCorners()
|
|
cell.avatarImageView.kf.setImage(with: account.avatarURL)
|
|
cell.displayNameLabel.text = account.displayName
|
|
cell.usernameLabel.text = "@\(account.acct!)"
|
|
}
|
|
|
|
return cell
|
|
case 1:
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "HashtagSearchTableViewCell", for: indexPath) as? HashtagSearchTableViewCell else {
|
|
fatalError("Unable to find reusable cell")
|
|
}
|
|
|
|
if let hashtag = self.hashtags?[indexPath.row] {
|
|
cell.hashtagLabel.text = "#\(hashtag.name!)"
|
|
}
|
|
|
|
return cell
|
|
case 2:
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "StatusSearchTableViewCell", for: indexPath) as? StatusSearchTableViewCell else {
|
|
fatalError("Unable to find reusable cell")
|
|
}
|
|
|
|
if let status = self.statuses?[indexPath.row] {
|
|
cell.statusView.update(withStatus: status)
|
|
}
|
|
|
|
return cell
|
|
default:
|
|
fatalError("")
|
|
}
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
switch searchSegmentedControl.selectedSegmentIndex {
|
|
case 0:
|
|
if let account = self.accounts?[indexPath.row] {
|
|
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AccountTableViewController") as? AccountTableViewController {
|
|
controller.account = account
|
|
self.navigationController?.pushViewController(controller, animated: true)
|
|
}
|
|
}
|
|
default:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
extension SearchTableViewController: UISearchBarDelegate {
|
|
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
|
if let search = searchBar.text, !search.isEmpty {
|
|
searchBar.resignFirstResponder()
|
|
|
|
MastodonAPI.search(content: search) { data, error in
|
|
guard let data = data, error == nil else {
|
|
return
|
|
}
|
|
|
|
if let accounts = data["accounts"] as? JSONObjectArray {
|
|
self.accounts = accounts.compactMap { MastodonDataManager.upsertAccount($0) }
|
|
}
|
|
|
|
if let hashtags = data["hashtags"] as? JSONObjectArray {
|
|
self.hashtags = hashtags.compactMap { MastodonDataManager.upsertTag($0) }
|
|
}
|
|
|
|
if let statuses = data["statuses"] as? JSONObjectArray {
|
|
self.statuses = statuses.compactMap { MastodonDataManager.upsertStatus($0) }.map { $0.object }
|
|
}
|
|
|
|
self.tableView.reloadData()
|
|
}
|
|
} else {
|
|
clearResults()
|
|
searchBar.resignFirstResponder()
|
|
}
|
|
}
|
|
|
|
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
|
|
clearResults()
|
|
}
|
|
}
|