From a75862b5cc8b83c864e9af360af381d1a69e69f8 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 22 Jan 2023 12:06:51 -0500 Subject: [PATCH] Mask trending link card previews with same corner radius as cells --- .../Screens/Search/SearchViewController.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Tusker/Screens/Search/SearchViewController.swift b/Tusker/Screens/Search/SearchViewController.swift index e8239cb5..b9655ad0 100644 --- a/Tusker/Screens/Search/SearchViewController.swift +++ b/Tusker/Screens/Search/SearchViewController.swift @@ -265,6 +265,7 @@ extension SearchViewController: UICollectionViewDelegate { } } + @available(iOS, obsoleted: 16.0) func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { guard let item = dataSource.itemIdentifier(for: indexPath) else { return nil @@ -294,6 +295,34 @@ extension SearchViewController: UICollectionViewDelegate { fatalError("todo") } } + + // implementing the highlightPreviewForItemAt method seems to prevent the old, single IndexPath variant of this method from being called on iOS 16 + @available(iOS 16.0, *) + func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemsAt indexPaths: [IndexPath], point: CGPoint) -> UIContextMenuConfiguration? { + guard indexPaths.count == 1 else { + return nil + } + return self.collectionView(collectionView, contextMenuConfigurationForItemAt: indexPaths[0], point: point) + } + + func collectionView(_ collectionView: UICollectionView, contextMenuConfiguration configuration: UIContextMenuConfiguration, highlightPreviewForItemAt indexPath: IndexPath) -> UITargetedPreview? { + switch dataSource.itemIdentifier(for: indexPath) { + case .link(_): + guard let cell = collectionView.cellForItem(at: indexPath) else { + return nil + } + let params = UIPreviewParameters() + params.visiblePath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius) + return UITargetedPreview(view: cell, parameters: params) + + default: + return nil + } + } + + func collectionView(_ collectionView: UICollectionView, contextMenuConfiguration configuration: UIContextMenuConfiguration, dismissalPreviewForItemAt indexPath: IndexPath) -> UITargetedPreview? { + return self.collectionView(collectionView, contextMenuConfiguration: configuration, highlightPreviewForItemAt: indexPath) + } } extension SearchViewController: UICollectionViewDragDelegate {