From a1cf4a5789916bc9b42f523b7ac35d178ad6e692 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 11 Jan 2022 11:37:41 -0500 Subject: [PATCH] More context menus --- Reader/Screens/Home/HomeViewController.swift | 19 ++++++++++++++++ .../Screens/Items/ItemsViewController.swift | 22 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/Reader/Screens/Home/HomeViewController.swift b/Reader/Screens/Home/HomeViewController.swift index 50f2973..300a282 100644 --- a/Reader/Screens/Home/HomeViewController.swift +++ b/Reader/Screens/Home/HomeViewController.swift @@ -210,4 +210,23 @@ extension HomeViewController: UICollectionViewDelegate { vc.title = item.title show(vc, sender: nil) } + + func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { + guard let item = dataSource.itemIdentifier(for: indexPath) else { + return nil + } + return UIContextMenuConfiguration(identifier: nil, previewProvider: { + return ItemsViewController(fetchRequest: item.fetchRequest, fervorController: self.fervorController) + }, actionProvider: nil) + } + + func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { + guard let vc = animator.previewViewController else { + return + } + animator.preferredCommitStyle = .pop + animator.addCompletion { + self.show(vc, sender: nil) + } + } } diff --git a/Reader/Screens/Items/ItemsViewController.swift b/Reader/Screens/Items/ItemsViewController.swift index b70d6d0..80301a7 100644 --- a/Reader/Screens/Items/ItemsViewController.swift +++ b/Reader/Screens/Items/ItemsViewController.swift @@ -35,6 +35,7 @@ class ItemsViewController: UIViewController { configuration.backgroundColor = .appBackground let layout = UICollectionViewCompositionalLayout.list(using: configuration) collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout) + collectionView.delegate = self collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] collectionView.register(ItemCollectionViewCell.self, forCellWithReuseIdentifier: "itemCell") view.addSubview(collectionView) @@ -80,6 +81,27 @@ extension ItemsViewController: NSFetchedResultsControllerDelegate { } } +extension ItemsViewController: UICollectionViewDelegate { + func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { + guard let item = dataSource.itemIdentifier(for: indexPath) else { + return nil + } + return UIContextMenuConfiguration(identifier: nil, previewProvider: { + ReadViewController(item: item, fervorController: self.fervorController) + }, actionProvider: nil) + } + + func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { + guard let vc = animator.previewViewController else { + return + } + animator.preferredCommitStyle = .pop + animator.addCompletion { + self.show(vc, sender: nil) + } + } +} + extension ItemsViewController: ItemCollectionViewCellDelegate { func itemCellSelected(item: Item) { show(ReadViewController(item: item, fervorController: fervorController), sender: nil)