// // AddSavedHashtagViewController.swift // Tusker // // Created by Shadowfacts on 12/19/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class AddSavedHashtagViewController: EnhancedTableViewController { weak var mastodonController: MastodonController! var resultsController: SearchResultsViewController! var searchController: UISearchController! var dataSource: UITableViewDiffableDataSource! init(mastodonController: MastodonController) { self.mastodonController = mastodonController super.init(style: .grouped) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() title = NSLocalizedString("Search", comment: "search screen title") tableView.register(UINib(nibName: "TrendingHashtagTableViewCell", bundle: .main), forCellReuseIdentifier: "trendingTagCell") tableView.rowHeight = 60 // 44 for content + 2 * 8 spacing dataSource = DataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in switch item { case let .tag(hashtag): let cell = tableView.dequeueReusableCell(withIdentifier: "trendingTagCell", for: indexPath) as! TrendingHashtagTableViewCell cell.updateUI(hashtag: hashtag) return cell } }) resultsController = HashtagSearchResultsViewController(mastodonController: mastodonController) resultsController.delegate = self resultsController.exploreNavigationController = self.navigationController! searchController = UISearchController(searchResultsController: resultsController) searchController.obscuresBackgroundDuringPresentation = true searchController.hidesNavigationBarDuringPresentation = false searchController.searchResultsUpdater = resultsController searchController.searchBar.autocapitalizationType = .none searchController.searchBar.placeholder = NSLocalizedString("Search for hashtags to save", comment: "add saved hashtag search field placeholder") searchController.searchBar.delegate = resultsController searchController.searchBar.showsCancelButton = false definesPresentationContext = true navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed)) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let request = Client.getTrends(limit: 10) mastodonController.run(request) { (response) in var snapshot = NSDiffableDataSourceSnapshot() guard case let .success(hashtags, _) = response, hashtags.count > 0 else { self.dataSource.apply(snapshot) return } snapshot.appendSections([.trendingTags]) snapshot.appendItems(hashtags.map { .tag($0) }) self.dataSource.apply(snapshot, animatingDifferences: false) } } private func selectHashtag(_ hashtag: Hashtag) { SavedDataManager.shared.add(hashtag: hashtag, for: mastodonController.accountInfo!) presentingViewController!.dismiss(animated: true) } // MARK: - Table View Delegate override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { switch dataSource.itemIdentifier(for: indexPath) { case nil: return case let .tag(hashtag): selectHashtag(hashtag) } } // MARK: - Interaction @objc func cancelButtonPressed() { dismiss(animated: true) } } extension AddSavedHashtagViewController { enum Section { case trendingTags } enum Item: Hashable { case tag(Hashtag) } class DataSource: UITableViewDiffableDataSource { override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return NSLocalizedString("Trending Hashtags", comment: "trending hashtags seciton title") } } } extension AddSavedHashtagViewController: SearchResultsViewControllerDelegate { func selectedSearchResult(hashtag: Hashtag) { selectHashtag(hashtag) } }