ScanCollectionView.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. import UIKit
  3. @available(iOS 11, *)
  4. class DragDropViewController: UIViewController
  5. {
  6. //MARK: Private Properties
  7. //Data Source for CollectionView-1
  8. private var items1 = [String]()
  9. //Data Source for CollectionView-2
  10. private var items2 = [String]()
  11. //MARK: Outlets
  12. @IBOutlet weak var collectionView1: UICollectionView!
  13. @IBOutlet weak var collectionView2: UICollectionView!
  14. //MARK: View Lifecycle Methods
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. //CollectionView-1 drag and drop configuration
  18. self.collectionView1.dragInteractionEnabled = true
  19. self.collectionView1.dragDelegate = self
  20. self.collectionView1.dropDelegate = self
  21. //CollectionView-2 drag and drop configuration
  22. self.collectionView2.dragInteractionEnabled = true
  23. self.collectionView2.dropDelegate = self
  24. self.collectionView2.dragDelegate = self
  25. self.collectionView2.reorderingCadence = .fast //default value - .immediate
  26. loadImage()
  27. }
  28. private func loadImage() {
  29. do {
  30. let directoryContents = try FileManager.default.contentsOfDirectory(atPath: CCUtility.getDirectoryPDFGenerator()!)
  31. for fileName in directoryContents {
  32. items1.append(fileName)
  33. }
  34. }
  35. catch {
  36. print(error.localizedDescription)
  37. }
  38. }
  39. //MARK: Private Methods
  40. /// This method moves a cell from source indexPath to destination indexPath within the same collection view. It works for only 1 item. If multiple items selected, no reordering happens.
  41. ///
  42. /// - Parameters:
  43. /// - coordinator: coordinator obtained from performDropWith: UICollectionViewDropDelegate method
  44. /// - destinationIndexPath: indexpath of the collection view where the user drops the element
  45. /// - collectionView: collectionView in which reordering needs to be done.
  46. private func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
  47. {
  48. let items = coordinator.items
  49. if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath
  50. {
  51. var dIndexPath = destinationIndexPath
  52. if dIndexPath.row >= collectionView.numberOfItems(inSection: 0)
  53. {
  54. dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
  55. }
  56. collectionView.performBatchUpdates({
  57. if collectionView === self.collectionView2
  58. {
  59. self.items2.remove(at: sourceIndexPath.row)
  60. self.items2.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  61. }
  62. else
  63. {
  64. self.items1.remove(at: sourceIndexPath.row)
  65. self.items1.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  66. }
  67. collectionView.deleteItems(at: [sourceIndexPath])
  68. collectionView.insertItems(at: [dIndexPath])
  69. })
  70. coordinator.drop(items.first!.dragItem, toItemAt: dIndexPath)
  71. }
  72. }
  73. /// This method copies a cell from source indexPath in 1st collection view to destination indexPath in 2nd collection view. It works for multiple items.
  74. ///
  75. /// - Parameters:
  76. /// - coordinator: coordinator obtained from performDropWith: UICollectionViewDropDelegate method
  77. /// - destinationIndexPath: indexpath of the collection view where the user drops the element
  78. /// - collectionView: collectionView in which reordering needs to be done.
  79. private func copyItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
  80. {
  81. collectionView.performBatchUpdates({
  82. var indexPaths = [IndexPath]()
  83. for (index, item) in coordinator.items.enumerated()
  84. {
  85. let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
  86. if collectionView === self.collectionView2
  87. {
  88. self.items2.insert(item.dragItem.localObject as! String, at: indexPath.row)
  89. }
  90. else
  91. {
  92. self.items1.insert(item.dragItem.localObject as! String, at: indexPath.row)
  93. }
  94. indexPaths.append(indexPath)
  95. }
  96. collectionView.insertItems(at: indexPaths)
  97. })
  98. }
  99. }
  100. // MARK: - UICollectionViewDataSource Methods
  101. @available(iOS 11, *)
  102. extension DragDropViewController : UICollectionViewDataSource
  103. {
  104. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
  105. {
  106. return collectionView == self.collectionView1 ? self.items1.count : self.items2.count
  107. }
  108. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
  109. {
  110. if collectionView == self.collectionView1
  111. {
  112. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! ScanCell
  113. let fileNamePath = CCUtility.getDirectoryPDFGenerator() + "/" + self.items1[indexPath.row]
  114. let data = try? Data(contentsOf: fileNamePath.url)
  115. cell.customImageView?.image = UIImage(data: data!)
  116. cell.customLabel.text = self.items1[indexPath.row].capitalized
  117. return cell
  118. }
  119. else
  120. {
  121. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! ScanCell
  122. let fileNamePath = CCUtility.getDirectoryPDFGenerator() + "/" + self.items2[indexPath.row]
  123. let data = try? Data(contentsOf: fileNamePath.url)
  124. cell.customImageView?.image = UIImage(data: data!)
  125. cell.customLabel.text = self.items2[indexPath.row].capitalized
  126. return cell
  127. }
  128. }
  129. }
  130. // MARK: - UICollectionViewDragDelegate Methods
  131. @available(iOS 11, *)
  132. extension DragDropViewController : UICollectionViewDragDelegate
  133. {
  134. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem]
  135. {
  136. let item = collectionView == collectionView1 ? self.items1[indexPath.row] : self.items2[indexPath.row]
  137. let itemProvider = NSItemProvider(object: item as NSString)
  138. let dragItem = UIDragItem(itemProvider: itemProvider)
  139. dragItem.localObject = item
  140. return [dragItem]
  141. }
  142. func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem]
  143. {
  144. let item = collectionView == collectionView1 ? self.items1[indexPath.row] : self.items2[indexPath.row]
  145. let itemProvider = NSItemProvider(object: item as NSString)
  146. let dragItem = UIDragItem(itemProvider: itemProvider)
  147. dragItem.localObject = item
  148. return [dragItem]
  149. }
  150. func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters?
  151. {
  152. if collectionView == collectionView1
  153. {
  154. let previewParameters = UIDragPreviewParameters()
  155. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 25, y: 25, width: 120, height: 120))
  156. return previewParameters
  157. }
  158. return nil
  159. }
  160. }
  161. // MARK: - UICollectionViewDropDelegate Methods
  162. @available(iOS 11, *)
  163. extension DragDropViewController : UICollectionViewDropDelegate
  164. {
  165. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool
  166. {
  167. return session.canLoadObjects(ofClass: NSString.self)
  168. }
  169. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal
  170. {
  171. if collectionView === self.collectionView1
  172. {
  173. if collectionView.hasActiveDrag
  174. {
  175. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  176. }
  177. else
  178. {
  179. return UICollectionViewDropProposal(operation: .forbidden)
  180. }
  181. }
  182. else
  183. {
  184. if collectionView.hasActiveDrag
  185. {
  186. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  187. }
  188. else
  189. {
  190. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  191. }
  192. }
  193. }
  194. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator)
  195. {
  196. let destinationIndexPath: IndexPath
  197. if let indexPath = coordinator.destinationIndexPath
  198. {
  199. destinationIndexPath = indexPath
  200. }
  201. else
  202. {
  203. // Get last index path of table view.
  204. let section = collectionView.numberOfSections - 1
  205. let row = collectionView.numberOfItems(inSection: section)
  206. destinationIndexPath = IndexPath(row: row, section: section)
  207. }
  208. switch coordinator.proposal.operation
  209. {
  210. case .move:
  211. self.reorderItems(coordinator: coordinator, destinationIndexPath:destinationIndexPath, collectionView: collectionView)
  212. break
  213. case .copy:
  214. self.copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  215. default:
  216. return
  217. }
  218. }
  219. }