2022-09-24 14:49:06 +00:00
|
|
|
//
|
|
|
|
// TimelineViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/20/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
|
|
|
import Combine
|
|
|
|
|
2022-10-08 18:53:21 +00:00
|
|
|
class TimelineViewController: UIViewController, TimelineLikeCollectionViewController, RefreshableViewController {
|
2022-09-24 14:49:06 +00:00
|
|
|
let timeline: Timeline
|
|
|
|
weak var mastodonController: MastodonController!
|
|
|
|
|
2022-09-24 15:32:50 +00:00
|
|
|
private(set) var controller: TimelineLikeController<TimelineItem>!
|
|
|
|
let confirmLoadMore = PassthroughSubject<Void, Never>()
|
2022-10-01 19:32:06 +00:00
|
|
|
// stored separately because i don't want to query the snapshot every time the user scrolls
|
|
|
|
private var isShowingTimelineDescription = false
|
2022-09-24 14:49:06 +00:00
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
private(set) var collectionView: UICollectionView!
|
2022-09-24 15:32:50 +00:00
|
|
|
private(set) var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
2022-09-24 14:49:06 +00:00
|
|
|
|
2022-11-19 16:15:14 +00:00
|
|
|
private var contentOffsetObservation: NSKeyValueObservation?
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
init(for timeline: Timeline, mastodonController: MastodonController!) {
|
|
|
|
self.timeline = timeline
|
|
|
|
self.mastodonController = mastodonController
|
|
|
|
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
|
|
|
|
self.controller = TimelineLikeController(delegate: self)
|
2022-10-08 18:53:21 +00:00
|
|
|
|
2022-11-02 01:06:06 +00:00
|
|
|
self.navigationItem.title = timeline.title
|
2022-10-08 18:53:21 +00:00
|
|
|
addKeyCommand(MenuController.refreshCommand(discoverabilityTitle: "Refresh Timeline"))
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
var config = UICollectionLayoutListConfiguration(appearance: .plain)
|
2022-10-08 18:33:07 +00:00
|
|
|
config.leadingSwipeActionsConfigurationProvider = { [unowned self] in
|
|
|
|
(collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.leadingSwipeActions()
|
|
|
|
}
|
|
|
|
config.trailingSwipeActionsConfigurationProvider = { [unowned self] in
|
|
|
|
(collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.trailingSwipeActions()
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
config.itemSeparatorHandler = { [unowned self] indexPath, sectionSeparatorConfiguration in
|
2022-10-03 01:34:06 +00:00
|
|
|
guard let item = self.dataSource.itemIdentifier(for: indexPath) else {
|
|
|
|
return sectionSeparatorConfiguration
|
|
|
|
}
|
|
|
|
var config = sectionSeparatorConfiguration
|
|
|
|
if item.hideSeparators {
|
2022-09-24 14:49:06 +00:00
|
|
|
config.topSeparatorVisibility = .hidden
|
|
|
|
config.bottomSeparatorVisibility = .hidden
|
2022-11-19 16:15:14 +00:00
|
|
|
} else {
|
2022-10-29 01:14:46 +00:00
|
|
|
config.topSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
|
|
|
|
config.bottomSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
return config
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
let layout = UICollectionViewCompositionalLayout.list(using: config)
|
2022-11-19 01:49:15 +00:00
|
|
|
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
|
2022-09-24 14:49:06 +00:00
|
|
|
collectionView.delegate = self
|
2022-10-08 18:59:50 +00:00
|
|
|
collectionView.dragDelegate = self
|
2022-11-19 01:49:15 +00:00
|
|
|
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(collectionView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
collectionView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
])
|
2022-09-24 14:49:06 +00:00
|
|
|
|
2022-10-01 19:08:51 +00:00
|
|
|
registerTimelineLikeCells()
|
2022-10-01 19:32:06 +00:00
|
|
|
collectionView.register(PublicTimelineDescriptionCollectionViewCell.self, forCellWithReuseIdentifier: "publicTimelineDescription")
|
2022-09-24 14:49:06 +00:00
|
|
|
dataSource = createDataSource()
|
|
|
|
applyInitialSnapshot()
|
|
|
|
|
2022-09-24 15:32:50 +00:00
|
|
|
#if !targetEnvironment(macCatalyst)
|
2022-10-08 18:53:21 +00:00
|
|
|
collectionView.refreshControl = UIRefreshControl()
|
|
|
|
collectionView.refreshControl!.addTarget(self, action: #selector(refresh), for: .valueChanged)
|
2022-09-24 15:32:50 +00:00
|
|
|
#endif
|
2022-11-18 22:29:55 +00:00
|
|
|
|
|
|
|
#if DEBUG
|
2022-11-19 16:15:14 +00:00
|
|
|
let split = UIBarButtonItem(image: UIImage(systemName: "square.split.1x2.fill"), primaryAction: UIAction(handler: { [unowned self] _ in
|
|
|
|
var snapshot = self.dataSource.snapshot()
|
|
|
|
let statuses = snapshot.itemIdentifiers(inSection: .statuses)
|
|
|
|
if statuses.count > 20 {
|
|
|
|
let toRemove = Array(Array(statuses.dropFirst(10)).dropLast(10))
|
|
|
|
if !toRemove.isEmpty {
|
|
|
|
print("REMOVING MIDDLE \(toRemove.count) STATUSES")
|
|
|
|
snapshot.insertItems([.gap], beforeItem: toRemove.first!)
|
|
|
|
snapshot.deleteItems(toRemove)
|
|
|
|
self.dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
let rewind = UIBarButtonItem(image: UIImage(systemName: "clock.arrow.circlepath"), primaryAction: UIAction(handler: { [unowned self] _ in
|
2022-11-18 22:29:55 +00:00
|
|
|
var snapshot = self.dataSource.snapshot()
|
|
|
|
let statuses = snapshot.itemIdentifiers(inSection: .statuses)
|
|
|
|
if statuses.count > 20 {
|
2022-11-19 01:49:15 +00:00
|
|
|
let toRemove = Array(statuses.dropLast(20))
|
|
|
|
snapshot.deleteItems(toRemove)
|
|
|
|
self.dataSource.apply(snapshot)
|
2022-11-18 22:29:55 +00:00
|
|
|
}
|
|
|
|
}))
|
2022-11-19 16:15:14 +00:00
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
navigationItem.trailingItemGroups = [
|
|
|
|
UIBarButtonItemGroup(barButtonItems: [split, rewind], representativeItem: nil)
|
|
|
|
]
|
|
|
|
}
|
2022-11-18 22:29:55 +00:00
|
|
|
#else
|
|
|
|
#error("remove me")
|
|
|
|
#endif
|
2022-11-19 16:15:14 +00:00
|
|
|
|
|
|
|
contentOffsetObservation = collectionView.observe(\.contentOffset) { [weak self] _, _ in
|
|
|
|
if let indexPath = self?.dataSource.indexPath(for: .gap),
|
|
|
|
let cell = self?.collectionView.cellForItem(at: indexPath) as? TimelineGapCollectionViewCell {
|
|
|
|
cell.update()
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 01:06:06 +00:00
|
|
|
// separate method because InstanceTimelineViewController needs to be able to customize it
|
|
|
|
func configureStatusCell(_ cell: TimelineStatusCollectionViewCell, id: String, state: StatusState) {
|
|
|
|
cell.delegate = self
|
|
|
|
cell.updateUI(statusID: id, state: state)
|
|
|
|
}
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
|
2022-10-11 01:01:03 +00:00
|
|
|
let statusCell = UICollectionView.CellRegistration<TimelineStatusCollectionViewCell, (String, StatusState)> { [unowned self] cell, indexPath, item in
|
2022-11-02 01:06:06 +00:00
|
|
|
self.configureStatusCell(cell, id: item.0, state: item.1)
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
2022-11-19 16:15:14 +00:00
|
|
|
let gapCell = UICollectionView.CellRegistration<TimelineGapCollectionViewCell, Void> { cell, indexPath, _ in
|
|
|
|
cell.showsIndicator = false
|
2022-11-18 22:29:55 +00:00
|
|
|
}
|
2022-10-01 19:32:06 +00:00
|
|
|
let timelineDescriptionCell = UICollectionView.CellRegistration<PublicTimelineDescriptionCollectionViewCell, Item> { [unowned self] cell, indexPath, item in
|
|
|
|
guard case .public(let local) = timeline else {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
cell.mastodonController = self.mastodonController
|
|
|
|
cell.local = local
|
|
|
|
cell.didDismiss = { [unowned self] in
|
|
|
|
self.removeTimelineDescriptionCell()
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
return UICollectionViewDiffableDataSource(collectionView: collectionView) { [unowned self] collectionView, indexPath, itemIdentifier in
|
|
|
|
switch itemIdentifier {
|
2022-10-11 01:01:03 +00:00
|
|
|
case .status(id: let id, state: let state):
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: (id, state))
|
2022-11-18 22:29:55 +00:00
|
|
|
case .gap:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: gapCell, for: indexPath, item: ())
|
2022-09-24 14:49:06 +00:00
|
|
|
case .loadingIndicator:
|
2022-10-01 19:08:51 +00:00
|
|
|
return loadingIndicatorCell(for: indexPath)
|
2022-09-24 14:49:06 +00:00
|
|
|
case .confirmLoadMore:
|
2022-10-01 19:08:51 +00:00
|
|
|
return confirmLoadMoreCell(for: indexPath)
|
2022-10-01 19:32:06 +00:00
|
|
|
case .publicTimelineDescription:
|
|
|
|
self.isShowingTimelineDescription = true
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: timelineDescriptionCell, for: indexPath, item: itemIdentifier)
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 01:06:06 +00:00
|
|
|
// non-private, because ListTimelineViewController needs to be able to reload it from scratch
|
|
|
|
func applyInitialSnapshot() {
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
2022-10-01 19:32:06 +00:00
|
|
|
if case .public(let local) = timeline,
|
|
|
|
(local && !Preferences.shared.hasShownLocalTimelineDescription) ||
|
2022-10-09 23:11:34 +00:00
|
|
|
(!local && !Preferences.shared.hasShownFederatedTimelineDescription) {
|
2022-10-01 19:32:06 +00:00
|
|
|
snapshot.appendSections([.header])
|
|
|
|
snapshot.appendItems([.publicTimelineDescription], toSection: .header)
|
|
|
|
}
|
2022-11-02 01:06:06 +00:00
|
|
|
dataSource.apply(snapshot, animatingDifferences: false)
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
collectionView.indexPathsForSelectedItems?.forEach {
|
|
|
|
collectionView.deselectItem(at: $0, animated: true)
|
|
|
|
}
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
Task {
|
2022-11-19 01:49:15 +00:00
|
|
|
if case .notLoadedInitial = controller.state {
|
2022-11-02 01:52:28 +00:00
|
|
|
await controller.loadInitial()
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 23:11:34 +00:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
if isShowingTimelineDescription,
|
|
|
|
case .public(let local) = timeline {
|
|
|
|
if local {
|
|
|
|
Preferences.shared.hasShownLocalTimelineDescription = true
|
|
|
|
} else {
|
|
|
|
Preferences.shared.hasShownFederatedTimelineDescription = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 21:06:10 +00:00
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
|
2022-10-28 22:56:18 +00:00
|
|
|
// pruneOffscreenRows()
|
2022-10-09 21:06:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 19:32:06 +00:00
|
|
|
private func removeTimelineDescriptionCell() {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.deleteSections([.header])
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: true)
|
|
|
|
isShowingTimelineDescription = false
|
|
|
|
}
|
|
|
|
|
2022-10-28 22:56:18 +00:00
|
|
|
// private func pruneOffscreenRows() {
|
|
|
|
// guard let lastVisibleIndexPath = collectionView.indexPathsForVisibleItems.last else {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// var snapshot = dataSource.snapshot()
|
|
|
|
// guard snapshot.indexOfSection(.statuses) != nil else {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// let items = snapshot.itemIdentifiers(inSection: .statuses)
|
|
|
|
// let pageSize = 20
|
|
|
|
// let numberOfPagesToPrune = (items.count - lastVisibleIndexPath.row - 1) / pageSize
|
|
|
|
// if numberOfPagesToPrune > 0 {
|
|
|
|
// let itemsToRemove = Array(items.suffix(numberOfPagesToPrune * pageSize))
|
|
|
|
// snapshot.deleteItems(itemsToRemove)
|
|
|
|
//
|
|
|
|
// dataSource.apply(snapshot, animatingDifferences: false)
|
|
|
|
//
|
|
|
|
// if case .status(id: let id, state: _) = snapshot.itemIdentifiers(inSection: .statuses).last {
|
|
|
|
// older = .before(id: id, count: nil)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2022-10-10 00:11:00 +00:00
|
|
|
|
2022-10-08 18:53:21 +00:00
|
|
|
@objc func refresh() {
|
|
|
|
Task {
|
2022-11-19 01:49:15 +00:00
|
|
|
if case .notLoadedInitial = controller.state {
|
2022-11-10 00:15:08 +00:00
|
|
|
await controller.loadInitial()
|
|
|
|
} else {
|
2022-11-19 01:49:15 +00:00
|
|
|
// I'm not sure whether this should move into TimelineLikeController/TimelineLikeCollectionViewController
|
|
|
|
let (_, presentItems) = await (controller.loadNewer(), try? loadInitial())
|
|
|
|
if let presentItems,
|
|
|
|
case .status(id: let id, state: _) = dataSource.snapshot().itemIdentifiers(inSection: .statuses).first {
|
|
|
|
// if there's no overlap between presentItems and the existing items in the data source, prompt the user to scroll to present
|
|
|
|
if !presentItems.contains(id) {
|
|
|
|
var snapshot = self.dataSource.snapshot()
|
|
|
|
let currentItems = snapshot.itemIdentifiers(inSection: .statuses)
|
|
|
|
guard let item = currentItems.first,
|
|
|
|
case .status(id: id, state: _) = item else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove any existing gap if there is one
|
|
|
|
if let index = currentItems.lastIndex(of: .gap) {
|
|
|
|
snapshot.deleteItems(Array(currentItems[index...]))
|
|
|
|
}
|
|
|
|
snapshot.insertItems([.gap], beforeItem: item)
|
|
|
|
snapshot.insertItems(presentItems.map { .status(id: $0, state: .unknown) }, beforeItem: .gap)
|
|
|
|
|
|
|
|
// use a snapshot of the collection view to hide the flicker as the content offset changes and then changes back
|
|
|
|
let snapshotView = collectionView.snapshotView(afterScreenUpdates: false)!
|
|
|
|
snapshotView.layer.zPosition = 1000
|
|
|
|
snapshotView.frame = view.bounds
|
|
|
|
view.addSubview(snapshotView)
|
|
|
|
|
|
|
|
let bottomOffset = collectionView.contentSize.height - collectionView.contentOffset.y
|
|
|
|
self.dataSource.apply(snapshot, animatingDifferences: false) {
|
|
|
|
self.collectionView.contentOffset = CGPoint(x: 0, y: self.collectionView.contentSize.height - bottomOffset)
|
|
|
|
snapshotView.removeFromSuperview()
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = ToastConfiguration(title: "Jump to present")
|
|
|
|
config.edge = .top
|
|
|
|
config.systemImageName = "arrow.up"
|
2022-11-19 16:15:14 +00:00
|
|
|
config.dismissAutomaticallyAfter = 4
|
2022-11-19 01:49:15 +00:00
|
|
|
config.action = { [unowned self] toast in
|
|
|
|
toast.dismissToast(animated: true)
|
|
|
|
|
|
|
|
self.collectionView.scrollToTop()
|
|
|
|
}
|
|
|
|
self.showToast(configuration: config, animated: true)
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 00:15:08 +00:00
|
|
|
}
|
2022-10-08 18:53:21 +00:00
|
|
|
#if !targetEnvironment(macCatalyst)
|
|
|
|
collectionView.refreshControl?.endRefreshing()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension TimelineViewController {
|
2022-09-24 15:32:50 +00:00
|
|
|
enum Section: TimelineLikeCollectionViewSection {
|
2022-09-24 14:49:06 +00:00
|
|
|
case header
|
|
|
|
case statuses
|
|
|
|
case footer
|
2022-09-24 15:32:50 +00:00
|
|
|
|
|
|
|
static var entries: Self { .statuses }
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
2022-09-24 15:32:50 +00:00
|
|
|
enum Item: TimelineLikeCollectionViewItem {
|
|
|
|
typealias TimelineItem = String // status ID
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
case status(id: String, state: StatusState)
|
2022-11-18 22:29:55 +00:00
|
|
|
case gap
|
2022-09-24 14:49:06 +00:00
|
|
|
case loadingIndicator
|
|
|
|
case confirmLoadMore
|
2022-10-01 19:32:06 +00:00
|
|
|
case publicTimelineDescription
|
2022-09-24 14:49:06 +00:00
|
|
|
|
2022-09-24 15:32:50 +00:00
|
|
|
static func fromTimelineItem(_ id: String) -> Self {
|
|
|
|
return .status(id: id, state: .unknown)
|
|
|
|
}
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
static func ==(lhs: Item, rhs: Item) -> Bool {
|
|
|
|
switch (lhs, rhs) {
|
|
|
|
case let (.status(id: a, state: _), .status(id: b, state: _)):
|
|
|
|
return a == b
|
2022-11-18 22:29:55 +00:00
|
|
|
case (.gap, .gap):
|
|
|
|
return true
|
2022-09-24 14:49:06 +00:00
|
|
|
case (.loadingIndicator, .loadingIndicator):
|
|
|
|
return true
|
|
|
|
case (.confirmLoadMore, .confirmLoadMore):
|
|
|
|
return true
|
2022-10-01 19:32:06 +00:00
|
|
|
case (.publicTimelineDescription, .publicTimelineDescription):
|
|
|
|
return true
|
2022-09-24 14:49:06 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
switch self {
|
|
|
|
case .status(id: let id, state: _):
|
|
|
|
hasher.combine(0)
|
|
|
|
hasher.combine(id)
|
2022-11-18 22:29:55 +00:00
|
|
|
case .gap:
|
2022-09-24 14:49:06 +00:00
|
|
|
hasher.combine(1)
|
2022-11-18 22:29:55 +00:00
|
|
|
case .loadingIndicator:
|
2022-09-24 14:49:06 +00:00
|
|
|
hasher.combine(2)
|
2022-11-18 22:29:55 +00:00
|
|
|
case .confirmLoadMore:
|
2022-10-01 19:32:06 +00:00
|
|
|
hasher.combine(3)
|
2022-11-18 22:29:55 +00:00
|
|
|
case .publicTimelineDescription:
|
|
|
|
hasher.combine(4)
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var hideSeparators: Bool {
|
|
|
|
switch self {
|
2022-10-29 01:14:46 +00:00
|
|
|
case .loadingIndicator, .publicTimelineDescription, .confirmLoadMore:
|
2022-09-24 14:49:06 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-10-06 02:28:10 +00:00
|
|
|
|
|
|
|
var isSelectable: Bool {
|
|
|
|
switch self {
|
2022-11-19 16:15:14 +00:00
|
|
|
case .publicTimelineDescription, .gap, .status(id: _, state: _):
|
2022-10-06 02:28:10 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 19:08:51 +00:00
|
|
|
// MARK: TimelineLikeControllerDelegate
|
2022-09-24 15:32:50 +00:00
|
|
|
extension TimelineViewController {
|
2022-09-24 14:49:06 +00:00
|
|
|
typealias TimelineItem = String // status ID
|
|
|
|
|
|
|
|
func loadInitial() async throws -> [TimelineItem] {
|
2022-11-10 00:15:08 +00:00
|
|
|
try await Task.sleep(nanoseconds: 1 * NSEC_PER_SEC)
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
let request = Client.getStatuses(timeline: timeline)
|
|
|
|
let (statuses, _) = try await mastodonController.run(request)
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
await withCheckedContinuation { continuation in
|
2022-09-24 14:49:06 +00:00
|
|
|
mastodonController.persistentContainer.addAll(statuses: statuses) {
|
2022-11-19 01:49:15 +00:00
|
|
|
continuation.resume()
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-19 01:49:15 +00:00
|
|
|
|
|
|
|
return statuses.map(\.id)
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadNewer() async throws -> [TimelineItem] {
|
2022-11-19 01:49:15 +00:00
|
|
|
let statusesSection = dataSource.snapshot().indexOfSection(.statuses)!
|
|
|
|
guard case .status(id: let id, state: _) = dataSource.itemIdentifier(for: IndexPath(row: 0, section: statusesSection)) else {
|
2022-09-24 14:49:06 +00:00
|
|
|
throw Error.noNewer
|
|
|
|
}
|
2022-11-19 01:49:15 +00:00
|
|
|
let newer = RequestRange.after(id: id, count: nil)
|
2022-09-24 14:49:06 +00:00
|
|
|
|
|
|
|
let request = Client.getStatuses(timeline: timeline, range: newer)
|
|
|
|
let (statuses, _) = try await mastodonController.run(request)
|
|
|
|
|
|
|
|
guard !statuses.isEmpty else {
|
2022-11-19 01:49:15 +00:00
|
|
|
throw TimelineViewController.Error.allCaughtUp
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
await withCheckedContinuation { continuation in
|
2022-09-24 14:49:06 +00:00
|
|
|
mastodonController.persistentContainer.addAll(statuses: statuses) {
|
2022-11-19 01:49:15 +00:00
|
|
|
continuation.resume()
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-19 01:49:15 +00:00
|
|
|
|
|
|
|
return statuses.map(\.id)
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadOlder() async throws -> [TimelineItem] {
|
2022-11-19 01:49:15 +00:00
|
|
|
let snapshot = dataSource.snapshot()
|
|
|
|
let statusesSection = snapshot.indexOfSection(.statuses)!
|
|
|
|
guard case .status(id: let id, state: _) = dataSource.itemIdentifier(for: IndexPath(row: snapshot.numberOfItems(inSection: .statuses) - 1, section: statusesSection)) else {
|
|
|
|
throw Error.noNewer
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
2022-11-19 01:49:15 +00:00
|
|
|
let older = RequestRange.before(id: id, count: nil)
|
2022-09-24 14:49:06 +00:00
|
|
|
|
|
|
|
let request = Client.getStatuses(timeline: timeline, range: older)
|
|
|
|
let (statuses, _) = try await mastodonController.run(request)
|
|
|
|
|
2022-10-11 01:01:03 +00:00
|
|
|
guard !statuses.isEmpty else {
|
|
|
|
return []
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
await withCheckedContinuation { continuation in
|
2022-09-24 14:49:06 +00:00
|
|
|
mastodonController.persistentContainer.addAll(statuses: statuses) {
|
2022-11-19 01:49:15 +00:00
|
|
|
continuation.resume()
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-19 01:49:15 +00:00
|
|
|
|
|
|
|
return statuses.map(\.id)
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
func loadGap(in direction: TimelineGapDirection) async throws -> [TimelineItem] {
|
2022-11-18 22:29:55 +00:00
|
|
|
guard let gapIndexPath = dataSource.indexPath(for: .gap) else {
|
|
|
|
throw Error.noGap
|
|
|
|
}
|
|
|
|
let statusItemsCount = collectionView.numberOfItems(inSection: gapIndexPath.section)
|
|
|
|
let range: RequestRange
|
|
|
|
switch direction {
|
|
|
|
case .above:
|
|
|
|
guard gapIndexPath.row > 0,
|
|
|
|
case .status(id: let id, state: _) = dataSource.itemIdentifier(for: IndexPath(row: gapIndexPath.row - 1, section: gapIndexPath.section)) else {
|
|
|
|
// not really the right error but w/e
|
|
|
|
throw Error.noGap
|
|
|
|
}
|
|
|
|
range = .before(id: id, count: nil)
|
|
|
|
case .below:
|
|
|
|
guard gapIndexPath.row < statusItemsCount - 1,
|
|
|
|
case .status(id: let id, state: _) = dataSource.itemIdentifier(for: IndexPath(row: gapIndexPath.row + 1, section: gapIndexPath.section)) else {
|
|
|
|
throw Error.noGap
|
|
|
|
}
|
|
|
|
range = .after(id: id, count: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
let request = Client.getStatuses(timeline: timeline, range: range)
|
|
|
|
let (statuses, _) = try await mastodonController.run(request)
|
|
|
|
|
|
|
|
guard !statuses.isEmpty else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: closing the gap (if necessary) happens in handleFillGap
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
await withCheckedContinuation { continuation in
|
2022-11-18 22:29:55 +00:00
|
|
|
mastodonController.persistentContainer.addAll(statuses: statuses) {
|
2022-11-19 01:49:15 +00:00
|
|
|
continuation.resume()
|
2022-11-18 22:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-19 01:49:15 +00:00
|
|
|
|
|
|
|
return statuses.map(\.id)
|
2022-11-18 22:29:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
func handleLoadGapError(_ error: Swift.Error, direction: TimelineGapDirection) async {
|
2022-11-18 22:29:55 +00:00
|
|
|
// TODO: better title, involving direction?
|
|
|
|
let config = ToastConfiguration(from: error, with: "Error Loading", in: self) { [weak self] toast in
|
|
|
|
toast.dismissToast(animated: true)
|
|
|
|
Task {
|
|
|
|
await self?.controller.fillGap(in: direction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.showToast(configuration: config, animated: true)
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
func handleFillGap(_ timelineItems: [String], direction: TimelineGapDirection) async {
|
2022-11-18 22:29:55 +00:00
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
let addedItems: Bool
|
|
|
|
|
|
|
|
let statusItems = snapshot.itemIdentifiers(inSection: .statuses)
|
|
|
|
let gapIndex = statusItems.firstIndex(of: .gap)!
|
|
|
|
|
|
|
|
switch direction {
|
|
|
|
case .above:
|
|
|
|
// dropFirst to remove .gap item
|
|
|
|
let afterGap = statusItems[gapIndex...].dropFirst().prefix(20)
|
|
|
|
precondition(!afterGap.contains(.gap))
|
|
|
|
|
|
|
|
// if there is any overlap, the first overlapping item will be the first item below the gap
|
|
|
|
var indexOfFirstTimelineItemExistingBelowGap: Int?
|
|
|
|
if case .status(id: let id, state: _) = afterGap.first {
|
|
|
|
indexOfFirstTimelineItemExistingBelowGap = timelineItems.firstIndex(of: id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the end index of the range of timelineItems that don't yet exist in the data source
|
|
|
|
let endIndex = indexOfFirstTimelineItemExistingBelowGap ?? timelineItems.endIndex
|
|
|
|
let toInsert = timelineItems[..<endIndex].map { Item.status(id: $0, state: .unknown) }
|
|
|
|
if toInsert.isEmpty {
|
|
|
|
addedItems = false
|
|
|
|
} else {
|
|
|
|
snapshot.insertItems(toInsert, beforeItem: .gap)
|
|
|
|
addedItems = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there's any overlap between the items we loaded to insert above the gap
|
|
|
|
// and the items that already exist below the gap, we've completely filled the gap
|
|
|
|
if indexOfFirstTimelineItemExistingBelowGap != nil {
|
|
|
|
snapshot.deleteItems([.gap])
|
|
|
|
}
|
2022-11-19 16:15:14 +00:00
|
|
|
|
|
|
|
await apply(snapshot, animatingDifferences: !addedItems)
|
2022-11-18 22:29:55 +00:00
|
|
|
|
|
|
|
case .below:
|
|
|
|
let beforeGap = statusItems[..<gapIndex].suffix(20)
|
|
|
|
precondition(!beforeGap.contains(.gap))
|
|
|
|
|
|
|
|
// if there's any overlap, last overlapping item will be the last item below the gap
|
|
|
|
var indexOfLastTimelineItemExistingAboveGap: Int?
|
|
|
|
if case .status(id: let id, state: _) = beforeGap.last {
|
|
|
|
indexOfLastTimelineItemExistingAboveGap = timelineItems.lastIndex(of: id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the start index of the reange of timeline items that don't yet exist in the data source
|
|
|
|
let startIndex: Int
|
|
|
|
if let indexOfLastTimelineItemExistingAboveGap {
|
|
|
|
// index(after:) because the beginning of the range is inclusive, but we don't want the item at indexOfLastTimelineItemExistingAboveGap
|
|
|
|
startIndex = timelineItems.index(after: indexOfLastTimelineItemExistingAboveGap)
|
|
|
|
} else {
|
|
|
|
startIndex = timelineItems.startIndex
|
|
|
|
}
|
|
|
|
let toInsert = timelineItems[startIndex...].map { Item.status(id: $0, state: .unknown) }
|
|
|
|
if toInsert.isEmpty {
|
|
|
|
addedItems = false
|
|
|
|
} else {
|
|
|
|
snapshot.insertItems(toInsert, afterItem: .gap)
|
|
|
|
addedItems = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there's any overlap between the items we loaded to insert below the gap
|
|
|
|
// and the items that already exist above the gap, we've completely filled the gap
|
|
|
|
if indexOfLastTimelineItemExistingAboveGap != nil {
|
|
|
|
snapshot.deleteItems([.gap])
|
|
|
|
}
|
2022-11-19 16:15:14 +00:00
|
|
|
|
|
|
|
if addedItems {
|
|
|
|
// use a snapshot of the collection view to hide the flicker as the content offset changes and then changes back
|
|
|
|
let snapshotView = collectionView.snapshotView(afterScreenUpdates: false)!
|
|
|
|
snapshotView.layer.zPosition = 1000
|
|
|
|
snapshotView.frame = view.bounds
|
|
|
|
view.addSubview(snapshotView)
|
|
|
|
|
|
|
|
// Yes, these are all load bearing. Setting the contentOffset seems to cause the collection view to recalculate the size
|
|
|
|
// of some cells, thus changing the contentSize and the offset necessary to match to match the bottom offset.
|
|
|
|
// Three DispatchQueue.main.async's seems to be the fewest we can reliably get away with.
|
|
|
|
let bottomOffset = collectionView.contentSize.height - collectionView.contentOffset.y
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: false) {
|
|
|
|
self.collectionView.contentOffset = CGPoint(x: 0, y: self.collectionView.contentSize.height - bottomOffset)
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.collectionView.contentOffset = CGPoint(x: 0, y: self.collectionView.contentSize.height - bottomOffset)
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.collectionView.contentOffset = CGPoint(x: 0, y: self.collectionView.contentSize.height - bottomOffset)
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.collectionView.contentOffset = CGPoint(x: 0, y: self.collectionView.contentSize.height - bottomOffset)
|
|
|
|
snapshotView.removeFromSuperview()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: true) {}
|
|
|
|
}
|
2022-11-18 22:29:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
// if we didn't add any items, that implies the gap was removed, and we want to to make clear what's happening
|
2022-11-18 22:29:55 +00:00
|
|
|
if !addedItems {
|
2022-11-19 01:49:15 +00:00
|
|
|
var config = ToastConfiguration(title: "There's nothing in between!")
|
|
|
|
config.dismissAutomaticallyAfter = 2
|
2022-11-18 22:29:55 +00:00
|
|
|
showToast(configuration: config, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 15:32:50 +00:00
|
|
|
enum Error: TimelineLikeCollectionViewError {
|
2022-09-24 14:49:06 +00:00
|
|
|
case noClient
|
|
|
|
case noNewer
|
|
|
|
case noOlder
|
|
|
|
case allCaughtUp
|
2022-11-18 22:29:55 +00:00
|
|
|
case noGap
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TimelineViewController: UICollectionViewDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
|
2022-10-11 01:01:03 +00:00
|
|
|
guard case .statuses = dataSource.sectionIdentifier(for: indexPath.section) else {
|
2022-09-24 14:49:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let itemsInSection = collectionView.numberOfItems(inSection: indexPath.section)
|
|
|
|
if indexPath.row == itemsInSection - 1 {
|
|
|
|
Task {
|
|
|
|
await controller.loadOlder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 19:32:06 +00:00
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
|
|
|
return dataSource.itemIdentifier(for: indexPath)?.isSelectable ?? false
|
|
|
|
}
|
|
|
|
|
2022-10-01 19:32:06 +00:00
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch item {
|
|
|
|
case .publicTimelineDescription:
|
|
|
|
removeTimelineDescriptionCell()
|
2022-10-06 02:28:10 +00:00
|
|
|
case .status(id: let id, state: let state):
|
2022-10-10 18:23:22 +00:00
|
|
|
let status = mastodonController.persistentContainer.status(for: id)!
|
|
|
|
// if the status in the timeline is a reblog, show the status that it is a reblog of
|
|
|
|
selected(status: status.reblog?.id ?? id, state: state.copy())
|
2022-11-19 16:15:14 +00:00
|
|
|
case .gap:
|
|
|
|
let cell = collectionView.cellForItem(at: indexPath) as! TimelineGapCollectionViewCell
|
|
|
|
cell.showsIndicator = true
|
|
|
|
Task {
|
|
|
|
await controller.fillGap(in: cell.direction)
|
|
|
|
cell.showsIndicator = false
|
|
|
|
}
|
|
|
|
case .loadingIndicator, .confirmLoadMore:
|
2022-10-06 02:28:10 +00:00
|
|
|
fatalError("unreachable")
|
2022-10-01 19:32:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 23:16:10 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-10-01 19:32:06 +00:00
|
|
|
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
|
|
|
|
if isShowingTimelineDescription {
|
|
|
|
removeTimelineDescriptionCell()
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
|
2022-10-08 18:59:50 +00:00
|
|
|
extension TimelineViewController: UICollectionViewDragDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
|
|
|
|
(collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.dragItemsForBeginning(session: session) ?? []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 21:40:00 +00:00
|
|
|
extension TimelineViewController: TuskerNavigationDelegate {
|
2022-10-31 20:27:13 +00:00
|
|
|
var apiController: MastodonController! { mastodonController }
|
2022-10-05 21:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension TimelineViewController: MenuActionProvider {
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
extension TimelineViewController: StatusCollectionViewCellDelegate {
|
2022-10-08 19:31:08 +00:00
|
|
|
func statusCellNeedsReconfigure(_ cell: StatusCollectionViewCell, animated: Bool, completion: (() -> Void)?) {
|
2022-10-05 21:40:00 +00:00
|
|
|
if let indexPath = collectionView.indexPath(for: cell) {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.reconfigureItems([dataSource.itemIdentifier(for: indexPath)!])
|
2022-10-08 19:31:08 +00:00
|
|
|
dataSource.apply(snapshot, animatingDifferences: animated, completion: completion)
|
2022-10-05 21:40:00 +00:00
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-02 00:49:07 +00:00
|
|
|
|
|
|
|
extension TimelineViewController: TabBarScrollableViewController {
|
|
|
|
func tabBarScrollToTop() {
|
|
|
|
collectionView.scrollToTop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TimelineViewController: StatusBarTappableViewController {
|
|
|
|
func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult {
|
|
|
|
collectionView.scrollToTop()
|
|
|
|
return .stop
|
|
|
|
}
|
|
|
|
}
|