// // TrendingHashtagsViewController.swift // Tusker // // Created by Shadowfacts on 2/6/21. // Copyright © 2021 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class TrendingHashtagsViewController: EnhancedTableViewController { weak var mastodonController: MastodonController! private var dataSource: UITableViewDiffableDataSource! init(mastodonController: MastodonController) { self.mastodonController = mastodonController super.init(style: .grouped) dragEnabled = true } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() title = NSLocalizedString("Trending Hashtags", comment: "trending hashtags screen title") tableView.register(UINib(nibName: "TrendingHashtagTableViewCell", bundle: .main), forCellReuseIdentifier: "trendingTagCell") tableView.rowHeight = 60 // 44 for content + 2 * 8 spacing dataSource = UITableViewDiffableDataSource(tableView: tableView) { (tableView, indexPath, item) in switch item { case let .tag(hashtag): let cell = tableView.dequeueReusableCell(withIdentifier: "trendingTagCell", for: indexPath) as! TrendingHashtagTableViewCell cell.updateUI(hashtag: hashtag) return cell } } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let request = Client.getTrendingHashtags(limit: 10) Task { guard let (hashtags, _) = try? await mastodonController.run(request) else { return } var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([.trendingTags]) snapshot.appendItems(hashtags.map { .tag($0) }) dataSource.apply(snapshot) } } // MARK: - Table View Delegate override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let item = dataSource.itemIdentifier(for: indexPath), case let .tag(hashtag) = item else { return } show(HashtagTimelineViewController(for: hashtag, mastodonController: mastodonController), sender: nil) } override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { guard let item = dataSource.itemIdentifier(for: indexPath), case let .tag(hashtag) = item else { return nil } return UIContextMenuConfiguration(identifier: nil) { HashtagTimelineViewController(for: hashtag, mastodonController: self.mastodonController) } actionProvider: { (_) in UIMenu(children: self.actionsForHashtag(hashtag, sourceView: self.tableView.cellForRow(at: indexPath))) } } override func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { guard let item = dataSource.itemIdentifier(for: indexPath), case let .tag(hashtag) = item else { return [] } let provider = NSItemProvider(object: hashtag.url as NSURL) if let activity = UserActivityManager.showTimelineActivity(timeline: .tag(hashtag: hashtag.name), accountID: mastodonController.accountInfo!.id) { provider.registerObject(activity, visibility: .all) } return [UIDragItem(itemProvider: provider)] } } extension TrendingHashtagsViewController { enum Section { case trendingTags } enum Item: Hashable { case tag(Hashtag) } } extension TrendingHashtagsViewController: TuskerNavigationDelegate { var apiController: MastodonController { mastodonController } } extension TrendingHashtagsViewController: MenuPreviewProvider { var navigationDelegate: TuskerNavigationDelegate? { self } }