ScanCollectionView.swift 10 KB

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