Browse Source

Improve error handling

Can't user NCContentPresenter in share extension (no crash, but it's also not shown)

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

+ 33 - 41
Share/NCShareExtension.swift

@@ -173,15 +173,20 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
                 }
                 }
 
 
             } else {
             } else {
-
-                let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_no_active_account_", comment: ""), preferredStyle: .alert)
-                alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in
+                showAlert(description: "_no_active_account_") {
                     self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
                     self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
-                }))
-                self.present(alertController, animated: true)
+                }
             }
             }
         }
         }
     }
     }
+    
+    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
+            onDismiss?()
+        }))
+        self.present(alertController, animated: true)
+    }
 
 
     override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
     override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
         super.viewWillTransition(to: size, with: coordinator)
         super.viewWillTransition(to: size, with: coordinator)
@@ -394,15 +399,13 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
             textField.autocapitalizationType = UITextAutocapitalizationType.words
             textField.autocapitalizationType = UITextAutocapitalizationType.words
         }
         }
 
 
-        let actionSave = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default) { (_: UIAlertAction) in
+        let actionSave = UIAlertAction(title: NSLocalizedString("_save_", comment: ""), style: .default) { _ in
             if let fileName = alertController.textFields?.first?.text {
             if let fileName = alertController.textFields?.first?.text {
                 self.createFolder(with: fileName)
                 self.createFolder(with: fileName)
             }
             }
         }
         }
 
 
-        let actionCancel = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in
-            print("You've pressed cancel button")
-        }
+        let actionCancel = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)
 
 
         alertController.addAction(actionSave)
         alertController.addAction(actionSave)
         alertController.addAction(actionCancel)
         alertController.addAction(actionCancel)
@@ -465,7 +468,7 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
         NCNetworking.shared.upload(metadata: metadata) {
         NCNetworking.shared.upload(metadata: metadata) {
 
 
         } completion: { errorCode, errorDescription in
         } completion: { errorCode, errorDescription in
-            if errorCode == 0 {
+            if errorCode != 0 {
                 self.counterUpload += 1
                 self.counterUpload += 1
                 self.uploadDispatchGroup?.leave()
                 self.uploadDispatchGroup?.leave()
             } else {
             } else {
@@ -475,13 +478,9 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
                 NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
                 NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
                 NCManageDatabase.shared.deleteChunks(account: self.activeAccount.account, ocId: metadata.ocId)
                 NCManageDatabase.shared.deleteChunks(account: self.activeAccount.account, ocId: metadata.ocId)
 
 
-                let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: errorDescription, preferredStyle: .alert)
-                alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in
-                    // what does this do?
-//                    self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
+                self.showAlert(description: errorDescription) {
                     self.uploadDispatchGroup?.leave()
                     self.uploadDispatchGroup?.leave()
-                }))
-                self.present(alertController, animated: true)
+                }
             }
             }
         }
         }
     }
     }
@@ -503,22 +502,18 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
 extension NCShareExtension: UICollectionViewDelegate {
 extension NCShareExtension: UICollectionViewDelegate {
 
 
     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
+        guard let metadata = dataSource.cellForItemAt(indexPath: indexPath),
+              let serverUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else {
+                  return showAlert(description: "_invalid_url_")
+              }
 
 
-        if let metadata = dataSource.cellForItemAt(indexPath: indexPath) {
-            if let serverUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) {
-
-                if metadata.e2eEncrypted && !CCUtility.isEnd(toEndEnabled: activeAccount.account) {
-                    let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: NSLocalizedString("_e2e_goto_settings_for_enable_", comment: ""), preferredStyle: .alert)
-                    alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))
-                    self.present(alertController, animated: true)
-                    return
-                }
-
-                self.serverUrl = serverUrl
-                reloadDatasource(withLoadFolder: true)
-                setNavigationBar(navigationTitle: metadata.fileNameView)
-            }
+        if metadata.e2eEncrypted && !CCUtility.isEnd(toEndEnabled: activeAccount.account) {
+            showAlert(title: "_info_", description: "_e2e_goto_settings_for_enable_")
         }
         }
+
+        self.serverUrl = serverUrl
+        reloadDatasource(withLoadFolder: true)
+        setNavigationBar(navigationTitle: metadata.fileNameView)
     }
     }
 }
 }
 
 
@@ -676,8 +671,9 @@ extension NCShareExtension: UITableViewDataSource {
 
 
 extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate {
 extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate {
     func removeFile(named fileName: String) {
     func removeFile(named fileName: String) {
-        print(#line, self.filesName.firstIndex(of: fileName), fileName)
-        guard let index = self.filesName.firstIndex(of: fileName) else { return }
+        guard let index = self.filesName.firstIndex(of: fileName) else {
+            return showAlert(title: "_file_not_found_", description: fileName)
+        }
         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?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
@@ -689,8 +685,9 @@ extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate {
     func rename(fileName: String, fileNameNew: String) {
     func rename(fileName: String, fileNameNew: String) {
         guard let fileIx = self.filesName.firstIndex(of: fileName),
         guard let fileIx = self.filesName.firstIndex(of: fileName),
               !self.filesName.contains(fileNameNew),
               !self.filesName.contains(fileNameNew),
-              NCUtilityFileSystem.shared.moveFile(atPath: (NSTemporaryDirectory() + fileName), toPath: (NSTemporaryDirectory() + fileNameNew))
-        else { return }
+              NCUtilityFileSystem.shared.moveFile(atPath: (NSTemporaryDirectory() + fileName), toPath: (NSTemporaryDirectory() + fileNameNew)) else {
+                  return showAlert(title: "_single_file_conflict_title_", description: "'\(fileName)' -> '\(fileNameNew)'")
+              }
 
 
         filesName[fileIx] = fileNameNew
         filesName[fileIx] = fileNameNew
         tableView.reloadData()
         tableView.reloadData()
@@ -729,10 +726,7 @@ extension NCShareExtension {
                     self.setNavigationBar(navigationTitle: fileName)
                     self.setNavigationBar(navigationTitle: fileName)
 
 
                 } else {
                 } else {
-
-                    let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: errorDescription, preferredStyle: .alert)
-                    alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))
-                    self.present(alertController, animated: true)
+                    self.showAlert(description: errorDescription)
                 }
                 }
             }
             }
         }
         }
@@ -747,9 +741,7 @@ extension NCShareExtension {
 
 
             DispatchQueue.main.async {
             DispatchQueue.main.async {
                 if errorCode != 0 {
                 if errorCode != 0 {
-                    let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: errorDescription, preferredStyle: .alert)
-                    alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))
-                    self.present(alertController, animated: true)
+                    self.showAlert(description: errorDescription)
                 }
                 }
                 self.networkInProgress = false
                 self.networkInProgress = false
                 self.metadataFolder = metadataFolder
                 self.metadataFolder = metadataFolder