Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit 873520b

Browse files
committed
Add canDropItem
1 parent 392ccfc commit 873520b

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

Sources/ASCollectionView/ASDragDropConfig+Public.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public extension ASDragDropConfig
5858
return this
5959
}
6060

61+
/// Called to check whether an item can be dropped
62+
func canDropItem(_ closure: @escaping ((IndexPath) -> Bool)) -> Self
63+
{
64+
var this = self
65+
this.canDropItem = closure
66+
return this
67+
}
68+
6169
/// An optional closure that you can use to decide what to do with a dropped item.
6270
/// Return nil if you want to ignore the drop.
6371
/// Return an item (of the same type as your section data) if you want to insert a row.

Sources/ASCollectionView/Config/ASDragDropConfig.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public struct ASDragDropConfig<Data>
2929
/// Called to check whether an item can be moved to the specified indexPath
3030
var canMoveItem: ((_ sourceIndexPath: IndexPath, _ destinationIndexPath: IndexPath) -> Bool)?
3131

32+
/// Called to check whether an item can be dropped
33+
var canDropItem: ((_ indexPath: IndexPath) -> Bool)?
34+
3235
var dragItemProvider: ((_ item: Data) -> NSItemProvider?)?
3336

3437
var dropItemProvider: ((_ sourceItem: Data?, _ dragItem: UIDragItem) -> Data?)?

Sources/ASCollectionView/Implementation/ASCollectionView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,9 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
751751
func canDrop(at indexPath: IndexPath) -> Bool
752752
{
753753
guard !indexPath.isEmpty else { return false }
754-
return parent.sections[safe: indexPath.section]?.dataSource.dropEnabled ?? false
754+
755+
return (parent.sections[safe: indexPath.section]?.dataSource.dropEnabled ?? false)
756+
&& (parent.sections[safe: indexPath.section]?.dataSource.canDropItem?(indexPath) ?? true)
755757
}
756758

757759
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem]

Sources/ASCollectionView/Implementation/ASSectionDataSource.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ internal protocol ASSectionDataSourceProtocol
4545

4646
var dragEnabled: Bool { get }
4747
var dropEnabled: Bool { get }
48+
var canDropItem: ((IndexPath) -> Bool)? { get }
4849
var reorderingEnabled: Bool { get }
4950

5051
mutating func setSelfSizingConfig(config: @escaping SelfSizingConfig)
@@ -100,6 +101,7 @@ internal struct ASSectionDataSource<DataCollection: RandomAccessCollection, Data
100101

101102
var dragEnabled: Bool { dragDropConfig.dragEnabled }
102103
var dropEnabled: Bool { dragDropConfig.dropEnabled }
104+
var canDropItem: ((IndexPath) -> Bool)? { dragDropConfig.canDropItem }
103105
var reorderingEnabled: Bool { dragDropConfig.reorderingEnabled }
104106

105107
var endIndex: Int { data.endIndex }

0 commit comments

Comments
 (0)