From 4293b51c31f1dce2fad86ec0364bf417f00f829b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 11 Feb 2023 19:05:12 -0500 Subject: [PATCH] Add extended suggested profiles screen Closes #355 --- Tusker.xcodeproj/project.pbxproj | 4 + .../MoreTrendsFooterCollectionViewCell.swift | 2 +- .../SuggestedProfilesViewController.swift | 212 ++++++++++++++++++ .../Explore/TrendingLinksViewController.swift | 6 +- 4 files changed, 218 insertions(+), 6 deletions(-) create mode 100644 Tusker/Screens/Explore/SuggestedProfilesViewController.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 05733ac1..36db9dfc 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -335,6 +335,7 @@ D6DD353F22F502EC00A9563A /* Preferences+Notification.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD353E22F502EC00A9563A /* Preferences+Notification.swift */; }; D6DD8FFD298495A8002AD3FD /* LogoutService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD8FFC298495A8002AD3FD /* LogoutService.swift */; }; D6DD8FFF2984D327002AD3FD /* BookmarksViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD8FFE2984D327002AD3FD /* BookmarksViewController.swift */; }; + D6DD996B2998611A0015C962 /* SuggestedProfilesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD996A2998611A0015C962 /* SuggestedProfilesViewController.swift */; }; D6DF95C12533F5DE0027A9B6 /* RelationshipMO.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DF95C02533F5DE0027A9B6 /* RelationshipMO.swift */; }; D6DFC69E242C490400ACC392 /* TrackpadScrollGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DFC69D242C490400ACC392 /* TrackpadScrollGestureRecognizer.swift */; }; D6DFC6A0242C4CCC00ACC392 /* Weak.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DFC69F242C4CCC00ACC392 /* Weak.swift */; }; @@ -756,6 +757,7 @@ D6DD353E22F502EC00A9563A /* Preferences+Notification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Preferences+Notification.swift"; sourceTree = ""; }; D6DD8FFC298495A8002AD3FD /* LogoutService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoutService.swift; sourceTree = ""; }; D6DD8FFE2984D327002AD3FD /* BookmarksViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksViewController.swift; sourceTree = ""; }; + D6DD996A2998611A0015C962 /* SuggestedProfilesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuggestedProfilesViewController.swift; sourceTree = ""; }; D6DF95C02533F5DE0027A9B6 /* RelationshipMO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelationshipMO.swift; sourceTree = ""; }; D6DFC69D242C490400ACC392 /* TrackpadScrollGestureRecognizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrackpadScrollGestureRecognizer.swift; sourceTree = ""; }; D6DFC69F242C4CCC00ACC392 /* Weak.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weak.swift; sourceTree = ""; }; @@ -948,6 +950,7 @@ D693A72E25CF91C6003A14E2 /* FeaturedProfileCollectionViewCell.xib */, D6C3F4FA299035650009FCFF /* TrendsViewController.swift */, D68329EE299540050026EB24 /* MoreTrendsFooterCollectionViewCell.swift */, + D6DD996A2998611A0015C962 /* SuggestedProfilesViewController.swift */, ); path = Explore; sourceTree = ""; @@ -1968,6 +1971,7 @@ D6093FB725BE0CF3004811E6 /* TrendHistoryView.swift in Sources */, D6EBF01723C55E0D00AE061B /* UISceneSession+MastodonController.swift in Sources */, D61F759229365C6C00C0B37F /* CollectionViewController.swift in Sources */, + D6DD996B2998611A0015C962 /* SuggestedProfilesViewController.swift in Sources */, D6DD8FFD298495A8002AD3FD /* LogoutService.swift in Sources */, 04DACE8C212CB14B009840C4 /* MainTabBarViewController.swift in Sources */, D68E525D24A3E8F00054355A /* InlineTrendsViewController.swift in Sources */, diff --git a/Tusker/Screens/Explore/MoreTrendsFooterCollectionViewCell.swift b/Tusker/Screens/Explore/MoreTrendsFooterCollectionViewCell.swift index 73683bfe..eb00ddf1 100644 --- a/Tusker/Screens/Explore/MoreTrendsFooterCollectionViewCell.swift +++ b/Tusker/Screens/Explore/MoreTrendsFooterCollectionViewCell.swift @@ -69,7 +69,7 @@ class MoreTrendsFooterCollectionViewCell: UICollectionViewCell { case .links: delegate.show(TrendingLinksViewController(mastodonController: delegate.apiController)) case .profileSuggestions: - delegate.show(ProfileDirectoryViewController(mastodonController: delegate.apiController)) + delegate.show(SuggestedProfilesViewController(mastodonController: delegate.apiController)) } } diff --git a/Tusker/Screens/Explore/SuggestedProfilesViewController.swift b/Tusker/Screens/Explore/SuggestedProfilesViewController.swift new file mode 100644 index 00000000..0f35cdfa --- /dev/null +++ b/Tusker/Screens/Explore/SuggestedProfilesViewController.swift @@ -0,0 +1,212 @@ +// +// SuggestedProfilesViewController.swift +// Tusker +// +// Created by Shadowfacts on 2/11/23. +// Copyright © 2023 Shadowfacts. All rights reserved. +// + +import UIKit +import Pachyderm + +class SuggestedProfilesViewController: UIViewController, CollectionViewController { + + weak var mastodonController: MastodonController! + + var collectionView: UICollectionView! + private var dataSource: UICollectionViewDiffableDataSource! + + private var state = State.unloaded + + 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 viewDidLoad() { + super.viewDidLoad() + + title = "Suggested Accounts" + + let layout = UICollectionViewCompositionalLayout { [unowned self] sectionIndex, environment in + switch dataSource.sectionIdentifier(for: sectionIndex) { + case nil: + fatalError() + + case .loadingIndicator: + var config = UICollectionLayoutListConfiguration(appearance: .grouped) + config.backgroundColor = .appGroupedBackground + config.showsSeparators = false + return .list(using: config, layoutEnvironment: environment) + + case .accounts: + let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(280)) + let item = NSCollectionLayoutItem(layoutSize: size) + let item2 = NSCollectionLayoutItem(layoutSize: size) + let group = NSCollectionLayoutGroup.vertical(layoutSize: size, subitems: [item, item2]) + group.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16) + group.interItemSpacing = .fixed(16) + let section = NSCollectionLayoutSection(group: group) + section.interGroupSpacing = 16 + section.contentInsets = NSDirectionalEdgeInsets(top: 16, leading: 0, bottom: 16, trailing: 0) + return section + } + } + collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) + collectionView.translatesAutoresizingMaskIntoConstraints = false + collectionView.delegate = self + collectionView.dragDelegate = self + collectionView.backgroundColor = .appGroupedBackground + collectionView.allowsFocus = true + 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), + ]) + + dataSource = createDataSource() + } + + private func createDataSource() -> UICollectionViewDiffableDataSource { + let loadingCell = UICollectionView.CellRegistration { cell, indexPath, itemIdentifier in + cell.indicator.startAnimating() + } + let accountCell = UICollectionView.CellRegistration(cellNib: UINib(nibName: "SuggestedProfileCardCollectionViewCell", bundle: .main)) { cell, indexPath, item in + cell.delegate = self + cell.updateUI(accountID: item.0, source: item.1) + } + return UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in + switch itemIdentifier { + case .loadingIndicator: + return collectionView.dequeueConfiguredReusableCell(using: loadingCell, for: indexPath, item: ()) + case .account(let id, let source): + return collectionView.dequeueConfiguredReusableCell(using: accountCell, for: indexPath, item: (id, source)) + } + } + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + Task { + await loadInitial() + } + } + + @MainActor + private func loadInitial() async { + guard case .unloaded = state else { + return + } + state = .loading + + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([.loadingIndicator]) + snapshot.appendItems([.loadingIndicator]) + await dataSource.apply(snapshot) + + do { + let request = Client.getSuggestions(limit: 80) + let (suggestions, _) = try await mastodonController.run(request) + + await mastodonController.persistentContainer.addAll(accounts: suggestions.map(\.account)) + + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([.accounts]) + snapshot.appendItems(suggestions.map { .account($0.account.id, $0.source) }) + await dataSource.apply(snapshot) + + state = .loaded + } catch { + state = .unloaded + let config = ToastConfiguration(from: error, with: "Error Loading Suggested Accounts", in: self) { [weak self] toast in + toast.dismissToast(animated: true) + await self?.loadInitial() + } + showToast(configuration: config, animated: true) + } + } + +} + +extension SuggestedProfilesViewController { + enum State { + case unloaded + case loading + case loaded + } +} + +extension SuggestedProfilesViewController { + enum Section { + case loadingIndicator + case accounts + } + enum Item: Hashable { + case loadingIndicator + case account(String, Suggestion.Source) + } +} + +extension SuggestedProfilesViewController: UICollectionViewDelegate { + func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { + if case .account(_, _) = dataSource.itemIdentifier(for: indexPath) { + return true + } else { + return false + } + } + + func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + guard case .account(let id, _) = dataSource.itemIdentifier(for: indexPath) else { + return + } + selected(account: id) + } + + func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { + guard case .account(let id, _) = dataSource.itemIdentifier(for: indexPath), + let cell = collectionView.cellForItem(at: indexPath) else { + return nil + } + 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 SuggestedProfilesViewController: UICollectionViewDragDelegate { + func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { + guard case .account(let id, _) = dataSource.itemIdentifier(for: indexPath), + let account = mastodonController.persistentContainer.account(for: id) else { + return [] + } + let provider = NSItemProvider(object: account.url as NSURL) + let activity = UserActivityManager.showProfileActivity(id: id, accountID: mastodonController.accountInfo!.id) + provider.registerObject(activity, visibility: .all) + return [UIDragItem(itemProvider: provider)] + } +} + +extension SuggestedProfilesViewController: TuskerNavigationDelegate { + var apiController: MastodonController! { mastodonController } +} + +extension SuggestedProfilesViewController: MenuActionProvider { +} + +extension SuggestedProfilesViewController: ToastableViewController { +} diff --git a/Tusker/Screens/Explore/TrendingLinksViewController.swift b/Tusker/Screens/Explore/TrendingLinksViewController.swift index fc05f89c..592630d1 100644 --- a/Tusker/Screens/Explore/TrendingLinksViewController.swift +++ b/Tusker/Screens/Explore/TrendingLinksViewController.swift @@ -46,11 +46,7 @@ class TrendingLinksViewController: UIViewController, CollectionViewController { var config = UICollectionLayoutListConfiguration(appearance: .grouped) config.backgroundColor = .appGroupedBackground config.showsSeparators = false - let section = NSCollectionLayoutSection.list(using: config, layoutEnvironment: environment) - if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { - section.contentInsetsReference = .readableContent - } - return section + return .list(using: config, layoutEnvironment: environment) case .links: let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(280))