123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- import Foundation
- @available(iOS 13.0, *)
- extension NCScan: UICollectionViewDataSource {
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return collectionView == collectionViewSource ? itemsSource.count : imagesDestination.count
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- if collectionView == collectionViewSource {
- let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as? NCScanCell)!
- let fileNamePath = CCUtility.getDirectoryScan() + "/" + itemsSource[indexPath.row]
- guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePath)), var image = UIImage(data: data) else { return cell }
- let imageWidthInPixels = image.size.width * image.scale
- let imageHeightInPixels = image.size.height * image.scale
-
- if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
- image = image.resizeImage(size: CGSize(width: 595, height: 842), isAspectRation: true) ?? image
- }
- cell.customImageView?.image = image
- cell.delete.action(for: .touchUpInside) { sender in
- let buttonPosition: CGPoint = (sender as? UIButton)!.convert(.zero, to: self.collectionViewSource)
- if let indexPath = self.collectionViewSource.indexPathForItem(at: buttonPosition) {
- let fileNameAtPath = CCUtility.getDirectoryScan() + "/" + self.itemsSource[indexPath.row]
- CCUtility.removeFile(atPath: fileNameAtPath)
- self.itemsSource.remove(at: indexPath.row)
- self.collectionViewSource.deleteItems(at: [indexPath])
- }
- }
- return cell
- } else {
- let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as? NCScanCell)!
- cell.delegate = self
- cell.index = indexPath.row
- var image = imagesDestination[indexPath.row]
- let imageWidthInPixels = image.size.width * image.scale
- let imageHeightInPixels = image.size.height * image.scale
-
- if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
- image = image.resizeImage(size: CGSize(width: 595, height: 842), isAspectRation: true) ?? image
- }
- cell.customImageView?.image = filter(image: image)
- cell.customLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + " " + "\(indexPath.row + 1)"
- return cell
- }
- }
- }
- @available(iOS 13.0, *)
- extension NCScan: UICollectionViewDragDelegate {
- func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
- if collectionView == collectionViewSource {
- let item = itemsSource[indexPath.row]
- let itemProvider = NSItemProvider(object: item as NSString)
- let dragItem = UIDragItem(itemProvider: itemProvider)
- dragItem.localObject = item
- return [dragItem]
- } else {
- let item = imagesDestination[indexPath.row]
- let itemProvider = NSItemProvider(object: item as UIImage)
- let dragItem = UIDragItem(itemProvider: itemProvider)
- dragItem.localObject = item
- return [dragItem]
- }
- }
- func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
- if collectionView == collectionViewSource {
- let item = itemsSource[indexPath.row]
- let itemProvider = NSItemProvider(object: item as NSString)
- let dragItem = UIDragItem(itemProvider: itemProvider)
- dragItem.localObject = item
- return [dragItem]
- } else {
- let item = imagesDestination[indexPath.row]
- let itemProvider = NSItemProvider(object: item as UIImage)
- let dragItem = UIDragItem(itemProvider: itemProvider)
- dragItem.localObject = item
- return [dragItem]
- }
- }
- func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
- let previewParameters = UIDragPreviewParameters()
- if collectionView == collectionViewSource {
- previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 100, height: 100))
- } else {
- previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 20, y: 20, width: 80, height: 80))
- }
- return previewParameters
- }
- }
- @available(iOS 13.0, *)
- extension NCScan: UICollectionViewDropDelegate {
- func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
- return true
- }
- func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
- if collectionView == collectionViewSource {
- if collectionView.hasActiveDrag {
- return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
- } else {
- return UICollectionViewDropProposal(operation: .forbidden)
- }
- } else {
- if collectionView.hasActiveDrag {
- return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
- } else {
- return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
- }
- }
- }
- func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
- let destinationIndexPath: IndexPath
- switch coordinator.proposal.operation {
- case .move:
- if let indexPath = coordinator.destinationIndexPath {
- destinationIndexPath = indexPath
- } else {
-
- let section = collectionView.numberOfSections - 1
- let row = collectionView.numberOfItems(inSection: section)
- destinationIndexPath = IndexPath(row: row, section: section)
- }
- reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
- case .copy:
-
- let section = collectionView.numberOfSections - 1
- let row = collectionView.numberOfItems(inSection: section)
- destinationIndexPath = IndexPath(row: row, section: section)
- copyItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)
- default:
- return
- }
- }
- func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
- collectionViewDestination.reloadData()
-
- if imagesDestination.isEmpty {
- save.isEnabled = false
- } else {
- save.isEnabled = true
- }
- }
- }
|