2021-02-06 19:54:35 +00:00
|
|
|
//
|
|
|
|
// TrendingHashtagsViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 2/6/21.
|
|
|
|
// Copyright © 2021 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
2022-07-09 15:45:27 +00:00
|
|
|
import WebURLFoundationExtras
|
2021-02-06 19:54:35 +00:00
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
class TrendingHashtagsViewController: UIViewController {
|
2021-02-06 19:54:35 +00:00
|
|
|
|
|
|
|
weak var mastodonController: MastodonController!
|
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
private var collectionView: UICollectionView!
|
|
|
|
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
2021-02-06 19:54:35 +00:00
|
|
|
|
|
|
|
init(mastodonController: MastodonController) {
|
|
|
|
self.mastodonController = mastodonController
|
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
super.init(nibName: nil, bundle: nil)
|
2021-02-06 19:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
view.backgroundColor = .systemGroupedBackground
|
|
|
|
|
|
|
|
let config = UICollectionLayoutListConfiguration(appearance: .grouped)
|
|
|
|
let layout = UICollectionViewCompositionalLayout.list(using: config)
|
|
|
|
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
|
|
|
|
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
|
|
collectionView.delegate = self
|
|
|
|
collectionView.dragDelegate = self
|
2023-01-16 22:47:56 +00:00
|
|
|
collectionView.allowsFocus = true
|
2022-06-30 00:54:37 +00:00
|
|
|
view.addSubview(collectionView)
|
|
|
|
|
|
|
|
let registration = UICollectionView.CellRegistration<TrendingHashtagCollectionViewCell, Hashtag> { cell, indexPath, hashtag in
|
|
|
|
cell.updateUI(hashtag: hashtag)
|
|
|
|
}
|
2021-02-06 19:54:35 +00:00
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { (collectionView, indexPath, item) in
|
2021-02-06 19:54:35 +00:00
|
|
|
switch item {
|
|
|
|
case let .tag(hashtag):
|
2022-06-30 00:54:37 +00:00
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: registration, for: indexPath, item: hashtag)
|
2021-02-06 19:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2022-04-02 14:39:03 +00:00
|
|
|
let request = Client.getTrendingHashtags(limit: 10)
|
|
|
|
Task {
|
|
|
|
guard let (hashtags, _) = try? await mastodonController.run(request) else {
|
2021-02-06 19:54:35 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-02 14:39:03 +00:00
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
2021-02-06 19:54:35 +00:00
|
|
|
snapshot.appendSections([.trendingTags])
|
|
|
|
snapshot.appendItems(hashtags.map { .tag($0) })
|
2022-07-01 01:41:05 +00:00
|
|
|
await dataSource.apply(snapshot)
|
2021-02-06 19:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension TrendingHashtagsViewController {
|
|
|
|
enum Section {
|
|
|
|
case trendingTags
|
|
|
|
}
|
|
|
|
enum Item: Hashable {
|
|
|
|
case tag(Hashtag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TrendingHashtagsViewController: UICollectionViewDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
2021-02-06 19:54:35 +00:00
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath),
|
|
|
|
case let .tag(hashtag) = item else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
show(HashtagTimelineViewController(for: hashtag, mastodonController: mastodonController), sender: nil)
|
|
|
|
}
|
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
2021-11-24 20:12:23 +00:00
|
|
|
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
|
2022-11-30 03:41:36 +00:00
|
|
|
UIMenu(children: self.actionsForHashtag(hashtag, source: .view(self.collectionView.cellForItem(at: indexPath))))
|
2021-11-24 20:12:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-30 00:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension TrendingHashtagsViewController: UICollectionViewDragDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
|
2021-11-24 20:12:23 +00:00
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath),
|
2022-07-09 15:45:27 +00:00
|
|
|
case let .tag(hashtag) = item,
|
|
|
|
let url = URL(hashtag.url) else {
|
2021-11-24 20:12:23 +00:00
|
|
|
return []
|
|
|
|
}
|
2022-07-09 15:45:27 +00:00
|
|
|
let provider = NSItemProvider(object: url as NSURL)
|
2021-11-24 20:12:23 +00:00
|
|
|
if let activity = UserActivityManager.showTimelineActivity(timeline: .tag(hashtag: hashtag.name), accountID: mastodonController.accountInfo!.id) {
|
2022-05-13 21:10:18 +00:00
|
|
|
activity.displaysAuxiliaryScene = true
|
2021-11-24 20:12:23 +00:00
|
|
|
provider.registerObject(activity, visibility: .all)
|
|
|
|
}
|
|
|
|
return [UIDragItem(itemProvider: provider)]
|
|
|
|
}
|
2021-02-06 19:54:35 +00:00
|
|
|
}
|
2021-11-24 20:12:23 +00:00
|
|
|
|
|
|
|
extension TrendingHashtagsViewController: TuskerNavigationDelegate {
|
2022-10-31 20:27:13 +00:00
|
|
|
var apiController: MastodonController! { mastodonController }
|
2021-11-24 20:12:23 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 03:04:56 +00:00
|
|
|
extension TrendingHashtagsViewController: ToastableViewController {
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TrendingHashtagsViewController: MenuActionProvider {
|
2021-11-24 20:12:23 +00:00
|
|
|
}
|