diff options
author | Henrik Storch <henrik.storch@nextcloud.com> | 2022-01-11 13:34:42 +0300 |
---|---|---|
committer | Henrik Storch <henrik.storch@nextcloud.com> | 2022-01-11 13:34:54 +0300 |
commit | 9419025f24cecd0aeacc6ccd0f71b8cb7fae6fd9 (patch) | |
tree | 86058f3c9f3e7cafcb6bbe73e16a7bab968ee225 /Share | |
parent | 6e0e25b62acb4db7f1e2a9075aa2613b4fcd0f5a (diff) |
Revert Share ext. upload one by one
- gives ability to cancel further requests
- can keep using IHProgressHUD
- only start upload after conflicts are resolved, don't start if conflict was cancelled
Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Diffstat (limited to 'Share')
-rw-r--r-- | Share/NCShareExtension+NCDelegate.swift | 12 | ||||
-rw-r--r-- | Share/NCShareExtension.swift | 49 |
2 files changed, 36 insertions, 25 deletions
diff --git a/Share/NCShareExtension+NCDelegate.swift b/Share/NCShareExtension+NCDelegate.swift index 5005cc44e..b0bf1dd40 100644 --- a/Share/NCShareExtension+NCDelegate.swift +++ b/Share/NCShareExtension+NCDelegate.swift @@ -71,6 +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) return } @@ -109,6 +110,7 @@ extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate, NCListCel } self.filesName.remove(at: index) if self.filesName.isEmpty { + uploadStarted = false self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles) } else { self.setCommandView() @@ -143,7 +145,13 @@ extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate, NCListCel extension NCShareExtension: NCCreateFormUploadConflictDelegate { func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) { - metadatas?.forEach { self.upload($0) } - uploadDispatchGroup?.leave() + guard let metadatas = metadatas else { + uploadStarted = false + uploadMetadata.removeAll() + return + } + + self.uploadMetadata.append(contentsOf: metadatas) + self.upload() } } diff --git a/Share/NCShareExtension.swift b/Share/NCShareExtension.swift index aebf3c5e0..b25578143 100644 --- a/Share/NCShareExtension.swift +++ b/Share/NCShareExtension.swift @@ -73,10 +73,10 @@ class NCShareExtension: UIViewController { var activeAccount: tableAccount! private let chunckSize = CCUtility.getChunkSize() * 1000000 - private var counterUpload: Int = 0 - var uploadDispatchGroup: DispatchGroup? + private var counterUploaded: Int = 0 + var uploadMetadata: [tableMetadata] = [] private var uploadErrors: [tableMetadata] = [] - private(set) var uploadStarted = false + var uploadStarted = false // MARK: - View Life Cycle @@ -137,7 +137,7 @@ class NCShareExtension: UIViewController { } // HUD - IHProgressHUD.set(viewForExtension: self.view) + IHProgressHUD.set(viewForExtension: self.collectionView) IHProgressHUD.set(defaultMaskType: .clear) IHProgressHUD.set(minimumDismiss: 0) @@ -149,6 +149,7 @@ 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) } @@ -156,6 +157,7 @@ class NCShareExtension: UIViewController { accountRequestChangeAccount(account: activeAccount.account) guard let inputItems = extensionContext?.inputItems as? [NSExtensionItem] else { + uploadStarted = false self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles) return } @@ -190,7 +192,7 @@ class NCShareExtension: UIViewController { @objc func triggerProgressTask(_ notification: NSNotification) { guard let progress = notification.userInfo?["progress"] as? CGFloat else { return } - let status = NSLocalizedString("_upload_file_", comment: "") + " \(counterUpload + 1) " + NSLocalizedString("_of_", comment: "") + " \(filesName.count)" + let status = NSLocalizedString("_upload_file_", comment: "") + " \(counterUploaded + 1) " + NSLocalizedString("_of_", comment: "") + " \(filesName.count)" IHProgressHUD.show(progress: progress, status: status) } @@ -258,6 +260,7 @@ class NCShareExtension: UIViewController { func setCommandView() { guard !filesName.isEmpty else { + uploadStarted = false self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles) return } @@ -277,6 +280,8 @@ class NCShareExtension: UIViewController { // MARK: ACTION @IBAction func actionCancel(_ sender: UIBarButtonItem) { + // make sure no uploads are continued + uploadStarted = false extensionContext?.cancelRequest(withError: NCShareExtensionError.cancel) } @@ -311,9 +316,6 @@ extension NCShareExtension { uploadStarted = true uploadErrors = [] - uploadDispatchGroup = DispatchGroup() - uploadDispatchGroup?.enter() - uploadDispatchGroup?.notify(queue: .main, execute: finishedUploading) var conflicts: [tableMetadata] = [] for fileName in filesName { @@ -334,26 +336,26 @@ extension NCShareExtension { if NCManageDatabase.shared.getMetadataConflict(account: activeAccount.account, serverUrl: serverUrl, fileName: fileName) != nil { conflicts.append(metadata) } else { - upload(metadata) + uploadMetadata.append(metadata) } } if !conflicts.isEmpty { - uploadDispatchGroup?.enter() - DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { - guard let conflict = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict - else { return } - conflict.serverUrl = self.serverUrl - conflict.metadatasUploadInConflict = conflicts - conflict.delegate = self - self.present(conflict, animated: true, completion: nil) - } + guard let conflict = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict + else { return } + conflict.serverUrl = self.serverUrl + conflict.metadatasUploadInConflict = conflicts + conflict.delegate = self + self.present(conflict, animated: true, completion: nil) + } else { + upload() } - uploadDispatchGroup?.leave() } - func upload(_ metadata: tableMetadata) { - uploadDispatchGroup?.enter() + func upload() { + guard uploadStarted else { return } + guard !uploadMetadata.isEmpty else { return finishedUploading() } + let metadata = uploadMetadata.removeFirst() // E2EE if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase) { metadata.e2eEncrypted = true @@ -367,9 +369,10 @@ extension NCShareExtension { NCNetworking.shared.upload(metadata: metadata) { } completion: { errorCode, _ in - defer { self.uploadDispatchGroup?.leave() } if errorCode == 0 { - self.counterUpload += 1 + self.counterUploaded += 1 + // next + self.upload() } else { NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) NCManageDatabase.shared.deleteChunks(account: self.activeAccount.account, ocId: metadata.ocId) |