Browse Source

Improve Share rename / remove file

- rename: tap the cell / the file name
- remove: tap "x" button (no confirmation needed, as it is non-destructive)
- don't show remove button for only one element. Use cancel instead.

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch 3 years ago
parent
commit
b919be0701

+ 4 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -22,6 +22,7 @@
 		AF22B207277B4E4C00DAB0CC /* NCCreateFormUploadConflict.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F704B5E22430AA6F00632F5F /* NCCreateFormUploadConflict.storyboard */; };
 		AF22B208277B4E4C00DAB0CC /* NCCreateFormUploadConflictCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F704B5E82430C0B800632F5F /* NCCreateFormUploadConflictCell.swift */; };
 		AF22B209277B4E4C00DAB0CC /* NCCreateFormUploadConflictCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = F704B5E62430C06700632F5F /* NCCreateFormUploadConflictCell.xib */; };
+		AF22B20C277C6F4D00DAB0CC /* NCShareCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF22B20B277C6F4D00DAB0CC /* NCShareCell.swift */; };
 		AF2AC8CC27620F9300F249C7 /* XLForm in Frameworks */ = {isa = PBXBuildFile; productRef = AF2AC8CB27620F9300F249C7 /* XLForm */; };
 		AF2D7C7C2742556F00ADF566 /* NCShareLinkCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF2D7C7B2742556F00ADF566 /* NCShareLinkCell.swift */; };
 		AF2D7C7E2742559100ADF566 /* NCShareUserCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF2D7C7D2742559100ADF566 /* NCShareUserCell.swift */; };
@@ -451,6 +452,7 @@
 		371B5A3223D0BD5500FAFAE9 /* FloatingPanel.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FloatingPanel.framework; path = Carthage/Build/iOS/FloatingPanel.framework; sourceTree = "<group>"; };
 		3781B9AF23DB2B7E006B4B1D /* AppDelegate+Menu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+Menu.swift"; sourceTree = "<group>"; };
 		8491B1CC273BBA82001C8C5B /* UIViewController+Menu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Menu.swift"; sourceTree = "<group>"; };
+		AF22B20B277C6F4D00DAB0CC /* NCShareCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCShareCell.swift; sourceTree = "<group>"; };
 		AF2D7C7B2742556F00ADF566 /* NCShareLinkCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCShareLinkCell.swift; sourceTree = "<group>"; };
 		AF2D7C7D2742559100ADF566 /* NCShareUserCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCShareUserCell.swift; sourceTree = "<group>"; };
 		AF4BF613275629E20081CEEF /* NCManageDatabase+Account.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NCManageDatabase+Account.swift"; sourceTree = "<group>"; };
@@ -1440,6 +1442,7 @@
 			children = (
 				F714803A262EBE3900693E51 /* MainInterface.storyboard */,
 				F7148040262EBE4000693E51 /* NCShareExtension.swift */,
+				AF22B20B277C6F4D00DAB0CC /* NCShareCell.swift */,
 				F7148046262EBE4B00693E51 /* Share-Bridging-Header.h */,
 			);
 			path = Share;
@@ -2256,6 +2259,7 @@
 				F7707687263A853700A1BA94 /* NCContentPresenter.swift in Sources */,
 				F70460532499095400BB98A7 /* NotificationCenter+MainThread.swift in Sources */,
 				F70BFC7520E0FA7D00C67599 /* NCUtility.swift in Sources */,
+				AF22B20C277C6F4D00DAB0CC /* NCShareCell.swift in Sources */,
 				F79B646126CA661600838ACA /* UIControl+Extensions.swift in Sources */,
 				F7EDE4CC262D7B6F00414FE6 /* NCEmptyDataSet.swift in Sources */,
 				AF4BF61A27562A4B0081CEEF /* NCManageDatabse+Metadata.swift in Sources */,

+ 17 - 8
Share/MainInterface.storyboard

@@ -1,9 +1,9 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="EAU-PF-EEd">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="EAU-PF-EEd">
     <device id="retina6_5" orientation="portrait" appearance="light"/>
     <dependencies>
         <deployment identifier="iOS"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19519"/>
         <capability name="Safe area layout guides" minToolsVersion="9.0"/>
         <capability name="System colors in document resources" minToolsVersion="11.0"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@@ -46,39 +46,42 @@
                                         <rect key="frame" x="10" y="70" width="394" height="220"/>
                                         <color key="backgroundColor" systemColor="systemBackgroundColor"/>
                                         <prototypes>
