ScanCollectionView.swift 11 KB

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