ScanCollectionView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //
  2. // ScanCollectionView.swift
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 21/08/18.
  6. // Copyright (c) 2018 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. @available(iOS 11, *)
  25. class DragDropViewController: UIViewController {
  26. //MARK: Private Properties
  27. //Data Source for collectionViewSource
  28. private var itemsSource = [String]()
  29. //Data Source for collectionViewDestination
  30. private var itemsDestination = [String]()
  31. //AppDelegate
  32. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  33. //MARK: Outlets
  34. @IBOutlet weak var collectionViewSource: UICollectionView!
  35. @IBOutlet weak var collectionViewDestination: UICollectionView!
  36. @IBOutlet weak var cancel: UIBarButtonItem!
  37. @IBOutlet weak var save: UIBarButtonItem!
  38. @IBOutlet weak var labelTitlePDFzone: UILabel!
  39. //MARK: View Lifecycle Methods
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. self.collectionViewSource.dragInteractionEnabled = true
  43. self.collectionViewSource.dragDelegate = self
  44. self.collectionViewSource.dropDelegate = self
  45. self.collectionViewDestination.dragInteractionEnabled = true
  46. self.collectionViewDestination.dropDelegate = self
  47. self.collectionViewDestination.dragDelegate = self
  48. self.collectionViewDestination.reorderingCadence = .fast //default value - .immediate
  49. self.navigationItem.title = NSLocalizedString("_scanned_images_", comment: "")
  50. cancel.title = NSLocalizedString("_cancel_", comment: "")
  51. save.title = NSLocalizedString("_save_", comment: "")
  52. labelTitlePDFzone.text = NSLocalizedString("_scan_label_PDF_zone_", comment: "")
  53. }
  54. override func viewWillAppear(_ animated: Bool) {
  55. super.viewWillAppear(animated)
  56. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  57. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  58. labelTitlePDFzone.textColor = NCBrandColor.sharedInstance.brandText
  59. labelTitlePDFzone.backgroundColor = NCBrandColor.sharedInstance.brand
  60. loadImage(atPath: CCUtility.getDirectoryScan(), items: &itemsSource)
  61. }
  62. //MARK: Button Action
  63. @IBAction func cancelAction(sender: UIBarButtonItem) {
  64. self.dismiss(animated: true, completion: nil)
  65. }
  66. @IBAction func saveAction(sender: UIBarButtonItem) {
  67. // open create cloud
  68. }
  69. //MARK: Private Methods
  70. private func loadImage(atPath: String, items: inout [String]) {
  71. do {
  72. let directoryContents = try FileManager.default.contentsOfDirectory(atPath: atPath)
  73. for fileName in directoryContents {
  74. if fileName != "Select" && fileName.first != "." {
  75. items.append(fileName)
  76. }
  77. }
  78. } catch {
  79. print(error.localizedDescription)
  80. }
  81. }
  82. func filter(image: UIImage, contrast: Double) -> UIImage? {
  83. let ciImage = CIImage(image: image)!
  84. let imageFilter = ciImage.applyingFilter("CIColorControls", parameters: ["inputSaturation": 0, "inputContrast": contrast])
  85. return UIImage(ciImage: imageFilter)
  86. }
  87. /// 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.
  88. ///
  89. /// - Parameters:
  90. /// - coordinator: coordinator obtained from performDropWith: UICollectionViewDropDelegate method
  91. /// - destinationIndexPath: indexpath of the collection view where the user drops the element
  92. /// - collectionView: collectionView in which reordering needs to be done.
  93. private func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
  94. let items = coordinator.items
  95. if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath {
  96. var dIndexPath = destinationIndexPath
  97. if dIndexPath.row >= collectionView.numberOfItems(inSection: 0) {
  98. dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
  99. }
  100. collectionView.performBatchUpdates({
  101. if collectionView === self.collectionViewDestination {
  102. self.itemsDestination.remove(at: sourceIndexPath.row)
  103. self.itemsDestination.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  104. } else {
  105. self.itemsSource.remove(at: sourceIndexPath.row)
  106. self.itemsSource.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
  107. }
  108. collectionView.deleteItems(at: [sourceIndexPath])
  109. collectionView.insertItems(at: [dIndexPath])
  110. })
  111. coordinator.drop(items.first!.dragItem, toItemAt: dIndexPath)
  112. }
  113. }
  114. /// This method copies a cell from source indexPath in 1st collection view to destination indexPath in 2nd collection view. It works for multiple items.
  115. ///
  116. /// - Parameters:
  117. /// - coordinator: coordinator obtained from performDropWith: UICollectionViewDropDelegate method
  118. /// - destinationIndexPath: indexpath of the collection view where the user drops the element
  119. /// - collectionView: collectionView in which reordering needs to be done.
  120. private func copyItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
  121. {
  122. collectionView.performBatchUpdates({
  123. var indexPaths = [IndexPath]()
  124. for (index, item) in coordinator.items.enumerated() {
  125. let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
  126. if collectionView === self.collectionViewDestination {
  127. self.itemsDestination.insert(item.dragItem.localObject as! String, at: indexPath.row)
  128. } else {
  129. self.itemsSource.insert(item.dragItem.localObject as! String, at: indexPath.row)
  130. }
  131. indexPaths.append(indexPath)
  132. }
  133. collectionView.insertItems(at: indexPaths)
  134. })
  135. }
  136. private func moveItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView)
  137. {
  138. collectionView.performBatchUpdates({
  139. var indexPathsTo = [IndexPath]()
  140. for (index, item) in coordinator.items.enumerated() {
  141. let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
  142. if collectionView === self.collectionViewDestination {
  143. self.itemsDestination.insert(item.dragItem.localObject as! String, at: indexPath.row) // array
  144. } else {
  145. self.itemsSource.insert(item.dragItem.localObject as! String, at: indexPath.row) // array
  146. }
  147. indexPathsTo.append(indexPath)
  148. }
  149. collectionView.insertItems(at: indexPathsTo)
  150. })
  151. }
  152. }
  153. // MARK: - UICollectionViewDataSource Methods
  154. @available(iOS 11, *)
  155. extension DragDropViewController : UICollectionViewDataSource {
  156. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  157. return collectionView == self.collectionViewSource ? self.itemsSource.count : self.itemsDestination.count
  158. }
  159. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  160. if collectionView == self.collectionViewSource {
  161. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! ScanCell
  162. let fileNamePath = CCUtility.getDirectoryScan() + "/" + self.itemsSource[indexPath.row]
  163. guard let data = try? Data(contentsOf: fileNamePath.url) else {
  164. return cell
  165. }
  166. cell.customImageView?.image = UIImage(data: data)
  167. cell.delete.addTarget(self, action: #selector(deleteSource(_:)), for: .touchUpInside)
  168. return cell
  169. } else {
  170. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! ScanCell
  171. let fileNamePath = CCUtility.getDirectoryScan() + "/" + self.itemsDestination[indexPath.row]
  172. guard let data = try? Data(contentsOf: fileNamePath.url) else {
  173. return cell
  174. }
  175. guard let image = UIImage(data: data) else {
  176. return cell
  177. }
  178. cell.customImageView?.image = self.filter(image: image, contrast: 1)
  179. cell.customLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + " " + "\(indexPath.row+1)"
  180. cell.delete.addTarget(self, action: #selector(deleteDestination(_:)), for: .touchUpInside)
  181. return cell
  182. }
  183. }
  184. @objc func deleteSource(_ sender: UIButton) {
  185. let buttonPosition:CGPoint = sender.convert(.zero, to: self.collectionViewSource)
  186. let indexPath:IndexPath = self.collectionViewSource.indexPathForItem(at: buttonPosition)!
  187. }
  188. @objc func deleteDestination(_ sender:UIButton) {
  189. let buttonPosition:CGPoint = sender.convert(.zero, to: self.collectionViewDestination)
  190. let indexPath:IndexPath = self.collectionViewDestination.indexPathForItem(at: buttonPosition)!
  191. }
  192. }
  193. // MARK: - UICollectionViewDragDelegate Methods
  194. @available(iOS 11, *)
  195. extension DragDropViewController : UICollectionViewDragDelegate
  196. {
  197. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  198. let item = collectionView == collectionViewSource ? self.itemsSource[indexPath.row] : self.itemsDestination[indexPath.row]
  199. let itemProvider = NSItemProvider(object: item as NSString)
  200. let dragItem = UIDragItem(itemProvider: itemProvider)
  201. dragItem.localObject = item
  202. return [dragItem]
  203. }
  204. func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
  205. let item = collectionView == collectionViewSource ? self.itemsSource[indexPath.row] : self.itemsDestination[indexPath.row]
  206. let itemProvider = NSItemProvider(object: item as NSString)
  207. let dragItem = UIDragItem(itemProvider: itemProvider)
  208. dragItem.localObject = item
  209. return [dragItem]
  210. }
  211. func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
  212. if collectionView == collectionViewSource {
  213. let previewParameters = UIDragPreviewParameters()
  214. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 25, y: 25, width: 120, height: 120))
  215. return previewParameters
  216. }
  217. return nil
  218. }
  219. }
  220. // MARK: - UICollectionViewDropDelegate Methods
  221. @available(iOS 11, *)
  222. extension DragDropViewController : UICollectionViewDropDelegate {
  223. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  224. return session.canLoadObjects(ofClass: NSString.self)
  225. }
  226. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
  227. if collectionView == self.collectionViewSource {
  228. if collectionView.hasActiveDrag {
  229. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  230. } else {
  231. return UICollectionViewDropProposal(operation: .forbidden)
  232. }
  233. } else {
  234. if collectionView.hasActiveDrag {
  235. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  236. } else {
  237. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  238. }
  239. }
  240. }
  241. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  242. let destinationIndexPath: IndexPath
  243. if let indexPath = coordinator.destinationIndexPath {
  244. destinationIndexPath = indexPath
  245. } else {
  246. // Get last index path of table view.
  247. let section = collectionView.numberOfSections - 1
  248. let row = collectionView.numberOfItems(inSection: section)
  249. destinationIndexPath = IndexPath(row: row, section: section)
  250. }
  251. switch coordinator.proposal.operation {
  252. case .move:
  253. self.reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  254. break
  255. case .copy:
  256. self.moveItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  257. break
  258. default:
  259. return
  260. }
  261. }
  262. func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
  263. self.collectionViewDestination.reloadData()
  264. }
  265. }