From 0ddce89c32595424a98f540e8d0ef2e60f5b8824 Mon Sep 17 00:00:00 2001 From: Soner Yuksel Date: Mon, 25 Oct 2021 12:02:58 -0400 Subject: [PATCH] Adding Favourite section check for drop and drag session functions --- .../NewTabPageViewController.swift | 80 ++++++++++--------- .../Sections/FavoritesSectionProvider.swift | 4 + 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift b/Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift index 4e516702986..ea60c660783 100644 --- a/Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift +++ b/Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift @@ -146,10 +146,12 @@ class NewTabPageViewController: UIViewController { } #endif - collectionView.delegate = self - collectionView.dataSource = self - collectionView.dragDelegate = self - collectionView.dropDelegate = self + collectionView.do { + $0.delegate = self + $0.dataSource = self + $0.dragDelegate = self + $0.dropDelegate = self + } background.changed = { [weak self] in self?.setupBackgroundImage() @@ -870,9 +872,6 @@ extension NewTabPageViewController: UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { sections[indexPath.section].collectionView?(collectionView, didEndDisplaying: cell, forItemAt: indexPath) } - func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt currentIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath { - currentIndexPath.section == proposedIndexPath.section ? proposedIndexPath : currentIndexPath - } } // MARK: - UICollectionViewDataSource @@ -917,24 +916,28 @@ extension NewTabPageViewController: UICollectionViewDataSource { extension NewTabPageViewController: UICollectionViewDragDelegate, UICollectionViewDropDelegate { func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { - - // TODO: Check If the action is in right section + // Check If the item that is dragged is a favourite item + guard sections[indexPath.section] is FavoritesSectionProvider else { + return [] + } - let itemProvider = NSItemProvider(object: "\(indexPath)" as NSString) - let dragItem = UIDragItem(itemProvider: itemProvider) - dragItem.previewProvider = { () -> UIDragPreview? in + let itemProvider = NSItemProvider(object: "\(indexPath)" as NSString) + let dragItem = UIDragItem(itemProvider: itemProvider).then { + $0.previewProvider = { () -> UIDragPreview? in guard let cell = collectionView.cellForItem(at: indexPath) as? FavoriteCell else { - return nil + return nil } return UIDragPreview(view: cell.imageView) } - // TODO: Grab DragItem from Favourites - return [dragItem] + } + + return [dragItem] } func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) { guard let sourceIndexPath = coordinator.items.first?.sourceIndexPath else { return } let destinationIndexPath: IndexPath + if let indexPath = coordinator.destinationIndexPath { destinationIndexPath = indexPath } else { @@ -943,53 +946,54 @@ extension NewTabPageViewController: UICollectionViewDragDelegate, UICollectionVi destinationIndexPath = IndexPath(row: max(row - 1, 0), section: section) } - if sourceIndexPath.section != destinationIndexPath.section { - return - } + guard sourceIndexPath.section == destinationIndexPath.section else { return } - switch coordinator.proposal.operation { - case .move: + if coordinator.proposal.operation == .move { guard let item = coordinator.items.first else { return } - _ = coordinator.drop(item.dragItem, toItemAt: destinationIndexPath) + Favorite.reorder( sourceIndexPath: sourceIndexPath, destinationIndexPath: destinationIndexPath, isInteractiveDragReorder: true ) - case .copy: - break - default: return + _ = coordinator.drop(item.dragItem, toItemAt: destinationIndexPath) + } } func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal { - - // TODO: Can not drag cell drag if there is only one favourite + guard let destinationIndexSection = destinationIndexPath?.section, + let favouriteSection = sections[destinationIndexSection] as? FavoritesSectionProvider, + favouriteSection.hasMoreThanOneFavouriteItems else { + return .init(operation: .cancel) + } return .init(operation: .move, intent: .insertAtDestinationIndexPath) } func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? { - let params = UIDragPreviewParameters() - params.backgroundColor = .clear - if let cell = collectionView.cellForItem(at: indexPath) as? FavoriteCell { - params.visiblePath = UIBezierPath(roundedRect: cell.imageView.frame, cornerRadius: 8) - } - return params + fetchInteractionPreviewParameters(at: indexPath) } func collectionView(_ collectionView: UICollectionView, dropPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? { - let params = UIDragPreviewParameters() - params.backgroundColor = .clear - if let cell = collectionView.cellForItem(at: indexPath) as? FavoriteCell { - params.visiblePath = UIBezierPath(roundedRect: cell.imageView.frame, cornerRadius: 8) - } - return params + fetchInteractionPreviewParameters(at: indexPath) } func collectionView(_ collectionView: UICollectionView, dragSessionIsRestrictedToDraggingApplication session: UIDragSession) -> Bool { return true } + + private func fetchInteractionPreviewParameters(at indexPath: IndexPath) -> UIDragPreviewParameters { + let previewParameters = UIDragPreviewParameters().then { + $0.backgroundColor = .clear + + if let cell = collectionView.cellForItem(at: indexPath) as? FavoriteCell { + $0.visiblePath = UIBezierPath(roundedRect: cell.imageView.frame, cornerRadius: 8) + } + } + + return previewParameters + } } extension NewTabPageViewController { diff --git a/Client/Frontend/Browser/New Tab Page/Sections/FavoritesSectionProvider.swift b/Client/Frontend/Browser/New Tab Page/Sections/FavoritesSectionProvider.swift index 0355702790e..541dc6bba8c 100644 --- a/Client/Frontend/Browser/New Tab Page/Sections/FavoritesSectionProvider.swift +++ b/Client/Frontend/Browser/New Tab Page/Sections/FavoritesSectionProvider.swift @@ -21,6 +21,10 @@ class FavoritesSectionProvider: NSObject, NTPObservableSectionProvider { var action: (Favorite, BookmarksAction) -> Void var legacyLongPressAction: (UIAlertController) -> Void + var hasMoreThanOneFavouriteItems: Bool { + frc.fetchedObjects?.count ?? 0 > 0 + } + private var frc: NSFetchedResultsController init(action: @escaping (Favorite, BookmarksAction) -> Void,