NCScan+CollectionView.swift 8.2 KB

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