-                                            <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" reuseIdentifier="Cell" rowHeight="50" id="6Kt-n0-i8J">
-                                                <rect key="frame" x="0.0" y="24.333333969116211" width="394" height="50"/>
+                                            <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" reuseIdentifier="Cell" rowHeight="50" id="6Kt-n0-i8J" customClass="NCShareCell" customModule="Share" customModuleProvider="target">
+                                                <rect key="frame" x="0.0" y="44.666666030883789" width="394" height="50"/>
                                                 <autoresizingMask key="autoresizingMask"/>
                                                 <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="6Kt-n0-i8J" id="WGN-Zn-lR8">
                                                     <rect key="frame" x="0.0" y="0.0" width="394" height="50"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <subviews>
-                                                        <imageView clipsSubviews="YES" userInteractionEnabled="NO" tag="10" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="uvl-De-S9p">
+                                                        <imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="uvl-De-S9p">
                                                             <rect key="frame" x="10" y="5" width="40" height="40"/>
                                                             <constraints>
                                                                 <constraint firstAttribute="width" constant="40" id="Vpb-6e-ta9"/>
                                                                 <constraint firstAttribute="height" constant="40" id="wvf-Ey-woY"/>
                                                             </constraints>
                                                         </imageView>
-                                                        <label opaque="NO" userInteractionEnabled="NO" tag="20" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="filename" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rz6-pe-DB5">
+                                                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="filename" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rz6-pe-DB5">
                                                             <rect key="frame" x="60" y="10" width="284" height="17"/>
                                                             <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                                             <nil key="textColor"/>
                                                             <nil key="highlightedColor"/>
                                                         </label>
-                                                        <label opaque="NO" userInteractionEnabled="NO" tag="40" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="size" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="QFh-Vy-b4Z">
+                                                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="size" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="QFh-Vy-b4Z">
                                                             <rect key="frame" x="60" y="29.999999999999996" width="284" height="14.333333333333332"/>
                                                             <fontDescription key="fontDescription" type="system" pointSize="12"/>
                                                             <color key="textColor" systemColor="systemGray2Color"/>
                                                             <nil key="highlightedColor"/>
                                                         </label>
-                                                        <button opaque="NO" tag="30" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="OGY-95-ZYi" customClass="NCShareExtensionButtonWithIndexPath" customModule="Share" customModuleProvider="target">
+                                                        <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="OGY-95-ZYi">
                                                             <rect key="frame" x="354" y="10" width="30" height="30"/>
                                                             <constraints>
                                                                 <constraint firstAttribute="height" constant="30" id="QqR-Uw-RnV"/>
                                                                 <constraint firstAttribute="width" constant="30" id="bpm-t5-3HA"/>
                                                             </constraints>
                                                             <state key="normal" image="more"/>
+                                                            <connections>
+                                                                <action selector="buttonTapped:" destination="6Kt-n0-i8J" eventType="touchUpInside" id="Xx7-1X-AhZ"/>
+                                                            </connections>
                                                         </button>
                                                     </subviews>
                                                     <constraints>
@@ -95,6 +98,12 @@
                                                     </constraints>
                                                 </tableViewCellContentView>
                                                 <inset key="separatorInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
+                                                <connections>
+                                                    <outlet property="fileNameCell" destination="rz6-pe-DB5" id="e8w-G7-bBe"/>
+                                                    <outlet property="imageCell" destination="uvl-De-S9p" id="S4F-TD-YGI"/>
+                                                    <outlet property="moreButton" destination="OGY-95-ZYi" id="vA3-mU-S1e"/>
+                                                    <outlet property="sizeCell" destination="QFh-Vy-b4Z" id="rmM-qr-QXU"/>
+                                                </connections>
                                             </tableViewCell>
                                         </prototypes>
                                         <connections>

+ 53 - 0
Share/NCShareCell.swift

