ScanCollectionView.swift 11 KB

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