2023-01-28 20:03:13 +00:00
|
|
|
//
|
|
|
|
// BookmarksViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/15/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
2023-01-28 20:15:40 +00:00
|
|
|
import CoreData
|
2023-01-28 20:03:13 +00:00
|
|
|
|
|
|
|
class BookmarksViewController: UIViewController, CollectionViewController {
|
|
|
|
|
|
|
|
private static let pageSize = 40
|
|
|
|
|
|
|
|
let mastodonController: MastodonController
|
|
|
|
|
|
|
|
var collectionView: UICollectionView! {
|
|
|
|
view as? UICollectionView
|
|
|
|
}
|
|
|
|
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
|
|
|
|
|
|
|
private var state = State.unloaded
|
|
|
|
private var newer: RequestRange?
|
|
|
|
private var older: RequestRange?
|
|
|
|
|
|
|
|
init(mastodonController: MastodonController) {
|
|
|
|
self.mastodonController = mastodonController
|
|
|
|
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
|
|
|
|
title = NSLocalizedString("Bookmarks", comment: "bookmarks screen title")
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func loadView() {
|
|
|
|
var config = UICollectionLayoutListConfiguration(appearance: .plain)
|
|
|
|
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, sectionConfig in
|
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath) else {
|
|
|
|
return sectionConfig
|
|
|
|
}
|
|
|
|
var config = sectionConfig
|
|
|
|
if item.hideIndicators {
|
|
|
|
config.topSeparatorVisibility = .hidden
|
|
|
|
config.bottomSeparatorVisibility = .hidden
|
|
|
|
} else {
|
|
|
|
config.topSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
|
|
|
|
config.bottomSeparatorInsets = TimelineStatusCollectionViewCell.separatorInsets
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
let layout = UICollectionViewCompositionalLayout { sectionIndex, environment in
|
|
|
|
let section = NSCollectionLayoutSection.list(using: config, layoutEnvironment: environment)
|
|
|
|
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
|
|
|
|
section.contentInsetsReference = .readableContent
|
|
|
|
}
|
|
|
|
return section
|
|
|
|
}
|
|
|
|
view = UICollectionView(frame: .zero, collectionViewLayout: layout)
|
|
|
|
collectionView.delegate = self
|
|
|
|
collectionView.dragDelegate = self
|
|
|
|
collectionView.allowsFocus = true
|
|
|
|
|
|
|
|
dataSource = createDataSource()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
|
|
|
|
let statusCell = UICollectionView.CellRegistration<TimelineStatusCollectionViewCell, (String, CollapseState)> { [unowned self] cell, indexPath, item in
|
|
|
|
cell.delegate = self
|
|
|
|
cell.updateUI(statusID: item.0, state: item.1, filterResult: .allow, precomputedContent: nil)
|
|
|
|
}
|
|
|
|
let loadingCell = UICollectionView.CellRegistration<LoadingCollectionViewCell, Void> { cell, indexPath, itemIdentifier in
|
|
|
|
cell.indicator.startAnimating()
|
|
|
|
}
|
|
|
|
return UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in
|
|
|
|
switch itemIdentifier {
|
2023-01-28 20:15:40 +00:00
|
|
|
case .status(id: let id, state: let state, addedLocally: _):
|
2023-01-28 20:03:13 +00:00
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: (id, state))
|
|
|
|
case .loadingIndicator:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: loadingCell, for: indexPath, item: ())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2023-01-28 20:15:40 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleStatusDeleted), name: .statusDeleted, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(managedObjectsDidChange), name: .NSManagedObjectContextObjectsDidChange, object: mastodonController.persistentContainer.viewContext)
|
2023-01-28 20:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
clearSelectionOnAppear(animated: animated)
|
|
|
|
|
|
|
|
if case .unloaded = state {
|
|
|
|
Task {
|
|
|
|
await loadInitial()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func apply(snapshot: NSDiffableDataSourceSnapshot<Section, Item>, animatingDifferences: Bool) async {
|
|
|
|
await Task { @MainActor in
|
|
|
|
self.dataSource.apply(snapshot, animatingDifferences: animatingDifferences)
|
|
|
|
}.value
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private func loadInitial() async {
|
|
|
|
state = .loadingInitial
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
snapshot.appendSections([.bookmarks])
|
|
|
|
snapshot.appendItems([.loadingIndicator])
|
|
|
|
await apply(snapshot: snapshot, animatingDifferences: false)
|
|
|
|
|
|
|
|
do {
|
|
|
|
let req = Client.getBookmarks(range: .count(BookmarksViewController.pageSize))
|
|
|
|
let (statuses, pagination) = try await mastodonController.run(req)
|
|
|
|
newer = pagination?.newer
|
|
|
|
older = pagination?.older
|
|
|
|
|
|
|
|
await mastodonController.persistentContainer.addAll(statuses: statuses)
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
snapshot.appendSections([.bookmarks])
|
2023-01-28 20:15:40 +00:00
|
|
|
snapshot.appendItems(statuses.map { .status(id: $0.id, state: .unknown, addedLocally: false) })
|
2023-01-28 20:03:13 +00:00
|
|
|
await apply(snapshot: snapshot, animatingDifferences: true)
|
|
|
|
|
|
|
|
state = .loaded
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
let config = ToastConfiguration(from: error, with: "Error Loading Bookmarks", in: self) { [weak self] toast in
|
|
|
|
toast.dismissToast(animated: true)
|
|
|
|
await self?.loadInitial()
|
|
|
|
}
|
|
|
|
showToast(configuration: config, animated: true)
|
|
|
|
|
|
|
|
await apply(snapshot: NSDiffableDataSourceSnapshot(), animatingDifferences: false)
|
|
|
|
|
|
|
|
state = .unloaded
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private func loadOlder() async {
|
|
|
|
guard case .loaded = state,
|
|
|
|
let older else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
state = .loadingOlder
|
|
|
|
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.appendItems([.loadingIndicator])
|
|
|
|
await apply(snapshot: snapshot, animatingDifferences: false)
|
|
|
|
|
|
|
|
do {
|
|
|
|
let req = Client.getBookmarks(range: older.withCount(BookmarksViewController.pageSize))
|
|
|
|
let (statuses, pagination) = try await mastodonController.run(req)
|
|
|
|
self.older = pagination?.older
|
|
|
|
|
|
|
|
await mastodonController.persistentContainer.addAll(statuses: statuses)
|
|
|
|
|
|
|
|
snapshot.deleteItems([.loadingIndicator])
|
2023-01-28 20:15:40 +00:00
|
|
|
snapshot.appendItems(statuses.map { .status(id: $0.id, state: .unknown, addedLocally: false) })
|
2023-01-28 20:03:13 +00:00
|
|
|
await apply(snapshot: snapshot, animatingDifferences: true)
|
|
|
|
|
|
|
|
state = .loaded
|
|
|
|
} catch {
|
|
|
|
let config = ToastConfiguration(from: error, with: "Error Loading Older Bookmarks", in: self) { [weak self] toast in
|
|
|
|
toast.dismissToast(animated: true)
|
|
|
|
await self?.loadOlder()
|
|
|
|
}
|
|
|
|
showToast(configuration: config, animated: true)
|
|
|
|
|
|
|
|
snapshot.deleteItems([.loadingIndicator])
|
|
|
|
await apply(snapshot: snapshot, animatingDifferences: false)
|
|
|
|
|
|
|
|
state = .loaded
|
|
|
|
}
|
|
|
|
}
|
2023-01-28 20:15:40 +00:00
|
|
|
|
|
|
|
@objc private func handleStatusDeleted(_ notification: Foundation.Notification) {
|
|
|
|
guard let userInfo = notification.userInfo,
|
|
|
|
let accountID = mastodonController.accountInfo?.id,
|
|
|
|
userInfo["accountID"] as? String == accountID,
|
|
|
|
let statusIDs = userInfo["statusIDs"] as? [String] else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
let toDelete = statusIDs.map { id in
|
|
|
|
Item.status(id: id, state: .unknown, addedLocally: false)
|
|
|
|
}.filter { item in
|
|
|
|
snapshot.itemIdentifiers.contains(item)
|
|
|
|
}
|
|
|
|
if !toDelete.isEmpty {
|
|
|
|
snapshot.deleteItems(toDelete)
|
|
|
|
Task {
|
|
|
|
await apply(snapshot: snapshot, animatingDifferences: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func managedObjectsDidChange(_ notification: Foundation.Notification) {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
func prepend(item: Item) {
|
|
|
|
if let first = snapshot.itemIdentifiers.first {
|
|
|
|
snapshot.insertItems([item], beforeItem: first)
|
|
|
|
} else {
|
|
|
|
snapshot.appendItems([item])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var hasChanges = false
|
|
|
|
if let inserted = notification.userInfo?[NSInsertedObjectsKey] as? Set<NSManagedObject> {
|
|
|
|
for case let status as StatusMO in inserted where status.bookmarked == true {
|
|
|
|
prepend(item: .status(id: status.id, state: .unknown, addedLocally: true))
|
|
|
|
hasChanges = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let updated = notification.userInfo?[NSUpdatedObjectsKey] as? Set<NSManagedObject> {
|
|
|
|
for case let status as StatusMO in updated {
|
|
|
|
let item = Item.status(id: status.id, state: .unknown, addedLocally: true)
|
|
|
|
var exists = snapshot.itemIdentifiers.contains(item)
|
|
|
|
if status.bookmarked == true && !exists {
|
|
|
|
prepend(item: item)
|
|
|
|
hasChanges = true
|
|
|
|
} else if status.bookmarked == false && exists {
|
|
|
|
snapshot.deleteItems([item])
|
|
|
|
hasChanges = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasChanges {
|
|
|
|
Task {
|
|
|
|
await apply(snapshot: snapshot, animatingDifferences: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-28 20:03:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BookmarksViewController {
|
|
|
|
enum Section {
|
|
|
|
case bookmarks
|
|
|
|
}
|
|
|
|
enum Item: Equatable, Hashable {
|
2023-01-28 20:15:40 +00:00
|
|
|
case status(id: String, state: CollapseState, addedLocally: Bool)
|
2023-01-28 20:03:13 +00:00
|
|
|
case loadingIndicator
|
|
|
|
|
|
|
|
var hideIndicators: Bool {
|
|
|
|
switch self {
|
|
|
|
case .loadingIndicator:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func ==(lhs: Item, rhs: Item) -> Bool {
|
|
|
|
switch (lhs, rhs) {
|
2023-01-28 20:15:40 +00:00
|
|
|
case (.status(id: let a, _, _), .status(id: let b, _, _)):
|
2023-01-28 20:03:13 +00:00
|
|
|
return a == b
|
|
|
|
case (.loadingIndicator, .loadingIndicator):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
switch self {
|
2023-01-28 20:15:40 +00:00
|
|
|
case .status(id: let id, _, _):
|
2023-01-28 20:03:13 +00:00
|
|
|
hasher.combine(0)
|
|
|
|
hasher.combine(id)
|
|
|
|
case .loadingIndicator:
|
|
|
|
hasher.combine(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BookmarksViewController {
|
|
|
|
enum State {
|
|
|
|
case unloaded
|
|
|
|
case loadingInitial
|
|
|
|
case loaded
|
|
|
|
case loadingOlder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BookmarksViewController: UICollectionViewDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
|
|
|
|
if indexPath.section == 0,
|
|
|
|
indexPath.row == collectionView.numberOfItems(inSection: indexPath.section) - 1 {
|
|
|
|
Task {
|
|
|
|
await self.loadOlder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
2023-01-28 20:15:40 +00:00
|
|
|
if case .status(_, _, _) = dataSource.itemIdentifier(for: indexPath) {
|
2023-01-28 20:03:13 +00:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
2023-01-28 20:15:40 +00:00
|
|
|
if case .status(id: let id, state: let state, _) = dataSource.itemIdentifier(for: indexPath) {
|
2023-01-28 20:03:13 +00:00
|
|
|
selected(status: id, state: state.copy())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
(collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.contextMenuConfiguration()
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
MenuPreviewHelper.willPerformPreviewAction(animator: animator, presenter: self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BookmarksViewController: UICollectionViewDragDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
|
|
|
|
(collectionView.cellForItem(at: indexPath) as? TimelineStatusCollectionViewCell)?.dragItemsForBeginning(session: session) ?? []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BookmarksViewController: TuskerNavigationDelegate {
|
|
|
|
var apiController: MastodonController! { mastodonController }
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BookmarksViewController: StatusCollectionViewCellDelegate {
|
|
|
|
func statusCellNeedsReconfigure(_ cell: StatusCollectionViewCell, animated: Bool, completion: (() -> Void)?) {
|
|
|
|
if let indexPath = collectionView.indexPath(for: cell) {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.reconfigureItems([dataSource.itemIdentifier(for: indexPath)!])
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: animated, completion: completion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusCellShowFiltered(_ cell: StatusCollectionViewCell) {
|
|
|
|
// bookmarks aren't filtered
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BookmarksViewController: TabBarScrollableViewController {
|
|
|
|
func tabBarScrollToTop() {
|
|
|
|
collectionView.scrollToTop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BookmarksViewController: StatusBarTappableViewController {
|
|
|
|
func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult {
|
|
|
|
collectionView.scrollToTop()
|
|
|
|
return .stop
|
|
|
|
}
|
|
|
|
}
|