浏览代码

coding

Signed-off-by: marinofaggiana <ios@nextcloud.com>
marinofaggiana 3 年之前
父节点
当前提交
1dc4181138

+ 2 - 28
iOSClient/ScanDocument/NCScan+CollectionView.swift

@@ -74,6 +74,8 @@ extension NCScan: UICollectionViewDataSource {
         } else {
 
             let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as? NCScanCell)!
+            cell.delegate = self
+            cell.imageIndex = indexPath.row
 
             var image = imagesDestination[indexPath.row]
 
@@ -87,34 +89,6 @@ extension NCScan: UICollectionViewDataSource {
 
             cell.customImageView?.image = self.filter(image: image)
             cell.customLabel.text = NSLocalizedString("_scan_document_pdf_page_", comment: "") + " " + "\(indexPath.row + 1)"
-            cell.delete.action(for: .touchUpInside) { sender in
-
-                let buttonPosition: CGPoint = (sender as? UIButton)!.convert(.zero, to: self.collectionViewDestination)
-                if let indexPath = self.collectionViewDestination.indexPathForItem(at: buttonPosition) {
-
-                    self.imagesDestination.remove(at: indexPath.row)
-                    self.itemsDestination.remove(at: indexPath.row)
-                    self.collectionViewDestination.deleteItems(at: [indexPath])
-
-                    // Save button
-                    if self.imagesDestination.isEmpty {
-                        self.save.isEnabled = false
-                    } else {
-                        self.save.isEnabled = true
-                    }
-                }
-            }
-            cell.rotate.action(for: .touchUpInside) { sender in
-
-                let buttonPosition: CGPoint = (sender as? UIButton)!.convert(.zero, to: self.collectionViewDestination)
-                if let indexPath = self.collectionViewDestination.indexPathForItem(at: buttonPosition), let cell = self.collectionViewDestination.cellForItem(at: indexPath) as? NCScanCell {
-
-                    var image = self.imagesDestination[indexPath.row]
-                    image = image.rotate(radians: .pi / 2)!
-                    self.imagesDestination[indexPath.row] = image
-                    cell.customImageView.image = image
-                }
-            }
 
             return cell
         }

+ 6 - 0
iOSClient/ScanDocument/NCScan.storyboard

@@ -105,6 +105,9 @@
                                                         <constraint firstAttribute="height" constant="23" id="QOj-Nj-nAA"/>
                                                     </constraints>
                                                     <state key="normal" image="deleteScan"/>
+                                                    <connections>
+                                                        <action selector="touchUpInsideDelete:" destination="Pph-tY-PGX" eventType="touchUpInside" id="KcT-WM-s1K"/>
+                                                    </connections>
                                                 </button>
                                                 <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XEo-o0-dSF" userLabel="Rotate">
                                                     <rect key="frame" x="98" y="0.0" width="22" height="22"/>
@@ -113,6 +116,9 @@
                                                         <constraint firstAttribute="height" constant="22" id="fd5-QY-wlr"/>
                                                     </constraints>
                                                     <state key="normal" image="rotate"/>
+                                                    <connections>
+                                                        <action selector="touchUpInsideRotate:" destination="Pph-tY-PGX" eventType="touchUpInside" id="x5z-go-m4Y"/>
+                                                    </connections>
                                                 </button>
                                             </subviews>
                                         </view>

+ 28 - 1
iOSClient/ScanDocument/NCScan.swift

@@ -24,7 +24,7 @@
 import UIKit
 
 @available(iOS 13.0, *)
-class NCScan: UIViewController {
+class NCScan: UIViewController, NCScanCellCellDelegate {
 
     // Data Source for collectionViewSource
     internal var itemsSource: [String] = []
@@ -353,6 +353,33 @@ class NCScan: UIViewController {
             loadImage()
         }
     }
+
+    func delete(with imageIndex: Int, sender: Any) {
+
+        imagesDestination.remove(at: imageIndex)
+        itemsDestination.remove(at: imageIndex)
+
+        // Save button
+        if imagesDestination.isEmpty {
+            save.isEnabled = false
+        } else {
+            save.isEnabled = true
+        }
+
+        collectionViewDestination.reloadData()
+    }
+
+    func rotate(with imageIndex: Int, sender: Any) {
+
+        let indexPath = IndexPath(row: imageIndex, section: 0)
+        if let cell = self.collectionViewDestination.cellForItem(at: indexPath) as? NCScanCell {
+
+            var image = self.imagesDestination[imageIndex]
+            image = image.rotate(radians: .pi / 2)!
+            imagesDestination[imageIndex] = image
+            cell.customImageView.image = image
+        }
+    }
 }
 
 extension UIImage {

+ 25 - 0
iOSClient/ScanDocument/NCScanCell.swift

@@ -29,4 +29,29 @@ class NCScanCell: UICollectionViewCell {
     @IBOutlet weak var customLabel: UILabel!
     @IBOutlet weak var delete: UIButton!
     @IBOutlet weak var rotate: UIButton!
+
+    weak var delegate: NCScanCellCellDelegate?
+    internal var index = 0
+
+    var imageIndex: Int {
+        get {
+            return index
+        }
+        set {
+            index = newValue
+        }
+    }
+
+    @IBAction func touchUpInsideDelete(_ sender: Any) {
+        delegate?.delete(with: index, sender: sender)
+    }
+
+    @IBAction func touchUpInsideRotate(_ sender: Any) {
+        delegate?.rotate(with: index, sender: sender)
+    }
+}
+
+protocol NCScanCellCellDelegate: AnyObject {
+    func delete(with index: Int, sender: Any)
+    func rotate(with index: Int, sender: Any)
 }