@@ -54,6 +54,7 @@ final class ItemsTableViewHandler: NSObject {
54
54
private unowned let delegate : ItemsTableViewHandlerDelegate
55
55
private unowned let dataSource : ItemsTableViewDataSource
56
56
private unowned let dragDropController : DragDropController
57
+ private unowned let citationController : CitationController ?
57
58
private let disposeBag : DisposeBag
58
59
59
60
private var reloadAnimationsDisabled : Bool
@@ -62,12 +63,14 @@ final class ItemsTableViewHandler: NSObject {
62
63
tableView: UITableView ,
63
64
delegate: ItemsTableViewHandlerDelegate ,
64
65
dataSource: ItemsTableViewDataSource ,
65
- dragDropController: DragDropController
66
+ dragDropController: DragDropController ,
67
+ citationController: CitationController ?
66
68
) {
67
69
self . tableView = tableView
68
70
self . delegate = delegate
69
71
self . dataSource = dataSource
70
72
self . dragDropController = dragDropController
73
+ self . citationController = citationController
71
74
reloadAnimationsDisabled = false
72
75
disposeBag = DisposeBag ( )
73
76
@@ -308,56 +311,47 @@ extension ItemsTableViewHandler: UITableViewDelegate {
308
311
extension ItemsTableViewHandler : UITableViewDragDelegate {
309
312
func tableView( _ tableView: UITableView , itemsForBeginning session: UIDragSession , at indexPath: IndexPath ) -> [ UIDragItem ] {
310
313
guard let item = dataSource. object ( at: indexPath. row) as? RItem else { return [ ] }
311
- return [ self . dragDropController. dragItem ( from: item) ]
314
+ session. localContext = DragSessionItemsLocalContext ( libraryIdentifier: item. libraryIdentifier, keys: Set ( [ item. key] ) )
315
+ return [ dragDropController. dragItem ( from: item, citationController: citationController, disposeBag: disposeBag) ]
316
+ }
317
+
318
+ func tableView( _ tableView: UITableView , itemsForAddingTo session: any UIDragSession , at indexPath: IndexPath , point: CGPoint ) -> [ UIDragItem ] {
319
+ guard let item = dataSource. object ( at: indexPath. row) as? RItem ,
320
+ let localContext = session. localContext as? DragSessionItemsLocalContext ,
321
+ let newLocalContext = localContext. createNewContext ( with: item)
322
+ else { return [ ] }
323
+ session. localContext = newLocalContext
324
+ return [ dragDropController. dragItem ( from: item, citationController: citationController, disposeBag: disposeBag) ]
312
325
}
313
326
}
314
327
315
328
extension ItemsTableViewHandler : UITableViewDropDelegate {
316
329
func tableView( _ tableView: UITableView , performDropWith coordinator: UITableViewDropCoordinator ) {
317
- guard let object = coordinator. destinationIndexPath. flatMap ( { dataSource. object ( at: $0. row) } ) else { return }
318
-
330
+ guard let indexPath = coordinator. destinationIndexPath, let object = dataSource. object ( at: indexPath. row) else { return }
319
331
switch coordinator. proposal. operation {
320
332
case . copy:
321
333
let key = object. key
322
- let localObject = coordinator. items. first? . dragItem. localObject
323
- self . dragDropController. keys ( from: coordinator. items. map ( { $0. dragItem } ) ) { [ weak self] keys in
324
- guard let self else { return }
325
- if localObject is RItem {
326
- delegate. process ( dragAndDropAction: . moveItems( keys: keys, toKey: key) )
327
- } else if localObject is RTag {
328
- delegate. process ( dragAndDropAction: . tagItem( key: key, libraryId: object. libraryIdentifier, tags: keys) )
329
- }
330
- }
331
- default : break
332
- }
333
- }
334
+ guard let localContext = coordinator. session. localDragSession? . localContext as? DragSessionItemsLocalContext , !localContext. keys. isEmpty else { break }
335
+ delegate. process ( dragAndDropAction: . moveItems( keys: localContext. keys, toKey: key) )
334
336
335
- func tableView( _ tableView: UITableView , dropSessionDidUpdate session: UIDropSession , withDestinationIndexPath destinationIndexPath: IndexPath ? ) -> UITableViewDropProposal {
336
- guard
337
- delegate. library. metadataEditable, // allow only when library is editable
338
- session. localDragSession != nil , // allow only local drag session
339
- let destinationIndexPath = destinationIndexPath,
340
- destinationIndexPath. row < dataSource. count,
341
- session. items. first? . localObject is RItem
342
- else {
343
- return UITableViewDropProposal ( operation: . forbidden)
337
+ default :
338
+ break
344
339
}
345
- return self . itemDropSessionDidUpdate ( session: session, withDestinationIndexPath: destinationIndexPath)
346
340
}
347
341
348
- private func itemDropSessionDidUpdate ( session: UIDropSession , withDestinationIndexPath destinationIndexPath: IndexPath ) -> UITableViewDropProposal {
349
- guard let object = dataSource . object ( at : destinationIndexPath . row ) else {
350
- return UITableViewDropProposal ( operation : . forbidden )
351
- }
352
- let dragItemsLibraryId = session . items . compactMap ( { $0 . localObject as? RItem } ) . compactMap ( { $0 . libraryId } ) . first
353
-
354
- if dragItemsLibraryId != object . libraryIdentifier || // allow dropping only to the same library
355
- object . isNote || object . isAttachment || // allow dropping only to non-standalone items
356
- session . items . compactMap ( { self . dragDropController . item ( from : $0 ) } ) // allow drops of only standalone items
357
- . contains ( where : { $0 . rawType != ItemTypes . attachment && $0 . rawType != ItemTypes . note } ) {
358
- return UITableViewDropProposal ( operation : . forbidden )
359
- }
360
-
342
+ func tableView ( _ tableView : UITableView , dropSessionDidUpdate session: UIDropSession , withDestinationIndexPath destinationIndexPath: IndexPath ? ) -> UITableViewDropProposal {
343
+ let library = delegate . library
344
+ guard library . metadataEditable ,
345
+ let localContext = session . localDragSession ? . localContext as? DragSessionItemsLocalContext ,
346
+ localContext . libraryIdentifier == library . identifier ,
347
+ !localContext . keys . isEmpty ,
348
+ let destinationIndexPath ,
349
+ destinationIndexPath . row < dataSource . count ,
350
+ let object = dataSource . object ( at : destinationIndexPath . row ) ,
351
+ !object . isNote ,
352
+ !object . isAttachment ,
353
+ !session . items . compactMap ( { $0 . localObject as? RItem } ) . contains ( where : { $0 . rawType != ItemTypes . attachment && $0 . rawType != ItemTypes . note } )
354
+ else { return UITableViewDropProposal ( operation : . forbidden ) }
361
355
return UITableViewDropProposal ( operation: . copy, intent: . insertIntoDestinationIndexPath)
362
356
}
363
357
}
0 commit comments