From 3b132ab4dcb3600ac2f9336fc5b991f8d4d1174a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 24 Nov 2021 15:12:23 -0500 Subject: [PATCH] Enable context menus and drag and drop for trending hashtags --- .../TrendingHashtagsViewController.swift | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/Tusker/Screens/Explore/TrendingHashtagsViewController.swift b/Tusker/Screens/Explore/TrendingHashtagsViewController.swift index 080887fa4d..4b06db0f82 100644 --- a/Tusker/Screens/Explore/TrendingHashtagsViewController.swift +++ b/Tusker/Screens/Explore/TrendingHashtagsViewController.swift @@ -19,6 +19,8 @@ class TrendingHashtagsViewController: EnhancedTableViewController { self.mastodonController = mastodonController super.init(style: .grouped) + + dragEnabled = true } required init?(coder: NSCoder) { @@ -33,8 +35,6 @@ class TrendingHashtagsViewController: EnhancedTableViewController { tableView.register(UINib(nibName: "TrendingHashtagTableViewCell", bundle: .main), forCellReuseIdentifier: "trendingTagCell") tableView.rowHeight = 60 // 44 for content + 2 * 8 spacing - // todo: enable drag - dataSource = UITableViewDiffableDataSource(tableView: tableView) { (tableView, indexPath, item) in switch item { case let .tag(hashtag): @@ -75,6 +75,31 @@ class TrendingHashtagsViewController: EnhancedTableViewController { 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 { @@ -85,3 +110,11 @@ extension TrendingHashtagsViewController { case tag(Hashtag) } } + +extension TrendingHashtagsViewController: TuskerNavigationDelegate { + var apiController: MastodonController { mastodonController } +} + +extension TrendingHashtagsViewController: MenuPreviewProvider { + var navigationDelegate: TuskerNavigationDelegate? { self } +}