Compare commits
No commits in common. "e40f4faa8e85b42b4d5267ebe6ee51d444323330" and "999118798c01976725aa674920b6d882cfcaa2c0" have entirely different histories.
e40f4faa8e
...
999118798c
|
@ -9,163 +9,77 @@
|
||||||
import UIKit
|
import UIKit
|
||||||
import Pachyderm
|
import Pachyderm
|
||||||
|
|
||||||
class TrendingStatusesViewController: UIViewController {
|
class TrendingStatusesViewController: EnhancedTableViewController {
|
||||||
|
|
||||||
weak var mastodonController: MastodonController!
|
weak var mastodonController: MastodonController!
|
||||||
|
|
||||||
private var collectionView: UICollectionView {
|
private var dataSource: UITableViewDiffableDataSource<Section, Item>!
|
||||||
view as! UICollectionView
|
|
||||||
}
|
|
||||||
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
|
||||||
|
|
||||||
init(mastodonController: MastodonController) {
|
init(mastodonController: MastodonController) {
|
||||||
self.mastodonController = mastodonController
|
self.mastodonController = mastodonController
|
||||||
|
|
||||||
super.init(nibName: nil, bundle: nil)
|
super.init(style: .grouped)
|
||||||
|
|
||||||
title = NSLocalizedString("Trending Posts", comment: "trending posts screen title")
|
dragEnabled = true
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func loadView() {
|
override func viewDidLoad() {
|
||||||
var config = UICollectionLayoutListConfiguration(appearance: .plain)
|
super.viewDidLoad()
|
||||||
config.leadingSwipeActionsConfigurationProvider = { [unowned self] in
|
|
||||||
(collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.leadingSwipeActions()
|
|
||||||
}
|
|
||||||
config.trailingSwipeActionsConfigurationProvider = { [unowned self] in
|
|
||||||
(collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.trailingSwipeActions()
|
|
||||||
}
|
|
||||||
config.itemSeparatorHandler = { [unowned self] indexPath, sectionSeparatorConfiguration in
|
|
||||||
guard let item = self.dataSource.itemIdentifier(for: indexPath) else {
|
|
||||||
return sectionSeparatorConfiguration
|
|
||||||
}
|
|
||||||
var config = sectionSeparatorConfiguration
|
|
||||||
if item.hideSeparators {
|
|
||||||
config.topSeparatorVisibility = .hidden
|
|
||||||
config.bottomSeparatorVisibility = .hidden
|
|
||||||
}
|
|
||||||
if case .status(_, _) = item {
|
|
||||||
config.topSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
|
|
||||||
config.bottomSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
|
|
||||||
}
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
let layout = UICollectionViewCompositionalLayout.list(using: config)
|
|
||||||
view = UICollectionView(frame: .zero, collectionViewLayout: layout)
|
|
||||||
collectionView.delegate = self
|
|
||||||
collectionView.dragDelegate = self
|
|
||||||
|
|
||||||
dataSource = createDataSource()
|
title = NSLocalizedString("Trending Posts", comment: "trending posts screen title")
|
||||||
}
|
|
||||||
|
|
||||||
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
|
tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: "statusCell")
|
||||||
let statusCell = UICollectionView.CellRegistration<TimelineStatusCollectionViewCell, (String, StatusState)> { [unowned self] cell, indexPath, item in
|
tableView.estimatedRowHeight = 144
|
||||||
|
|
||||||
|
dataSource = UITableViewDiffableDataSource(tableView: tableView, cellProvider: { tableView, indexPath, item in
|
||||||
|
let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as! TimelineStatusTableViewCell
|
||||||
cell.delegate = self
|
cell.delegate = self
|
||||||
cell.updateUI(statusID: item.0, state: item.1)
|
cell.updateUI(statusID: item.id, state: item.state)
|
||||||
}
|
return cell
|
||||||
let loadingCell = UICollectionView.CellRegistration<LoadingCollectionViewCell, Void> { cell, _, _ in
|
})
|
||||||
cell.indicator.startAnimating()
|
|
||||||
}
|
|
||||||
return UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in
|
|
||||||
switch itemIdentifier {
|
|
||||||
case .status(id: let id, state: let state):
|
|
||||||
return collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: (id, state))
|
|
||||||
case .loadingIndicator:
|
|
||||||
return collectionView.dequeueConfiguredReusableCell(using: loadingCell, for: indexPath, item: ())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewWillAppear(_ animated: Bool) {
|
override func viewWillAppear(_ animated: Bool) {
|
||||||
super.viewWillAppear(animated)
|
super.viewWillAppear(animated)
|
||||||
|
|
||||||
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
let request = Client.getTrendingStatuses()
|
||||||
snapshot.appendSections([.statuses])
|
|
||||||
snapshot.appendItems([.loadingIndicator])
|
|
||||||
dataSource.apply(snapshot, animatingDifferences: false)
|
|
||||||
|
|
||||||
Task {
|
Task {
|
||||||
await loadTrendingStatuses()
|
guard let (statuses, _) = try? await mastodonController.run(request) else {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func loadTrendingStatuses() async {
|
|
||||||
let statuses: [Status]
|
|
||||||
do {
|
|
||||||
statuses = try await mastodonController.run(Client.getTrendingStatuses()).0
|
|
||||||
} catch {
|
|
||||||
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
||||||
await dataSource.apply(snapshot)
|
|
||||||
let config = ToastConfiguration(from: error, with: "Loading Trending Posts", in: self) { toast in
|
|
||||||
toast.dismissToast(animated: true)
|
|
||||||
await self.loadTrendingStatuses()
|
|
||||||
}
|
|
||||||
showToast(configuration: config, animated: true)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
await mastodonController.persistentContainer.addAll(statuses: statuses)
|
mastodonController.persistentContainer.addAll(statuses: statuses) {
|
||||||
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
||||||
snapshot.appendSections([.statuses])
|
snapshot.appendSections([.statuses])
|
||||||
snapshot.appendItems(statuses.map { .status(id: $0.id, state: .unknown) })
|
snapshot.appendItems(statuses.map { Item(id: $0.id, state: .unknown) })
|
||||||
await dataSource.apply(snapshot)
|
self.dataSource.apply(snapshot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Table View Delegate
|
||||||
|
}
|
||||||
|
|
||||||
extension TrendingStatusesViewController {
|
extension TrendingStatusesViewController {
|
||||||
enum Section {
|
enum Section {
|
||||||
case statuses
|
case statuses
|
||||||
}
|
}
|
||||||
enum Item: Hashable {
|
struct Item: Hashable {
|
||||||
case status(id: String, state: StatusState)
|
let id: String
|
||||||
case loadingIndicator
|
let state: StatusState
|
||||||
|
|
||||||
var hideSeparators: Bool {
|
static func ==(lhs: Item, rhs: Item) -> Bool {
|
||||||
if case .loadingIndicator = self {
|
return lhs.id == rhs.id
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var isSelectable: Bool {
|
func hash(into hasher: inout Hasher) {
|
||||||
if case .status(id: _, state: _) = self {
|
hasher.combine(id)
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
extension TrendingStatusesViewController: UICollectionViewDelegate {
|
|
||||||
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
|
||||||
return dataSource.itemIdentifier(for: indexPath)?.isSelectable ?? false
|
|
||||||
}
|
|
||||||
|
|
||||||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
||||||
guard case .status(id: let id, state: let state) = dataSource.itemIdentifier(for: indexPath) else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
selected(status: id, state: state.copy())
|
|
||||||
}
|
|
||||||
|
|
||||||
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
|
||||||
return (collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.contextMenuConfiguration()
|
|
||||||
}
|
|
||||||
|
|
||||||
func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
||||||
MenuPreviewHelper.willPerformPreviewAction(animator: animator, presenter: self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension TrendingStatusesViewController: UICollectionViewDragDelegate {
|
|
||||||
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
|
|
||||||
(collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.dragItemsForBeginning(session: session) ?? []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension TrendingStatusesViewController: TuskerNavigationDelegate {
|
extension TrendingStatusesViewController: TuskerNavigationDelegate {
|
||||||
var apiController: MastodonController! { mastodonController }
|
var apiController: MastodonController! { mastodonController }
|
||||||
|
@ -177,19 +91,9 @@ extension TrendingStatusesViewController: ToastableViewController {
|
||||||
extension TrendingStatusesViewController: MenuActionProvider {
|
extension TrendingStatusesViewController: MenuActionProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension TrendingStatusesViewController: StatusCollectionViewCellDelegate {
|
extension TrendingStatusesViewController: StatusTableViewCellDelegate {
|
||||||
func statusCellNeedsReconfigure(_ cell: StatusCollectionViewCell, animated: Bool, completion: (() -> Void)?) {
|
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell) {
|
||||||
if let indexPath = collectionView.indexPath(for: cell) {
|
tableView.beginUpdates()
|
||||||
var snapshot = dataSource.snapshot()
|
tableView.endUpdates()
|
||||||
snapshot.reconfigureItems([dataSource.itemIdentifier(for: indexPath)!])
|
|
||||||
dataSource.apply(snapshot, animatingDifferences: animated, completion: completion)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension TrendingStatusesViewController: StatusBarTappableViewController {
|
|
||||||
func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult {
|
|
||||||
collectionView.scrollToTop()
|
|
||||||
return .stop
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,11 +86,6 @@ class ProfileHeaderView: UIView {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createObservers() {
|
private func createObservers() {
|
||||||
// mastodonController may be nil if the ProfileViewController is deinit'd before the header is even created
|
|
||||||
guard let mastodonController else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cancellables = []
|
cancellables = []
|
||||||
|
|
||||||
mastodonController.persistentContainer.accountSubject
|
mastodonController.persistentContainer.accountSubject
|
||||||
|
|
Loading…
Reference in New Issue