Browse Source

Cancel Share extension if an error occurs instead of completing successfully

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch 3 years ago
parent
commit
7f529bd8d2
1 changed files with 11 additions and 7 deletions
  1. 11 7
      Share/NCShareExtension.swift

+ 11 - 7
Share/NCShareExtension.swift

@@ -27,6 +27,10 @@ import UIKit
 import NCCommunication
 import NCCommunication
 import IHProgressHUD
 import IHProgressHUD
 
 
+enum NCShareExtensionError: Error {
+    case cancel, fileUpload, noAccount, noFiles
+}
+
 class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDelegate, NCAccountRequestDelegate {
 class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDelegate, NCAccountRequestDelegate {
 
 
     @IBOutlet weak var collectionView: UICollectionView!
     @IBOutlet weak var collectionView: UICollectionView!
@@ -177,7 +181,7 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
 
 
             } else {
             } else {
                 showAlert(description: "_no_active_account_") {
                 showAlert(description: "_no_active_account_") {
-                    self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
+                    self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noAccount)
                 }
                 }
             }
             }
         }
         }
@@ -223,7 +227,7 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
     func setAccount(account: String) {
     func setAccount(account: String) {
 
 
         guard let activeAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)) else {
         guard let activeAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)) else {
-            extensionContext?.completeRequest(returningItems: extensionContext?.inputItems, completionHandler: nil)
+            extensionContext?.cancelRequest(withError: NCShareExtensionError.noAccount)
             return
             return
         }
         }
         self.activeAccount = activeAccount
         self.activeAccount = activeAccount
@@ -254,7 +258,7 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
         backButton.setImage(UIImage(named: "back"), for: .normal)
         backButton.setImage(UIImage(named: "back"), for: .normal)
         backButton.tintColor = .systemBlue
         backButton.tintColor = .systemBlue
         backButton.semanticContentAttribute = .forceLeftToRight
         backButton.semanticContentAttribute = .forceLeftToRight
-        backButton.setTitle(" "+NSLocalizedString("_back_", comment: ""), for: .normal)
+        backButton.setTitle(" " + NSLocalizedString("_back_", comment: ""), for: .normal)
         backButton.setTitleColor(.systemBlue, for: .normal)
         backButton.setTitleColor(.systemBlue, for: .normal)
         backButton.action(for: .touchUpInside) { _ in
         backButton.action(for: .touchUpInside) { _ in
 
 
@@ -351,7 +355,7 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
         var counter: CGFloat = 0
         var counter: CGFloat = 0
 
 
         if filesName.count == 0 {
         if filesName.count == 0 {
-            self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
+            self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles)
             return
             return
         } else {
         } else {
             if filesName.count < 3 {
             if filesName.count < 3 {
@@ -391,7 +395,7 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
     // MARK: ACTION
     // MARK: ACTION
 
 
     @IBAction func actionCancel(_ sender: UIBarButtonItem) {
     @IBAction func actionCancel(_ sender: UIBarButtonItem) {
-        extensionContext?.completeRequest(returningItems: extensionContext?.inputItems, completionHandler: nil)
+        extensionContext?.cancelRequest(withError: NCShareExtensionError.cancel)
     }
     }
 
 
     @objc func actionCreateFolder() {
     @objc func actionCreateFolder() {
@@ -491,7 +495,7 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
         if !uploadErrors.isEmpty {
         if !uploadErrors.isEmpty {
             let fileList = "- " + uploadErrors.map({ $0.fileName }).joined(separator: "\n  - ")
             let fileList = "- " + uploadErrors.map({ $0.fileName }).joined(separator: "\n  - ")
             showAlert(title: "_error_files_upload_", description: fileList) {
             showAlert(title: "_error_files_upload_", description: fileList) {
-                self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
+                self.extensionContext?.cancelRequest(withError: NCShareExtensionError.fileUpload)
             }
             }
         } else {
         } else {
             IHProgressHUD.showSuccesswithStatus(NSLocalizedString("_success_", comment: ""))
             IHProgressHUD.showSuccesswithStatus(NSLocalizedString("_success_", comment: ""))
@@ -685,7 +689,7 @@ extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate {
         }
         }
         self.filesName.remove(at: index)
         self.filesName.remove(at: index)
         if self.filesName.count == 0 {
         if self.filesName.count == 0 {
-            self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
+            self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles)
         } else {
         } else {
             self.setCommandView()
             self.setCommandView()
         }
         }