NCScan+CollectionView.swift 8.4 KB

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