@@ -0,0 +1,53 @@
+//
+//  NCShareCell.swift
+//  Share
+//
+//  Created by Henrik Storch on 29.12.21.
+//  Copyright © 2021 Marino Faggiana. All rights reserved.
+//
+
+import UIKit
+import NCCommunication
+
+protocol NCShareCellDelegate {
+    func removeFile(named fileName: String)
+}
+
+class NCShareCell: UITableViewCell {
+    @IBOutlet weak var imageCell: UIImageView!
+    @IBOutlet weak var fileNameCell: UILabel!
+    @IBOutlet weak var moreButton: UIButton!
+    @IBOutlet weak var sizeCell: UILabel!
+    var delegate: NCShareCellDelegate?
+    var fileName = ""
+
+    func setup(fileName: String) {
+        self.fileName = fileName
+        let resultInternalType = NCCommunicationCommon.shared.getInternalType(fileName: fileName, mimeType: "", directory: false)
+
+        backgroundColor = NCBrandColor.shared.systemBackground
+        imageCell?.layer.cornerRadius = 6
+        imageCell?.layer.masksToBounds = true
+
+        if let image = UIImage(contentsOfFile: (NSTemporaryDirectory() + fileName)) {
+            imageCell?.image = image.resizeImage(size: CGSize(width: 80, height: 80), isAspectRation: true)
+        } else {
+            if resultInternalType.iconName.count > 0 {
+                imageCell?.image = UIImage(named: resultInternalType.iconName)
+            } else {
+                imageCell?.image = NCBrandColor.cacheImages.file
+            }
+        }
+
+        fileNameCell?.text = fileName
+
+        let fileSize = NCUtilityFileSystem.shared.getFileSize(filePath: (NSTemporaryDirectory() + fileName))
+        sizeCell?.text = CCUtility.transformedSize(fileSize)
+        
+        moreButton?.setImage(NCUtility.shared.loadImage(named: "deleteScan").image(color: NCBrandColor.shared.label, size: 15), for: .normal)
+    }
+
+    @IBAction func buttonTapped(_ sender: Any) {
+        delegate?.removeFile(named: fileName)
+    }
+}

+ 37 - 130
Share/NCShareExtension.swift

@@ -27,7 +27,7 @@ import UIKit
 import NCCommunication
 import IHProgressHUD
 
-class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDelegate, NCRenameFileDelegate, NCAccountRequestDelegate {
+class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDelegate, NCAccountRequestDelegate {
 
     @IBOutlet weak var collectionView: UICollectionView!
     @IBOutlet weak var tableView: UITableView!
@@ -493,16 +493,6 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
         }
     }
 
-    func rename(fileName: String, fileNameNew: String) {
-        guard let fileIx = self.filesName.firstIndex(where: { $0 == fileName }),
-              !self.filesName.contains(fileNameNew),
-              NCUtilityFileSystem.shared.moveFile(atPath: (NSTemporaryDirectory() + fileName), toPath: (NSTemporaryDirectory() + fileNameNew))
-        else { return }
-
-        filesName[fileIx] = fileNameNew
-        tableView.reloadData()
-    }
-    
     func accountRequestChangeAccount(account: String) {
         setAccount(account: account)
     }
@@ -651,6 +641,18 @@ extension NCShareExtension: UITableViewDelegate {
     }
 
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        let fileName = filesName[indexPath.row]
+
+        guard let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile else  { return }
+        
+        let resultInternalType = NCCommunicationCommon.shared.getInternalType(fileName: fileName, mimeType: "", directory: false)
+        vcRename.delegate = self
+        vcRename.fileName = fileName
+        let img = UIImage(contentsOfFile: (NSTemporaryDirectory() + fileName)) ?? UIImage(named: resultInternalType.iconName) ?? NCBrandColor.cacheImages.file
+        vcRename.imagePreview = img
+        let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
+
+        self.present(popup, animated: true)
     }
 }
 
@@ -662,77 +664,36 @@ extension NCShareExtension: UITableViewDataSource {
 
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
-        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
-        cell.backgroundColor = NCBrandColor.shared.systemBackground
-
-        let imageCell = cell.viewWithTag(10) as? UIImageView
-        let fileNameCell = cell.viewWithTag(20) as? UILabel
-        let moreButton = cell.viewWithTag(30) as? NCShareExtensionButtonWithIndexPath
-        let sizeCell = cell.viewWithTag(40) as? UILabel
-
-        imageCell?.layer.cornerRadius = 6
-        imageCell?.layer.masksToBounds = true
-
+        guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NCShareCell else { return UITableViewCell() }
         let fileName = filesName[indexPath.row]
-        let resultInternalType = NCCommunicationCommon.shared.getInternalType(fileName: fileName, mimeType: "", directory: false)
+        cell.setup(fileName: fileName)
+        cell.delegate = self
+        cell.moreButton?.isHidden = filesName.count < 2
+
+        return cell
+    }
+}
 
