Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Adding Favourite section check for drop and drag session functions
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel committed Oct 25, 2021
1 parent 68e91a8 commit 0ddce89
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
80 changes: 42 additions & 38 deletions Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Favorite>

init(action: @escaping (Favorite, BookmarksAction) -> Void,
Expand Down

0 comments on commit 0ddce89

Please sign in to comment.