ScanCollectionView.swift 11 KB

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