ScanCollectionView.swift 14 KB

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