Selaa lähdekoodia

Refactor Share ext. cancel

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch 3 vuotta sitten
vanhempi
commit
f116f0d990
2 muutettua tiedostoa jossa 16 lisäystä ja 19 poistoa
  1. 2 4
      Share/NCShareExtension+NCDelegate.swift
  2. 14 15
      Share/NCShareExtension.swift

+ 2 - 4
Share/NCShareExtension+NCDelegate.swift

@@ -71,8 +71,7 @@ extension NCShareExtension: NCEmptyDataSetDelegate, NCAccountRequestDelegate {
 
     func accountRequestChangeAccount(account: String) {
         guard let activeAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)) else {
-            uploadStarted = false
-            extensionContext?.cancelRequest(withError: NCShareExtensionError.noAccount)
+            cancel(with: NCShareExtensionError.noAccount)
             return
         }
         self.activeAccount = activeAccount
@@ -110,8 +109,7 @@ extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate, NCListCel
         }
         self.filesName.remove(at: index)
         if self.filesName.isEmpty {
-            uploadStarted = false
-            self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles)
+            cancel(with: NCShareExtensionError.noFiles)
         } else {
             self.setCommandView()
         }

+ 14 - 15
Share/NCShareExtension.swift

@@ -63,8 +63,8 @@ class NCShareExtension: UIViewController {
 
     var layoutForView: NCGlobal.layoutForViewType?
 
-    var heightRowTableView: CGFloat = 50
-    private var heightCommandView: CGFloat = 170
+    let heightRowTableView: CGFloat = 50
+    private let heightCommandView: CGFloat = 170
 
     var autoUploadFileName = ""
     var autoUploadDirectory = ""
@@ -74,8 +74,8 @@ class NCShareExtension: UIViewController {
     private let chunckSize = CCUtility.getChunkSize() * 1000000
 
     private var counterUploaded: Int = 0
-    var uploadMetadata: [tableMetadata] = []
     private var uploadErrors: [tableMetadata] = []
+    var uploadMetadata: [tableMetadata] = []
     var uploadStarted = false
 
     // MARK: - View Life Cycle
@@ -149,16 +149,14 @@ class NCShareExtension: UIViewController {
         guard serverUrl.isEmpty else { return }
 
         guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else {
-            self.uploadStarted = false
             return showAlert(description: "_no_active_account_") {
-                self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noAccount)
+                self.cancel(with: NCShareExtensionError.noAccount)
             }
         }
 
         accountRequestChangeAccount(account: activeAccount.account)
         guard let inputItems = extensionContext?.inputItems as? [NSExtensionItem] else {
-            uploadStarted = false
-            self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles)
+            cancel(with: NCShareExtensionError.noFiles)
             return
         }
         NCFilesExtensionHandler(items: inputItems) { fileNames in
@@ -182,6 +180,12 @@ class NCShareExtension: UIViewController {
 
     // MARK: -
 
+    func cancel(with error: NCShareExtensionError) {
+        // make sure no uploads are continued
+        uploadStarted = false
+        extensionContext?.cancelRequest(withError: error)
+    }
+
     func showAlert(title: String = "_error_", description: String, onDismiss: (() -> Void)? = nil) {
         let alertController = UIAlertController(title: NSLocalizedString(title, comment: ""), message: NSLocalizedString(description, comment: ""), preferredStyle: .alert)
         alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in
@@ -260,8 +264,7 @@ class NCShareExtension: UIViewController {
 
     func setCommandView() {
         guard !filesName.isEmpty else {
-            uploadStarted = false
-            self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles)
+            cancel(with: NCShareExtensionError.noFiles)
             return
         }
         let counter = min(CGFloat(filesName.count), 3)
@@ -280,9 +283,7 @@ class NCShareExtension: UIViewController {
     // MARK: ACTION
 
     @IBAction func actionCancel(_ sender: UIBarButtonItem) {
-        // make sure no uploads are continued
-        uploadStarted = false
-        extensionContext?.cancelRequest(withError: NCShareExtensionError.cancel)
+        cancel(with: NCShareExtensionError.cancel)
     }
 
     @objc func actionCreateFolder() {
@@ -366,9 +367,7 @@ extension NCShareExtension {
             metadata.chunk = true
         }
 
-        NCNetworking.shared.upload(metadata: metadata) {
-
-        } completion: { errorCode, _ in
+        NCNetworking.shared.upload(metadata: metadata) { } completion: { errorCode, _ in
             if errorCode == 0 {
                 self.counterUploaded += 1
                 // next