-        if let image = UIImage(contentsOfFile: (NSTemporaryDirectory() + fileName)) {
-            imageCell?.image = image.resizeImage(size: CGSize(width: 80, height: 80), isAspectRation: true)
+extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate {
+    func removeFile(named fileName: String) {
+        print(#line, self.filesName.firstIndex(of: fileName), fileName)
+        guard let index = self.filesName.firstIndex(of: fileName) else { return }
+        self.filesName.remove(at: index)
+        if self.filesName.count == 0 {
+            self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
         } else {
-            if resultInternalType.iconName.count > 0 {
-                imageCell?.image = UIImage(named: resultInternalType.iconName)
-            } else {
-                imageCell?.image = NCBrandColor.cacheImages.file
-            }
+            self.setCommandView()
         }
+    }
 
-        fileNameCell?.text = fileName
-
-        let fileSize = NCUtilityFileSystem.shared.getFileSize(filePath: (NSTemporaryDirectory() + fileName))
-        sizeCell?.text = CCUtility.transformedSize(fileSize)
-
-        moreButton?.setImage(NCUtility.shared.loadImage(named: "more").image(color: NCBrandColor.shared.label, size: 15), for: .normal)
-        moreButton?.indexPath = indexPath
-        moreButton?.fileName = fileName
-        moreButton?.image = imageCell?.image
-        moreButton?.action(for: .touchUpInside, { sender in
-
-            if let fileName = (sender as! NCShareExtensionButtonWithIndexPath).fileName {
-                let alertController = UIAlertController(title: "", message: fileName, preferredStyle: .alert)
-
-                alertController.addAction(UIAlertAction(title: NSLocalizedString("_delete_file_", comment: ""), style: .default) { (_: UIAlertAction) in
-                    if let index = self.filesName.firstIndex(of: fileName) {
-
-                        self.filesName.remove(at: index)
-                        if self.filesName.count == 0 {
-                            self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
-                        } else {
-                            self.setCommandView()
-                        }
-                    }
-                })
-
-                alertController.addAction(UIAlertAction(title: NSLocalizedString("_rename_file_", comment: ""), style: .default) { (_: UIAlertAction) in
-
-                    if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
-
-                        vcRename.delegate = self
-                        vcRename.fileName = fileName
-                        vcRename.imagePreview = (sender as! NCShareExtensionButtonWithIndexPath).image
-
-                        let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
-
-                        self.present(popup, animated: true)
-                    }
-                })
-
-                alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in })
-
-                self.present(alertController, animated: true, completion: nil)
-            }
-        })
+    func rename(fileName: String, fileNameNew: String) {
+        guard let fileIx = self.filesName.firstIndex(of: fileName),
+              !self.filesName.contains(fileNameNew),
+              NCUtilityFileSystem.shared.moveFile(atPath: (NSTemporaryDirectory() + fileName), toPath: (NSTemporaryDirectory() + fileNameNew))
+        else { return }
 
-        return cell
+        filesName[fileIx] = fileNameNew
+        tableView.reloadData()
     }
 }
 
@@ -968,60 +929,6 @@ extension NCShareExtension {
     }
 }
 
-/*
-let task = URLSession.shared.downloadTask(with: urlitem) { localURL, urlResponse, error in
-    
-    if let localURL = localURL {
-        
-        if fileNameOriginal != nil {
-            fileName =  fileNameOriginal!
-        } else {
-            let ext = url.pathExtension
-            fileName = "\(dateFormatter.string(from: Date()))\(conuter)." + ext
-        }
-        
-        let filenamePath = NSTemporaryDirectory() + fileName
-      
-        do {
-            try FileManager.default.removeItem(atPath: filenamePath)
-        }
-        catch { }
-        
-        do {
-            try FileManager.default.copyItem(atPath: localURL.path, toPath:filenamePath)
-            
-            do {
-                let attr : NSDictionary? = try FileManager.default.attributesOfItem(atPath: filenamePath) as NSDictionary?
-                
-                if let _attr = attr {
-                    if _attr.fileSize() > 0 {
-                        
-                        filesName.append(fileName)
-                    }
-                }
-                
-            } catch let error {
-                outError = error
-            }
-            
-        } catch let error {
-            outError = error
-        }
-    }
-    
-    if index + 1 == attachments.count {
-        completion(filesName, outError)
-    }
-}
-task.resume()
-*/
-
-class NCShareExtensionButtonWithIndexPath: UIButton {
-    var indexPath: IndexPath?
-    var fileName: String?
-    var image: UIImage?
-}
-
 extension NCShareExtension: NCCreateFormUploadConflictDelegate {
     func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
         metadatas?.forEach { self.upload($0) }