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