2019-12-20 02:20:29 +00:00
|
|
|
//
|
|
|
|
// AddSavedHashtagViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/19/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
|
|
|
|
2021-01-29 04:20:25 +00:00
|
|
|
class AddSavedHashtagViewController: EnhancedTableViewController {
|
2019-12-20 02:20:29 +00:00
|
|
|
|
2021-01-29 04:20:25 +00:00
|
|
|
weak var mastodonController: MastodonController!
|
|
|
|
|
|
|
|
var resultsController: SearchResultsViewController!
|
2019-12-20 02:20:29 +00:00
|
|
|
var searchController: UISearchController!
|
|
|
|
|
2021-01-29 04:20:25 +00:00
|
|
|
var dataSource: UITableViewDiffableDataSource<Section, Item>!
|
|
|
|
|
2020-10-16 23:14:29 +00:00
|
|
|
init(mastodonController: MastodonController) {
|
2021-01-29 04:20:25 +00:00
|
|
|
self.mastodonController = mastodonController
|
|
|
|
|
|
|
|
super.init(style: .grouped)
|
2020-10-16 23:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2019-12-20 02:20:29 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-01-29 04:20:25 +00:00
|
|
|
title = NSLocalizedString("Search", comment: "search screen title")
|
|
|
|
|
|
|
|
tableView.register(UINib(nibName: "TrendingHashtagTableViewCell", bundle: .main), forCellReuseIdentifier: "trendingTagCell")
|
2021-02-06 19:54:35 +00:00
|
|
|
tableView.rowHeight = 60 // 44 for content + 2 * 8 spacing
|
|
|
|
|
2021-01-29 04:20:25 +00:00
|
|
|
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!
|
2019-12-20 02:20:29 +00:00
|
|
|
|
2021-01-29 04:20:25 +00:00
|
|
|
searchController = UISearchController(searchResultsController: resultsController)
|
|
|
|
searchController.obscuresBackgroundDuringPresentation = true
|
2019-12-20 02:20:29 +00:00
|
|
|
searchController.hidesNavigationBarDuringPresentation = false
|
2021-01-29 04:20:25 +00:00
|
|
|
searchController.searchResultsUpdater = resultsController
|
2019-12-20 02:20:29 +00:00
|
|
|
searchController.searchBar.autocapitalizationType = .none
|
|
|
|
searchController.searchBar.placeholder = NSLocalizedString("Search for hashtags to save", comment: "add saved hashtag search field placeholder")
|
2021-01-29 04:20:25 +00:00
|
|
|
searchController.searchBar.delegate = resultsController
|
|
|
|
searchController.searchBar.showsCancelButton = false
|
2019-12-20 02:20:29 +00:00
|
|
|
|
|
|
|
definesPresentationContext = true
|
|
|
|
|
|
|
|
navigationItem.searchController = searchController
|
|
|
|
navigationItem.hidesSearchBarWhenScrolling = false
|
|
|
|
|
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed))
|
|
|
|
}
|
|
|
|
|
2021-01-29 04:20:25 +00:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
let request = Client.getTrends(limit: 10)
|
|
|
|
mastodonController.run(request) { (response) in
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
|
|
|
|
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)
|
2019-12-20 02:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Interaction
|
|
|
|
|
|
|
|
@objc func cancelButtonPressed() {
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-29 04:20:25 +00:00
|
|
|
extension AddSavedHashtagViewController {
|
|
|
|
enum Section {
|
|
|
|
case trendingTags
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Item: Hashable {
|
|
|
|
case tag(Hashtag)
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataSource: UITableViewDiffableDataSource<Section, Item> {
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
return NSLocalizedString("Trending Hashtags", comment: "trending hashtags seciton title")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 02:20:29 +00:00
|
|
|
extension AddSavedHashtagViewController: SearchResultsViewControllerDelegate {
|
|
|
|
func selectedSearchResult(hashtag: Hashtag) {
|
2021-01-29 04:20:25 +00:00
|
|
|
selectHashtag(hashtag)
|
2019-12-20 02:20:29 +00:00
|
|
|
}
|
|
|
|
}
|