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
|
2023-02-11 23:21:40 +00:00
|
|
|
import Combine
|
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
|
|
|
|
2023-02-11 23:21:40 +00:00
|
|
|
private var state = State.unloaded
|
|
|
|
private var confirmLoadMore = PassthroughSubject<Void, Never>()
|
|
|
|
|
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")
|
|
|
|
|
2023-02-11 23:21:40 +00:00
|
|
|
view.backgroundColor = .appGroupedBackground
|
2022-06-30 00:54:37 +00:00
|
|
|
|
2023-02-11 23:21:40 +00:00
|
|
|
var config = UICollectionLayoutListConfiguration(appearance: .grouped)
|
|
|
|
config.backgroundColor = .appGroupedBackground
|
|
|
|
config.itemSeparatorHandler = { [unowned self] indexPath, sectionConfig in
|
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath) else {
|
|
|
|
return sectionConfig
|
|
|
|
}
|
|
|
|
var config = sectionConfig
|
|
|
|
if item.hideSeparators {
|
|
|
|
config.topSeparatorVisibility = .hidden
|
|
|
|
config.bottomSeparatorVisibility = .hidden
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
2022-06-30 00:54:37 +00:00
|
|
|
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)
|
|
|
|
|
2023-02-11 23:21:40 +00:00
|
|
|
let loadingCell = UICollectionView.CellRegistration<LoadingCollectionViewCell, Void> { cell, indexPath, itemIdentifier in
|
|
|
|
cell.indicator.startAnimating()
|
|
|
|
}
|
|
|
|
let hashtagCell = UICollectionView.CellRegistration<TrendingHashtagCollectionViewCell, Hashtag> { cell, indexPath, hashtag in
|
2022-06-30 00:54:37 +00:00
|
|
|
cell.updateUI(hashtag: hashtag)
|
|
|
|
}
|
2023-02-11 23:21:40 +00:00
|
|
|
let confirmLoadMoreCell = UICollectionView.CellRegistration<ConfirmLoadMoreCollectionViewCell, Bool> { [unowned self] cell, indexPath, isLoading in
|
|
|
|
cell.confirmLoadMore = self.confirmLoadMore
|
|
|
|
cell.isLoading = isLoading
|
|
|
|
}
|
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 {
|
2023-02-11 23:21:40 +00:00
|
|
|
case .loadingIndicator:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: loadingCell, for: indexPath, item: ())
|
|
|
|
case .tag(let hashtag):
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: hashtagCell, for: indexPath, item: hashtag)
|
|
|
|
case .confirmLoadMore(let loading):
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: confirmLoadMoreCell, for: indexPath, item: loading)
|
2021-02-06 19:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2022-04-02 14:39:03 +00:00
|
|
|
Task {
|
2023-02-11 23:21:40 +00:00
|
|
|
await loadInitial()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func request(offset: Int?) -> Request<[Hashtag]> {
|
|
|
|
if mastodonController.instanceFeatures.hasMastodonVersion(3, 5, 0) {
|
|
|
|
return Client.getTrendingHashtags(offset: offset)
|
|
|
|
} else {
|
|
|
|
return Client.getTrendingHashtagsDeprecated(offset: offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private func loadInitial() async {
|
|
|
|
guard case .unloaded = state else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
state = .loadingInitial
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
snapshot.appendSections([.trendingTags])
|
|
|
|
snapshot.appendItems([.loadingIndicator])
|
|
|
|
await dataSource.apply(snapshot)
|
|
|
|
|
|
|
|
do {
|
|
|
|
let request = self.request(offset: nil)
|
|
|
|
let (hashtags, _) = try await mastodonController.run(request)
|
|
|
|
snapshot.deleteItems([.loadingIndicator])
|
|
|
|
snapshot.appendItems(hashtags.map { .tag($0) })
|
|
|
|
state = .loaded
|
|
|
|
await dataSource.apply(snapshot)
|
|
|
|
} catch {
|
|
|
|
snapshot.deleteItems([.loadingIndicator])
|
|
|
|
await dataSource.apply(snapshot)
|
|
|
|
state = .unloaded
|
|
|
|
|
|
|
|
let config = ToastConfiguration(from: error, with: "Error Loading Trending Tags", in: self) { [weak self] toast in
|
|
|
|
toast.dismissToast(animated: true)
|
|
|
|
await self?.loadInitial()
|
2021-02-06 19:54:35 +00:00
|
|
|
}
|
2023-02-11 23:21:40 +00:00
|
|
|
self.showToast(configuration: config, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private func loadOlder() async {
|
|
|
|
guard case .loaded = state else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
state = .loadingOlder
|
|
|
|
|
|
|
|
let origSnapshot = dataSource.snapshot()
|
|
|
|
var snapshot = origSnapshot
|
|
|
|
if Preferences.shared.disableInfiniteScrolling {
|
|
|
|
snapshot.appendItems([.confirmLoadMore(false)])
|
|
|
|
await dataSource.apply(snapshot)
|
|
|
|
|
|
|
|
for await _ in confirmLoadMore.values {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.deleteItems([.confirmLoadMore(false)])
|
|
|
|
snapshot.appendItems([.confirmLoadMore(true)])
|
|
|
|
await dataSource.apply(snapshot, animatingDifferences: false)
|
|
|
|
} else {
|
|
|
|
snapshot.appendItems([.loadingIndicator])
|
|
|
|
await dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
let request = self.request(offset: snapshot.itemIdentifiers.count - 1)
|
|
|
|
let (hashtags, _) = try await mastodonController.run(request)
|
|
|
|
var snapshot = origSnapshot
|
2021-02-06 19:54:35 +00:00
|
|
|
snapshot.appendItems(hashtags.map { .tag($0) })
|
2022-07-01 01:41:05 +00:00
|
|
|
await dataSource.apply(snapshot)
|
2023-02-11 23:21:40 +00:00
|
|
|
} catch {
|
|
|
|
await dataSource.apply(origSnapshot)
|
|
|
|
|
2023-02-11 23:47:39 +00:00
|
|
|
let config = ToastConfiguration(from: error, with: "Error Loading More Tags", in: self) { [weak self] toast in
|
2023-02-11 23:21:40 +00:00
|
|
|
toast.dismissToast(animated: true)
|
|
|
|
await self?.loadOlder()
|
|
|
|
}
|
|
|
|
self.showToast(configuration: config, animated: true)
|
2021-02-06 19:54:35 +00:00
|
|
|
}
|
2023-02-11 23:21:40 +00:00
|
|
|
|
|
|
|
state = .loaded
|
2021-02-06 19:54:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 23:21:40 +00:00
|
|
|
extension TrendingHashtagsViewController {
|
|
|
|
enum State {
|
|
|
|
case unloaded
|
|
|
|
case loadingInitial
|
|
|
|
case loaded
|
|
|
|
case loadingOlder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
extension TrendingHashtagsViewController {
|
|
|
|
enum Section {
|
|
|
|
case trendingTags
|
|
|
|
}
|
|
|
|
enum Item: Hashable {
|
2023-02-11 23:21:40 +00:00
|
|
|
case loadingIndicator
|
2022-06-30 00:54:37 +00:00
|
|
|
case tag(Hashtag)
|
2023-02-11 23:21:40 +00:00
|
|
|
case confirmLoadMore(Bool)
|
|
|
|
|
|
|
|
var hideSeparators: Bool {
|
|
|
|
switch self {
|
|
|
|
case .loadingIndicator:
|
|
|
|
return true
|
|
|
|
case .tag(_):
|
|
|
|
return false
|
|
|
|
case .confirmLoadMore(_):
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var shouldSelect: Bool {
|
|
|
|
if case .tag(_) = self {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-06-30 00:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TrendingHashtagsViewController: UICollectionViewDelegate {
|
2023-02-11 23:21:40 +00:00
|
|
|
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
|
|
|
|
if indexPath.section == collectionView.numberOfSections - 1,
|
|
|
|
indexPath.row == collectionView.numberOfItems(inSection: indexPath.section) - 1 {
|
|
|
|
Task {
|
|
|
|
await self.loadOlder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
|
|
|
return dataSource.itemIdentifier(for: indexPath)?.shouldSelect ?? false
|
|
|
|
}
|
|
|
|
|
2022-06-30 00:54:37 +00:00
|
|
|
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
|
|
|
}
|