NCScan+CollectionView.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // NCScan+CollectionView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 22/02/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 Foundation
  24. @available(iOS 13.0, *)
  25. extension NCScan: UICollectionViewDataSource {
  26. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  27. return collectionView == collectionViewSource ? itemsSource.count : imagesDestination.count
  28. }
  29. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  30. if collectionView == collectionViewSource {
  31. let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as? NCScanCell)!
  32. let fileNamePath = CCUtility.getDirectoryScan() + "/" + itemsSource[indexPath.row]
  33. guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePath)), var image = UIImage(data: data) else { return cell }
  34. let imageWidthInPixels = image.size.width * image.scale
  35. let imageHeightInPixels = image.size.height * image.scale
  36. // 72 DPI
  37. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  38. image = image.resizeImage(size: CGSize(width: 595, height: 842), isAspectRation: true) ?? image
  39. }
  40. cell.customImageView?.image = image
  41. cell.delete.action(for: .touchUpInside) { sender in
  42. let buttonPosition: CGPoint = (sender as? UIButton)!.convert(.zero, to: self.collectionViewSource)
  43. if let indexPath = self.collectionViewSource.indexPathForItem(at: buttonPosition) {
  44. let fileNameAtPath = CCUtility.getDirectoryScan() + "/" + self.itemsSource[indexPath.row]
  45. CCUtility.removeFile(atPath: fileNameAtPath)
  46. self.itemsSource.remove(at: indexPath.row)
  47. self.collectionViewSource.deleteItems(at: [indexPath])
  48. }
  49. }
  50. return cell
  51. } else {
  52. let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as? NCScanCell)!
  53. cell.delegate = self
  54. cell.index = indexPath.row
  55. var image = imagesDestination[indexPath.row]
  56. let imageWidthInPixels = image.size.width * image.scale
  57. let imageHeightInPixels = image.size.height * image.scale
  58. // 72 DPI
  59. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  60. image = image.resizeImage(size: CGSize(width: 595, height: 842), isAspectRation: true) ?? image
  61. }
  62. cell.customImageView?.image = filter(image: image)
  63. cell.customLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + " " + "\(indexPath.row + 1)"
  64. return cell
  65. }
  66. }
  67. }
  68. @available(iOS 13.0, *)
  69. extension NCScan: UICollectionViewDragDelegate {
  70. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  71. if collectionView == collectionViewSource {
  72. let item = itemsSource[indexPath.row]
  73. let itemProvider = NSItemProvider(object: item as NSString)
  74. let dragItem = UIDragItem(itemProvider: itemProvider)
  75. dragItem.localObject = item
  76. return [dragItem]
  77. } else {
  78. let item = imagesDestination[indexPath.row]
  79. let itemProvider = NSItemProvider(object: item as UIImage)
  80. let dragItem = UIDragItem(itemProvider: itemProvider)
  81. dragItem.localObject = item
  82. return [dragItem]
  83. }
  84. }
  85. func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
  86. if collectionView == collectionViewSource {
  87. let item = itemsSource[indexPath.row]
  88. let itemProvider = NSItemProvider(object: item as NSString)
  89. let dragItem = UIDragItem(itemProvider: itemProvider)
  90. dragItem.localObject = item
  91. return [dragItem]
  92. } else {
  93. let item = imagesDestination[indexPath.row]
  94. let itemProvider = NSItemProvider(object: item as UIImage)
  95. let dragItem = UIDragItem(itemProvider: itemProvider)
  96. dragItem.localObject = item
  97. return [dragItem]
  98. }
  99. }
  100. func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
  101. let previewParameters = UIDragPreviewParameters()
  102. if collectionView == collectionViewSource {
  103. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 100, height: 100))
  104. } else {
  105. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 80, height: 80))
  106. }
  107. return previewParameters
  108. }
  109. }
  110. @available(iOS 13.0, *)
  111. extension NCScan: UICollectionViewDropDelegate {
  112. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  113. return true // session.canLoadObjects(ofClass: NSString.self)
  114. }
  115. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
  116. if collectionView == collectionViewSource {
  117. if collectionView.hasActiveDrag {
  118. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  119. } else {
  120. return UICollectionViewDropProposal(operation: .forbidden)
  121. }
  122. } else {
  123. if collectionView.hasActiveDrag {
  124. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  125. } else {
  126. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  127. }
  128. }
  129. }
  130. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  131. let destinationIndexPath: IndexPath
  132. switch coordinator.proposal.operation {
  133. case .move:
  134. if let indexPath = coordinator.destinationIndexPath {
  135. destinationIndexPath = indexPath
  136. } else {
  137. // Get last index path of table view.
  138. let section = collectionView.numberOfSections - 1
  139. let row = collectionView.numberOfItems(inSection: section)
  140. destinationIndexPath = IndexPath(row: row, section: section)
  141. }
  142. reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  143. case .copy:
  144. // Get last index path of table view.
  145. let section = collectionView.numberOfSections - 1
  146. let row = collectionView.numberOfItems(inSection: section)
  147. destinationIndexPath = IndexPath(row: row, section: section)
  148. copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  149. default:
  150. return
  151. }
  152. }
  153. func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
  154. collectionViewDestination.reloadData()
  155. // Save button
  156. if imagesDestination.isEmpty {
  157. save.isEnabled = false
  158. } else {
  159. save.isEnabled = true
  160. }
  161. }
  162. }