NCScan+CollectionView.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. extension NCScan: UICollectionViewDataSource {
  25. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  26. return collectionView == collectionViewSource ? itemsSource.count : imagesDestination.count
  27. }
  28. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  29. if collectionView == collectionViewSource {
  30. let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as? NCScanCell)!
  31. let fileNamePath = CCUtility.getDirectoryScan() + "/" + itemsSource[indexPath.row]
  32. guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePath)), var image = UIImage(data: data) else { return cell }
  33. let imageWidthInPixels = image.size.width * image.scale
  34. let imageHeightInPixels = image.size.height * image.scale
  35. // 72 DPI
  36. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  37. image = image.resizeImage(size: CGSize(width: 595, height: 842)) ?? image
  38. }
  39. cell.indexPath = indexPath
  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. cell.indexPath = indexPath
  56. var image = imagesDestination[indexPath.row]
  57. let imageWidthInPixels = image.size.width * image.scale
  58. let imageHeightInPixels = image.size.height * image.scale
  59. // 72 DPI
  60. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  61. image = image.resizeImage(size: CGSize(width: 595, height: 842)) ?? image
  62. }
  63. cell.customImageView?.image = filter(image: image)
  64. cell.customLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + " " + "\(indexPath.row + 1)"
  65. return cell
  66. }
  67. }
  68. }
  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. extension NCScan: UICollectionViewDropDelegate {
  111. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  112. return true // session.canLoadObjects(ofClass: NSString.self)
  113. }
  114. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
  115. if collectionView == collectionViewSource {
  116. if collectionView.hasActiveDrag {
  117. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  118. } else {
  119. return UICollectionViewDropProposal(operation: .forbidden)
  120. }
  121. } else {
  122. if collectionView.hasActiveDrag {
  123. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  124. } else {
  125. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  126. }
  127. }
  128. }
  129. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  130. let destinationIndexPath: IndexPath
  131. switch coordinator.proposal.operation {
  132. case .move:
  133. if let indexPath = coordinator.destinationIndexPath {
  134. destinationIndexPath = indexPath
  135. } else {
  136. // Get last index path of table view.
  137. let section = collectionView.numberOfSections - 1
  138. let row = collectionView.numberOfItems(inSection: section)
  139. destinationIndexPath = IndexPath(row: row, section: section)
  140. }
  141. reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  142. case .copy:
  143. // Get last index path of table view.
  144. let section = collectionView.numberOfSections - 1
  145. let row = collectionView.numberOfItems(inSection: section)
  146. destinationIndexPath = IndexPath(row: row, section: section)
  147. copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  148. default:
  149. return
  150. }
  151. }
  152. func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
  153. collectionViewDestination.reloadData()
  154. // Save button
  155. if imagesDestination.isEmpty {
  156. save.isEnabled = false
  157. } else {
  158. save.isEnabled = true
  159. }
  160. }
  161. }