From fb7a7db6e87c70284ea255f79d44c245f8ccc567 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 17 Jan 2023 20:02:03 -0500 Subject: [PATCH] Handle deleted statuses in status action account list --- Tusker.xcodeproj/project.pbxproj | 16 +- .../ConversationViewController.swift | 48 +-- ...nAccountListCollectionViewController.swift | 237 +++++++++++++ ...tatusActionAccountListViewController.swift | 332 ++++++++---------- Tusker/Views/StatusNotFoundView.swift | 60 ++++ 5 files changed, 467 insertions(+), 226 deletions(-) create mode 100644 Tusker/Screens/Status Action Account List/StatusActionAccountListCollectionViewController.swift create mode 100644 Tusker/Views/StatusNotFoundView.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index d386462c..43a128af 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -153,6 +153,8 @@ D65B4B6229771A3F00DABDFB /* FetchStatusService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B6129771A3F00DABDFB /* FetchStatusService.swift */; }; D65B4B6429771EFF00DABDFB /* ConversationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B6329771EFF00DABDFB /* ConversationViewController.swift */; }; D65B4B6629773AE600DABDFB /* DeleteStatusService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B6529773AE600DABDFB /* DeleteStatusService.swift */; }; + D65B4B682977769E00DABDFB /* StatusActionAccountListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B672977769E00DABDFB /* StatusActionAccountListViewController.swift */; }; + D65B4B6A297777D900DABDFB /* StatusNotFoundView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B69297777D900DABDFB /* StatusNotFoundView.swift */; }; D65C6BF525478A9C00A6E89C /* BackgroundableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65C6BF425478A9C00A6E89C /* BackgroundableViewController.swift */; }; D65F613423AEAB6600F3CFD3 /* OnboardingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65F613323AEAB6600F3CFD3 /* OnboardingTests.swift */; }; D6620ACE2511A0ED00312CA0 /* StatusStateResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6620ACD2511A0ED00312CA0 /* StatusStateResolver.swift */; }; @@ -300,7 +302,7 @@ D6CA8CDE296387310050C433 /* SaveToPhotosActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6CA8CDD296387310050C433 /* SaveToPhotosActivity.swift */; }; D6D12B2F2925D66500D528E1 /* TimelineGapCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D12B2E2925D66500D528E1 /* TimelineGapCollectionViewCell.swift */; }; D6D12B56292D57E800D528E1 /* AccountCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D12B55292D57E800D528E1 /* AccountCollectionViewCell.swift */; }; - D6D12B58292D5B2C00D528E1 /* StatusActionAccountListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D12B57292D5B2C00D528E1 /* StatusActionAccountListViewController.swift */; }; + D6D12B58292D5B2C00D528E1 /* StatusActionAccountListCollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D12B57292D5B2C00D528E1 /* StatusActionAccountListCollectionViewController.swift */; }; D6D12B5A292D684600D528E1 /* AccountListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D12B59292D684600D528E1 /* AccountListViewController.swift */; }; D6D3F4C424FDB6B700EC4A6A /* View+ConditionalModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D3F4C324FDB6B700EC4A6A /* View+ConditionalModifier.swift */; }; D6D3FDE224F46A8D00FF50A5 /* ComposeUIState.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D3FDE124F46A8D00FF50A5 /* ComposeUIState.swift */; }; @@ -543,6 +545,8 @@ D65B4B6129771A3F00DABDFB /* FetchStatusService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchStatusService.swift; sourceTree = ""; }; D65B4B6329771EFF00DABDFB /* ConversationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationViewController.swift; sourceTree = ""; }; D65B4B6529773AE600DABDFB /* DeleteStatusService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeleteStatusService.swift; sourceTree = ""; }; + D65B4B672977769E00DABDFB /* StatusActionAccountListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusActionAccountListViewController.swift; sourceTree = ""; }; + D65B4B69297777D900DABDFB /* StatusNotFoundView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusNotFoundView.swift; sourceTree = ""; }; D65C6BF425478A9C00A6E89C /* BackgroundableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundableViewController.swift; sourceTree = ""; }; D65F612D23AE990C00F3CFD3 /* Embassy.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Embassy.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D65F613023AE99E000F3CFD3 /* Ambassador.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Ambassador.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -694,7 +698,7 @@ D6CA8CDD296387310050C433 /* SaveToPhotosActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SaveToPhotosActivity.swift; sourceTree = ""; }; D6D12B2E2925D66500D528E1 /* TimelineGapCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineGapCollectionViewCell.swift; sourceTree = ""; }; D6D12B55292D57E800D528E1 /* AccountCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountCollectionViewCell.swift; sourceTree = ""; }; - D6D12B57292D5B2C00D528E1 /* StatusActionAccountListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusActionAccountListViewController.swift; sourceTree = ""; }; + D6D12B57292D5B2C00D528E1 /* StatusActionAccountListCollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusActionAccountListCollectionViewController.swift; sourceTree = ""; }; D6D12B59292D684600D528E1 /* AccountListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountListViewController.swift; sourceTree = ""; }; D6D3F4C324FDB6B700EC4A6A /* View+ConditionalModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+ConditionalModifier.swift"; sourceTree = ""; }; D6D3FDE124F46A8D00FF50A5 /* ComposeUIState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeUIState.swift; sourceTree = ""; }; @@ -1292,7 +1296,8 @@ D6A3BC8C2321FF9B00FD64D5 /* Status Action Account List */ = { isa = PBXGroup; children = ( - D6D12B57292D5B2C00D528E1 /* StatusActionAccountListViewController.swift */, + D65B4B672977769E00DABDFB /* StatusActionAccountListViewController.swift */, + D6D12B57292D5B2C00D528E1 /* StatusActionAccountListCollectionViewController.swift */, ); path = "Status Action Account List"; sourceTree = ""; @@ -1382,6 +1387,7 @@ D6D7069F29466649000827ED /* ScrollingSegmentedControl.swift */, D627943123A5466600D38C68 /* SelectableTableViewCell.swift */, D620483723D38190008A63EF /* StatusContentTextView.swift */, + D65B4B69297777D900DABDFB /* StatusNotFoundView.swift */, 04ED00B021481ED800567C53 /* SteppedProgressView.swift */, D6093FB625BE0CF3004811E6 /* TrendHistoryView.swift */, D686BBE224FBF8110068E6AA /* WrappedProgressView.swift */, @@ -1908,6 +1914,7 @@ D69693FA25859A8000F4E116 /* ComposeSceneDelegate.swift in Sources */, D6A3BC8A2321F79B00FD64D5 /* AccountTableViewCell.swift in Sources */, D66A77BB233838DC0058F1EC /* UIFont+Traits.swift in Sources */, + D65B4B682977769E00DABDFB /* StatusActionAccountListViewController.swift in Sources */, D61F759B29384F9C00C0B37F /* FilterMO.swift in Sources */, D68FEC4F232C5BC300C84F23 /* SegmentedPageViewController.swift in Sources */, D64AAE9126C80DC600FC57FB /* ToastView.swift in Sources */, @@ -2068,7 +2075,7 @@ D620483423D3801D008A63EF /* LinkTextView.swift in Sources */, D61F75882932DB6000C0B37F /* StatusSwipeAction.swift in Sources */, D68A76EA295285D0001DA1B3 /* AddHashtagPinnedTimelineView.swift in Sources */, - D6D12B58292D5B2C00D528E1 /* StatusActionAccountListViewController.swift in Sources */, + D6D12B58292D5B2C00D528E1 /* StatusActionAccountListCollectionViewController.swift in Sources */, D6412B0D24B0D4CF00F5412E /* ProfileHeaderView.swift in Sources */, D641C77F213DC78A004B4513 /* InlineTextAttachment.swift in Sources */, D61A45E628DC0F2F002BE511 /* ConfirmLoadMoreCollectionViewCell.swift in Sources */, @@ -2160,6 +2167,7 @@ D6E426B325337C7000C02E1C /* CustomEmojiImageView.swift in Sources */, D6D4DDD0212518A000E1C4BB /* AppDelegate.swift in Sources */, D61F75B3293BD89C00C0B37F /* UpdateFilterService.swift in Sources */, + D65B4B6A297777D900DABDFB /* StatusNotFoundView.swift in Sources */, D6412B0924B0291E00F5412E /* MyProfileViewController.swift in Sources */, D6D12B56292D57E800D528E1 /* AccountCollectionViewCell.swift in Sources */, D6A6C10525B6138A00298D0F /* StatusTablePrefetching.swift in Sources */, diff --git a/Tusker/Screens/Conversation/ConversationViewController.swift b/Tusker/Screens/Conversation/ConversationViewController.swift index b9bf1155..44415c78 100644 --- a/Tusker/Screens/Conversation/ConversationViewController.swift +++ b/Tusker/Screens/Conversation/ConversationViewController.swift @@ -142,7 +142,6 @@ class ConversationViewController: UIViewController { return mastodonController.persistentContainer.addOrUpdateOnViewContext(status: status) case .notFound: state = .notFound - showMainStatusNotFound() return nil case .error(let error): self.showMainStatusError(error) @@ -209,41 +208,13 @@ class ConversationViewController: UIViewController { } private func showMainStatusNotFound() { - let emoji = UILabel() - emoji.font = .systemFont(ofSize: 64) - emoji.text = "🤷" - - let title = UILabel() - title.textColor = .secondaryLabel - title.font = .preferredFont(forTextStyle: .title1).withTraits(.traitBold)! - title.adjustsFontForContentSizeCategory = true - title.text = "Not Found" - - let subtitle = UILabel() - subtitle.textColor = .secondaryLabel - subtitle.font = .preferredFont(forTextStyle: .body) - subtitle.adjustsFontForContentSizeCategory = true - subtitle.text = "The post you are looking for may have been deleted, or may not be visible to you." - subtitle.numberOfLines = 0 - subtitle.textAlignment = .center - - let stack = UIStackView(arrangedSubviews: [ - emoji, - title, - subtitle, - ]) - stack.axis = .vertical - stack.alignment = .center - stack.spacing = 8 - stack.isAccessibilityElement = true - stack.accessibilityLabel = "\(title.text!). \(subtitle.text!)" - - stack.translatesAutoresizingMaskIntoConstraints = false - view.addSubview(stack) + let notFoundView = StatusNotFoundView(frame: .zero) + notFoundView.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(notFoundView) NSLayoutConstraint.activate([ - stack.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 1), - view.safeAreaLayoutGuide.trailingAnchor.constraint(equalToSystemSpacingAfter: stack.trailingAnchor, multiplier: 1), - stack.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor), + notFoundView.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 1), + view.safeAreaLayoutGuide.trailingAnchor.constraint(equalToSystemSpacingAfter: notFoundView.trailingAnchor, multiplier: 1), + notFoundView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor), ]) } @@ -278,6 +249,13 @@ extension ConversationViewController { } extension ConversationViewController: ToastableViewController { + var toastScrollView: UIScrollView? { + if case .displaying(let vc) = state { + return vc.toastScrollView + } else { + return nil + } + } } extension ConversationViewController: StatusBarTappableViewController { diff --git a/Tusker/Screens/Status Action Account List/StatusActionAccountListCollectionViewController.swift b/Tusker/Screens/Status Action Account List/StatusActionAccountListCollectionViewController.swift new file mode 100644 index 00000000..eba79cf9 --- /dev/null +++ b/Tusker/Screens/Status Action Account List/StatusActionAccountListCollectionViewController.swift @@ -0,0 +1,237 @@ +// +// StatusActionAccountListCollectionViewController.swift +// Tusker +// +// Created by Shadowfacts on 9/5/19. +// Copyright © 2019 Shadowfacts. All rights reserved. +// + +import UIKit +import Pachyderm + +class StatusActionAccountListCollectionViewController: UIViewController, CollectionViewController { + + private let mastodonController: MastodonController + + /// If `true`, a warning will be shown below the account list describing that the total favs/reblogs may be innacurate. + var showInacurateCountWarning = false + + var collectionView: UICollectionView! { + view as? UICollectionView + } + private var dataSource: UICollectionViewDiffableDataSource! + + /** + Creates a new view controller showing the accounts that performed the given action on the given status. + + - Parameter mastodonController The `MastodonController` instance this view controller uses. + */ + init(mastodonController: MastodonController) { + self.mastodonController = mastodonController + + super.init(nibName: nil, bundle: nil) + + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func loadView() { + let layout = UICollectionViewCompositionalLayout { [unowned self] sectionIndex, environment in + switch dataSource.sectionIdentifier(for: sectionIndex)! { + case .status: + var config = UICollectionLayoutListConfiguration(appearance: .grouped) + config.footerMode = self.showInacurateCountWarning ? .supplementary : .none + config.leadingSwipeActionsConfigurationProvider = { [unowned self] in + (collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.leadingSwipeActions() + } + config.trailingSwipeActionsConfigurationProvider = { [unowned self] in + (collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.trailingSwipeActions() + } + return NSCollectionLayoutSection.list(using: config, layoutEnvironment: environment) + case .accounts: + return NSCollectionLayoutSection.list(using: .init(appearance: .grouped), layoutEnvironment: environment) + } + } + view = UICollectionView(frame: .zero, collectionViewLayout: layout) + collectionView.delegate = self + collectionView.dragDelegate = self + collectionView.allowsFocus = true + dataSource = createDataSource() + } + + private func createDataSource() -> UICollectionViewDiffableDataSource { + let statusCell = UICollectionView.CellRegistration { [unowned self] cell, indexPath, item in + cell.delegate = self + cell.updateUI(statusID: item.0, state: item.1, filterResult: .allow, precomputedContent: nil) + } + let accountCell = UICollectionView.CellRegistration { [unowned self] cell, indexPath, item in + cell.delegate = self + cell.updateUI(accountID: item) + } + let dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in + switch itemIdentifier { + case .status(let id, let state): + return collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: (id, state)) + case .account(let id): + return collectionView.dequeueConfiguredReusableCell(using: accountCell, for: indexPath, item: id) + } + } + let sectionHeaderCell = UICollectionView.SupplementaryRegistration(elementKind: UICollectionView.elementKindSectionFooter) { (headerView, collectionView, indexPath) in + var config = headerView.defaultContentConfiguration() + config.text = NSLocalizedString("Favorite and reblog counts for posts originating from instances other than your own may not be accurate.", comment: "shown on lists of status total actions") + headerView.contentConfiguration = config + } + dataSource.supplementaryViewProvider = { collectionView, elementKind, indexPath in + return collectionView.dequeueConfiguredReusableSupplementary(using: sectionHeaderCell, for: indexPath) + } + return dataSource + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + clearSelectionOnAppear(animated: animated) + } + + func addStatus(_ status: StatusMO, state: CollapseState) { + loadViewIfNeeded() + + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([.status, .accounts]) + snapshot.appendItems([.status(status.id, state)], toSection: .status) + dataSource.apply(snapshot, animatingDifferences: false) + } + + func addAccounts(_ accountIDs: [String], animated: Bool) { + var snapshot = dataSource.snapshot() + snapshot.appendItems(accountIDs.map { .account($0) }, toSection: .accounts) + dataSource.apply(snapshot, animatingDifferences: animated) + } + +} + +extension StatusActionAccountListCollectionViewController { + enum Section { + case status + case accounts + } + enum Item: Hashable { + case status(String, CollapseState) + case account(String) + + static func ==(lhs: Item, rhs: Item) -> Bool { + switch (lhs, rhs) { + case (.status(let a, _), .status(let b, _)): + return a == b + case (.account(let a), .account(let b)): + return a == b + default: + return false + } + } + + func hash(into hasher: inout Hasher) { + switch self { + case .status(let id, _): + hasher.combine(0) + hasher.combine(id) + case .account(let id): + hasher.combine(1) + hasher.combine(id) + } + } + } +} + +extension StatusActionAccountListCollectionViewController: UICollectionViewDelegate { + func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + switch dataSource.itemIdentifier(for: indexPath) { + case nil: + return + case .status(let id, let state): + selected(status: id, state: state.copy()) + case .account(let id): + selected(account: id) + } + } + + func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { + guard let item = dataSource.itemIdentifier(for: indexPath), + let cell = collectionView.cellForItem(at: indexPath) else { + return nil + } + switch item { + case .status: + return (cell as? TimelineStatusCollectionViewCell)?.contextMenuConfiguration() + case .account(let id): + return UIContextMenuConfiguration { + ProfileViewController(accountID: id, mastodonController: self.mastodonController) + } actionProvider: { _ in + UIMenu(children: self.actionsForProfile(accountID: id, source: .view(cell))) + } + } + } + + func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { + MenuPreviewHelper.willPerformPreviewAction(animator: animator, presenter: self) + } +} + +extension StatusActionAccountListCollectionViewController: UICollectionViewDragDelegate { + func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { + guard let currentAccountID = mastodonController.accountInfo?.id, + let item = dataSource.itemIdentifier(for: indexPath) else { + return [] + } + let provider: NSItemProvider + switch item { + case .status(let id, _): + guard let status = mastodonController.persistentContainer.status(for: id) else { + return [] + } + provider = NSItemProvider(object: status.url! as NSURL) + let activity = UserActivityManager.showConversationActivity(mainStatusID: id, accountID: currentAccountID) + activity.displaysAuxiliaryScene = true + provider.registerObject(activity, visibility: .all) + case .account(let id): + guard let account = mastodonController.persistentContainer.account(for: id) else { + return [] + } + provider = NSItemProvider(object: account.url as NSURL) + let activity = UserActivityManager.showProfileActivity(id: account.id, accountID: currentAccountID) + activity.displaysAuxiliaryScene = true + provider.registerObject(activity, visibility: .all) + } + return [UIDragItem(itemProvider: provider)] + } +} + +extension StatusActionAccountListCollectionViewController: TuskerNavigationDelegate { + var apiController: MastodonController! { mastodonController } +} + +extension StatusActionAccountListCollectionViewController: MenuActionProvider { +} + +extension StatusActionAccountListCollectionViewController: 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) { + fatalError() + } +} + +extension StatusActionAccountListCollectionViewController: StatusBarTappableViewController { + func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult { + collectionView.scrollToTop() + return .stop + } +} diff --git a/Tusker/Screens/Status Action Account List/StatusActionAccountListViewController.swift b/Tusker/Screens/Status Action Account List/StatusActionAccountListViewController.swift index 2d092e95..1150ebe0 100644 --- a/Tusker/Screens/Status Action Account List/StatusActionAccountListViewController.swift +++ b/Tusker/Screens/Status Action Account List/StatusActionAccountListViewController.swift @@ -2,28 +2,58 @@ // StatusActionAccountListViewController.swift // Tusker // -// Created by Shadowfacts on 9/5/19. -// Copyright © 2019 Shadowfacts. All rights reserved. +// Created by Shadowfacts on 1/17/23. +// Copyright © 2023 Shadowfacts. All rights reserved. // import UIKit import Pachyderm -class StatusActionAccountListViewController: UIViewController, CollectionViewController { +class StatusActionAccountListViewController: UIViewController { private let mastodonController: MastodonController - private let actionType: ActionType + private let actionType: StatusActionAccountListViewController.ActionType private let statusID: String private let statusState: CollapseState private var accountIDs: [String]? /// If `true`, a warning will be shown below the account list describing that the total favs/reblogs may be innacurate. - var showInacurateCountWarning = false - - var collectionView: UICollectionView! { - view as? UICollectionView + var showInacurateCountWarning = false { + didSet { + if case .displaying(let vc) = state { + vc.showInacurateCountWarning = showInacurateCountWarning + } + } + } + + private var state: State = .unloaded { + didSet { + switch oldValue { + case .loading(let indicator): + indicator.removeFromSuperview() + case .displaying(let vc): + vc.removeViewAndController() + default: + break + } + + switch state { + case .unloaded: + break + case .loading(let indicator): + indicator.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(indicator) + NSLayoutConstraint.activate([ + indicator.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor), + indicator.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor), + ]) + case .displaying(let vc): + embedChild(vc) + case .notFound: + showStatusNotFound() + } + } } - private var dataSource: UICollectionViewDiffableDataSource! /** Creates a new view controller showing the accounts that performed the given action on the given status. @@ -33,14 +63,22 @@ class StatusActionAccountListViewController: UIViewController, CollectionViewCon - Parameter accountIDs The accounts that will be shown. If `nil` is passed, a request will be performed to load the accounts. - Parameter mastodonController The `MastodonController` instance this view controller uses. */ - init(actionType: ActionType, statusID: String, statusState: CollapseState, accountIDs: [String]?, mastodonController: MastodonController) { + init(actionType: StatusActionAccountListViewController.ActionType, statusID: String, statusState: CollapseState, accountIDs: [String]?, mastodonController: MastodonController) { self.mastodonController = mastodonController self.actionType = actionType self.statusID = statusID self.statusState = statusState self.accountIDs = accountIDs - + super.init(nibName: nil, bundle: nil) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func viewDidLoad() { + super.viewDidLoad() switch actionType { case .favorite: @@ -48,89 +86,76 @@ class StatusActionAccountListViewController: UIViewController, CollectionViewCon case .reblog: title = NSLocalizedString("Reblogged By", comment: "status reblogged by accounts list title") } - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func loadView() { - let layout = UICollectionViewCompositionalLayout { [unowned self] sectionIndex, environment in - switch dataSource.sectionIdentifier(for: sectionIndex)! { - case .status: - var config = UICollectionLayoutListConfiguration(appearance: .grouped) - config.footerMode = self.showInacurateCountWarning ? .supplementary : .none - config.leadingSwipeActionsConfigurationProvider = { [unowned self] in - (collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.leadingSwipeActions() - } - config.trailingSwipeActionsConfigurationProvider = { [unowned self] in - (collectionView.cellForItem(at: $0) as? TimelineStatusCollectionViewCell)?.trailingSwipeActions() - } - return NSCollectionLayoutSection.list(using: config, layoutEnvironment: environment) - case .accounts: - return NSCollectionLayoutSection.list(using: .init(appearance: .grouped), layoutEnvironment: environment) - } - } - view = UICollectionView(frame: .zero, collectionViewLayout: layout) - collectionView.delegate = self - collectionView.dragDelegate = self - collectionView.allowsFocus = true - dataSource = createDataSource() - } - - private func createDataSource() -> UICollectionViewDiffableDataSource { - let statusCell = UICollectionView.CellRegistration { [unowned self] cell, indexPath, _ in - cell.delegate = self - cell.updateUI(statusID: self.statusID, state: self.statusState, filterResult: .allow, precomputedContent: nil) - } - let accountCell = UICollectionView.CellRegistration { [unowned self] cell, indexPath, item in - cell.delegate = self - cell.updateUI(accountID: item) - } - let dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in - switch itemIdentifier { - case .status: - return collectionView.dequeueConfiguredReusableCell(using: statusCell, for: indexPath, item: ()) - case .account(let id): - return collectionView.dequeueConfiguredReusableCell(using: accountCell, for: indexPath, item: id) - } - } - let sectionHeaderCell = UICollectionView.SupplementaryRegistration(elementKind: UICollectionView.elementKindSectionFooter) { (headerView, collectionView, indexPath) in - var config = headerView.defaultContentConfiguration() - config.text = NSLocalizedString("Favorite and reblog counts for posts originating from instances other than your own may not be accurate.", comment: "shown on lists of status total actions") - headerView.contentConfiguration = config - } - dataSource.supplementaryViewProvider = { collectionView, elementKind, indexPath in - return collectionView.dequeueConfiguredReusableSupplementary(using: sectionHeaderCell, for: indexPath) - } - return dataSource - } - - override func viewDidLoad() { - super.viewDidLoad() - var snapshot = NSDiffableDataSourceSnapshot() - snapshot.appendSections([.status, .accounts]) - snapshot.appendItems([.status], toSection: .status) - if let accountIDs { - snapshot.appendItems(accountIDs.map { .account($0) }, toSection: .accounts) - } - dataSource.apply(snapshot, animatingDifferences: false) + view.backgroundColor = .secondarySystemBackground + + NotificationCenter.default.addObserver(self, selector: #selector(handleStatusDeleted), name: .statusDeleted, object: nil) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) - clearSelectionOnAppear(animated: animated) - - if accountIDs == nil { - Task { - await loadAccounts() + Task { + await loadStatus() + } + } + + @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 + } + if statusIDs.contains(statusID) { + state = .notFound + } + } + + // MARK: Loading + + private func loadStatus() async { + @MainActor + func doLoadStatus() async -> StatusMO? { + switch await FetchStatusService(statusID: statusID, mastodonController: mastodonController).run() { + case .loaded(let status): + return mastodonController.persistentContainer.addOrUpdateOnViewContext(status: status) + case .notFound: + state = .notFound + return nil + case .error(let error): + self.showStatusError(error) + return nil + } + } + + if let cached = mastodonController.persistentContainer.status(for: statusID) { + await statusLoaded(cached) + } else { + let indicator = UIActivityIndicatorView(style: .medium) + indicator.startAnimating() + state = .loading(indicator) + + if let status = await doLoadStatus() { + await statusLoaded(status) } } } - private func loadAccounts() async { + private func statusLoaded(_ status: StatusMO) async { + let vc = StatusActionAccountListCollectionViewController(mastodonController: mastodonController) + vc.addStatus(status, state: statusState) + vc.showInacurateCountWarning = showInacurateCountWarning + state = .displaying(vc) + + if let accountIDs { + vc.addAccounts(accountIDs, animated: false) + } else { + await loadAccounts(list: vc) + } + } + + private func loadAccounts(list: StatusActionAccountListCollectionViewController) async { let request: Request<[Account]> switch actionType { case .favorite: @@ -148,20 +173,45 @@ class StatusActionAccountListViewController: UIViewController, CollectionViewCon } } - accountIDs = accounts.map(\.id) - - var snapshot = dataSource.snapshot() - snapshot.appendItems(accounts.map { .account($0.id) }, toSection: .accounts) - dataSource.apply(snapshot, animatingDifferences: true) {} + list.addAccounts(accounts.map(\.id), animated: true) } catch { let config = ToastConfiguration(from: error, with: "Error Loading Accounts", in: self) { toast in toast.dismissToast(animated: true) - await self.loadAccounts() + await self.loadAccounts(list: list) } self.showToast(configuration: config, animated: true) } } + + private func showStatusNotFound() { + let notFoundView = StatusNotFoundView(frame: .zero) + notFoundView.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(notFoundView) + NSLayoutConstraint.activate([ + notFoundView.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 1), + view.safeAreaLayoutGuide.trailingAnchor.constraint(equalToSystemSpacingAfter: notFoundView.trailingAnchor, multiplier: 1), + notFoundView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor), + ]) + } + + private func showStatusError(_ error: Client.Error) { + let config = ToastConfiguration(from: error, with: "Error Loading Status", in: self) { [weak self] toast in + toast.dismissToast(animated: true) + await self?.loadStatus() + } + self.showToast(configuration: config, animated: true) + } + +} + +extension StatusActionAccountListViewController { + enum State { + case unloaded + case loading(UIActivityIndicatorView) + case displaying(StatusActionAccountListCollectionViewController) + case notFound + } } extension StatusActionAccountListViewController { @@ -170,104 +220,12 @@ extension StatusActionAccountListViewController { } } -extension StatusActionAccountListViewController { - enum Section { - case status - case accounts - } - enum Item: Hashable { - case status - case account(String) - } -} - -extension StatusActionAccountListViewController: UICollectionViewDelegate { - func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - switch dataSource.itemIdentifier(for: indexPath) { - case nil: - return - case .status: - selected(status: statusID, state: statusState.copy()) - case .account(let id): - selected(account: id) - } - } - - func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { - guard let item = dataSource.itemIdentifier(for: indexPath), - let cell = collectionView.cellForItem(at: indexPath) else { +extension StatusActionAccountListViewController: ToastableViewController { + var toastScrollView: UIScrollView? { + if case .displaying(let vc) = state { + return vc.toastScrollView + } else { return nil } - switch item { - case .status: - return (cell as? TimelineStatusCollectionViewCell)?.contextMenuConfiguration() - case .account(let id): - return UIContextMenuConfiguration { - ProfileViewController(accountID: id, mastodonController: self.mastodonController) - } actionProvider: { _ in - UIMenu(children: self.actionsForProfile(accountID: id, source: .view(cell))) - } - } - } - - func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { - MenuPreviewHelper.willPerformPreviewAction(animator: animator, presenter: self) - } -} - -extension StatusActionAccountListViewController: UICollectionViewDragDelegate { - func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { - guard let currentAccountID = mastodonController.accountInfo?.id, - let item = dataSource.itemIdentifier(for: indexPath) else { - return [] - } - let provider: NSItemProvider - switch item { - case .status: - guard let status = mastodonController.persistentContainer.status(for: statusID) else { - return [] - } - provider = NSItemProvider(object: status.url! as NSURL) - let activity = UserActivityManager.showConversationActivity(mainStatusID: statusID, accountID: currentAccountID) - activity.displaysAuxiliaryScene = true - provider.registerObject(activity, visibility: .all) - case .account(let id): - guard let account = mastodonController.persistentContainer.account(for: id) else { - return [] - } - provider = NSItemProvider(object: account.url as NSURL) - let activity = UserActivityManager.showProfileActivity(id: account.id, accountID: currentAccountID) - activity.displaysAuxiliaryScene = true - provider.registerObject(activity, visibility: .all) - } - return [UIDragItem(itemProvider: provider)] - } -} - -extension StatusActionAccountListViewController: TuskerNavigationDelegate { - var apiController: MastodonController! { mastodonController } -} - -extension StatusActionAccountListViewController: MenuActionProvider { -} - -extension StatusActionAccountListViewController: 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) { - fatalError() - } -} - -extension StatusActionAccountListViewController: StatusBarTappableViewController { - func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult { - collectionView.scrollToTop() - return .stop } } diff --git a/Tusker/Views/StatusNotFoundView.swift b/Tusker/Views/StatusNotFoundView.swift new file mode 100644 index 00000000..d66d067b --- /dev/null +++ b/Tusker/Views/StatusNotFoundView.swift @@ -0,0 +1,60 @@ +// +// StatusNotFoundView.swift +// Tusker +// +// Created by Shadowfacts on 1/17/23. +// Copyright © 2023 Shadowfacts. All rights reserved. +// + +import UIKit + +class StatusNotFoundView: UIView { + + override init(frame: CGRect) { + super.init(frame: frame) + + let emoji = UILabel() + emoji.font = .systemFont(ofSize: 64) + emoji.text = "🤷" + + let title = UILabel() + title.textColor = .secondaryLabel + title.font = .preferredFont(forTextStyle: .title1).withTraits(.traitBold)! + title.adjustsFontForContentSizeCategory = true + title.text = "Not Found" + + let subtitle = UILabel() + subtitle.textColor = .secondaryLabel + subtitle.font = .preferredFont(forTextStyle: .body) + subtitle.adjustsFontForContentSizeCategory = true + subtitle.text = "The post you are looking for may have been deleted, or may not be visible to you." + subtitle.numberOfLines = 0 + subtitle.textAlignment = .center + + let stack = UIStackView(arrangedSubviews: [ + emoji, + title, + subtitle, + ]) + stack.axis = .vertical + stack.alignment = .center + stack.spacing = 8 + stack.isAccessibilityElement = true + stack.accessibilityLabel = "\(title.text!). \(subtitle.text!)" + + stack.translatesAutoresizingMaskIntoConstraints = false + addSubview(stack) + NSLayoutConstraint.activate([ + stack.leadingAnchor.constraint(equalTo: leadingAnchor), + stack.trailingAnchor.constraint(equalTo: trailingAnchor), + stack.topAnchor.constraint(equalTo: topAnchor), + stack.bottomAnchor.constraint(equalTo: bottomAnchor), + ]) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + +}