NCScan+CollectionView.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. import QuickLook
  25. import NextcloudKit
  26. extension NCScan: UICollectionViewDataSource {
  27. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  28. if imagesDestination.isEmpty {
  29. save.isEnabled = false
  30. } else {
  31. save.isEnabled = true
  32. }
  33. return collectionView == collectionViewSource ? itemsSource.count : imagesDestination.count
  34. }
  35. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  36. if collectionView == collectionViewSource {
  37. guard let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as? NCScanCell) else { return NCScanCell() }
  38. let fileNamePath = utilityFileSystem.directoryScan + "/" + itemsSource[indexPath.row]
  39. guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePath)), var image = UIImage(data: data) else { return cell }
  40. let imageWidthInPixels = image.size.width * image.scale
  41. let imageHeightInPixels = image.size.height * image.scale
  42. // 72 DPI
  43. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  44. image = image.resizeImage(size: CGSize(width: 595, height: 842)) ?? image
  45. }
  46. cell.delegate = self
  47. cell.index = indexPath.row
  48. cell.customImageView?.image = image
  49. cell.delete.action(for: .touchUpInside) { sender in
  50. let buttonPosition: CGPoint = (sender as? UIButton)!.convert(.zero, to: self.collectionViewSource)
  51. if let indexPath = self.collectionViewSource.indexPathForItem(at: buttonPosition) {
  52. let fileNameAtPath = self.utilityFileSystem.directoryScan + "/" + self.itemsSource[indexPath.row]
  53. self.utilityFileSystem.removeFile(atPath: fileNameAtPath)
  54. self.itemsSource.remove(at: indexPath.row)
  55. self.collectionViewSource.deleteItems(at: [indexPath])
  56. }
  57. }
  58. cell.modify.action(for: .touchUpInside) { sender in
  59. let buttonPosition: CGPoint = (sender as? UIButton)!.convert(.zero, to: self.collectionViewSource)
  60. if let indexPath = self.collectionViewSource.indexPathForItem(at: buttonPosition) {
  61. let fileName = self.itemsSource[indexPath.row]
  62. let fileNameAtPath = NCUtilityFileSystem().directoryScan + "/" + fileName
  63. let fileNameToPath = NSTemporaryDirectory() + fileName
  64. NCUtilityFileSystem().copyFile(atPath: fileNameAtPath, toPath: fileNameToPath)
  65. let metadata = tableMetadata()
  66. metadata.classFile = NKCommon.TypeClassFile.image.rawValue
  67. let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNameToPath), fileNameSource: fileName, isEditingEnabled: true, metadata: metadata)
  68. viewerQuickLook.delegateQuickLook = self
  69. viewerQuickLook.saveAsCopyAlert = false
  70. viewerQuickLook.uploadMetadata = false
  71. let navigationController = UINavigationController(rootViewController: viewerQuickLook)
  72. navigationController.modalPresentationStyle = .fullScreen
  73. self.present(navigationController, animated: true)
  74. }
  75. }
  76. return cell
  77. } else {
  78. guard let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as? NCScanCell) else { return NCScanCell() }
  79. var image = imagesDestination[indexPath.row]
  80. let imageWidthInPixels = image.size.width * image.scale
  81. let imageHeightInPixels = image.size.height * image.scale
  82. // 72 DPI
  83. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  84. image = image.resizeImage(size: CGSize(width: 595, height: 842)) ?? image
  85. }
  86. cell.delegate = self
  87. cell.index = indexPath.row
  88. cell.customImageView?.image = filter(image: image)
  89. cell.customLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + " " + "\(indexPath.row + 1)"
  90. return cell
  91. }
  92. }
  93. }
  94. extension NCScan: UICollectionViewDragDelegate {
  95. func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  96. if collectionView == collectionViewSource {
  97. let item = itemsSource[indexPath.row]
  98. let itemProvider = NSItemProvider(object: item as NSString)
  99. let dragItem = UIDragItem(itemProvider: itemProvider)
  100. dragItem.localObject = item
  101. return [dragItem]
  102. } else {
  103. let item = imagesDestination[indexPath.row]
  104. let itemProvider = NSItemProvider(object: item as UIImage)
  105. let dragItem = UIDragItem(itemProvider: itemProvider)
  106. dragItem.localObject = item
  107. return [dragItem]
  108. }
  109. }
  110. func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
  111. if collectionView == collectionViewSource {
  112. let item = itemsSource[indexPath.row]
  113. let itemProvider = NSItemProvider(object: item as NSString)
  114. let dragItem = UIDragItem(itemProvider: itemProvider)
  115. dragItem.localObject = item
  116. return [dragItem]
  117. } else {
  118. let item = imagesDestination[indexPath.row]
  119. let itemProvider = NSItemProvider(object: item as UIImage)
  120. let dragItem = UIDragItem(itemProvider: itemProvider)
  121. dragItem.localObject = item
  122. return [dragItem]
  123. }
  124. }
  125. func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
  126. let previewParameters = UIDragPreviewParameters()
  127. if collectionView == collectionViewSource {
  128. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 100, height: 100))
  129. } else {
  130. previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 80, height: 80))
  131. }
  132. return previewParameters
  133. }
  134. }
  135. extension NCScan: UICollectionViewDropDelegate {
  136. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  137. return true
  138. }
  139. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
  140. if collectionView == collectionViewSource {
  141. if collectionView.hasActiveDrag {
  142. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  143. } else {
  144. return UICollectionViewDropProposal(operation: .forbidden)
  145. }
  146. } else {
  147. if collectionView.hasActiveDrag {
  148. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  149. } else {
  150. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  151. }
  152. }
  153. }
  154. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  155. let destinationIndexPath: IndexPath
  156. switch coordinator.proposal.operation {
  157. case .move:
  158. if let indexPath = coordinator.destinationIndexPath {
  159. destinationIndexPath = indexPath
  160. } else {
  161. // Get last index path of table view.
  162. let section = collectionView.numberOfSections - 1
  163. let row = collectionView.numberOfItems(inSection: section)
  164. destinationIndexPath = IndexPath(row: row, section: section)
  165. }
  166. reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  167. case .copy:
  168. // Get last index path of table view.
  169. let section = collectionView.numberOfSections - 1
  170. let row = collectionView.numberOfItems(inSection: section)
  171. destinationIndexPath = IndexPath(row: row, section: section)
  172. copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  173. default:
  174. return
  175. }
  176. }
  177. func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
  178. collectionViewDestination.reloadData()
  179. }
  180. }