NCScan+CollectionView.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. var image = imagesDestination[indexPath.row]
  59. let imageWidthInPixels = image.size.width * image.scale
  60. let imageHeightInPixels = image.size.height * image.scale
  61. // 72 DPI
  62. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  63. image = image.resizeImage(size: CGSize(width: 595, height: 842), isAspectRation: true) ?? image
  64. }
  65. cell.customImageView?.image = self.filter(image: image)
  66. cell.customLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + " " + "\(indexPath.row + 1)"
  67. cell.delete.action(for: .touchUpInside) { sender in
  68. let buttonPosition: CGPoint = (sender as? UIButton)!.convert(.zero, to: self.collectionViewDestination)
  69. if let indexPath = self.collectionViewDestination.indexPathForItem(at: buttonPosition) {
  70. self.imagesDestination.remove(at: indexPath.row)
  71. self.itemsDestination.remove(at: indexPath.row)
  72. self.collectionViewDestination.deleteItems(at: [indexPath])
  73. // Save button
  74. if self.imagesDestination.isEmpty {
  75. self.save.isEnabled = false
  76. } else {
  77. self.save.isEnabled = true
  78. }
  79. }
  80. }
  81. cell.rotate.action(for: .touchUpInside) { sender in
  82. let buttonPosition: CGPoint = (sender as? UIButton)!.convert(.zero, to: self.collectionViewDestination)
  83. if let indexPath = self.collectionViewDestination.indexPathForItem(at: buttonPosition), let cell = self.collectionViewDestination.cellForItem(at: indexPath) as? NCScanCell {
  84. var image = self.imagesDestination[indexPath.row]
  85. image = image.rotate(radians: .pi / 2)!
  86. self.imagesDestination[indexPath.row] = image
  87. cell.customImageView.image = image
  88. }
  89. }
  90. return cell
  91. }
  92. }
  93. }
  94. @available(iOS 13.0, *)
  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. @available(iOS 13.0, *)
  137. extension NCScan: UICollectionViewDropDelegate {
  138. func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
  139. return true // session.canLoadObjects(ofClass: NSString.self)
  140. }
  141. func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
  142. if collectionView == collectionViewSource {
  143. if collectionView.hasActiveDrag {
  144. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  145. } else {
  146. return UICollectionViewDropProposal(operation: .forbidden)
  147. }
  148. } else {
  149. if collectionView.hasActiveDrag {
  150. return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
  151. } else {
  152. return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
  153. }
  154. }
  155. }
  156. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  157. let destinationIndexPath: IndexPath
  158. switch coordinator.proposal.operation {
  159. case .move:
  160. if let indexPath = coordinator.destinationIndexPath {
  161. destinationIndexPath = indexPath
  162. } else {
  163. // Get last index path of table view.
  164. let section = collectionView.numberOfSections - 1
  165. let row = collectionView.numberOfItems(inSection: section)
  166. destinationIndexPath = IndexPath(row: row, section: section)
  167. }
  168. self.reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  169. case .copy:
  170. // Get last index path of table view.
  171. let section = collectionView.numberOfSections - 1
  172. let row = collectionView.numberOfItems(inSection: section)
  173. destinationIndexPath = IndexPath(row: row, section: section)
  174. self.copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
  175. default:
  176. return
  177. }
  178. }
  179. func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
  180. collectionViewDestination.reloadData()
  181. // Save button
  182. if imagesDestination.isEmpty {
  183. save.isEnabled = false
  184. } else {
  185. save.isEnabled = true
  186. }
  187. }
  188. }