Przeglądaj źródła

Improvements on Auto Upload

marinofaggiana 4 lat temu
rodzic
commit
ffee3ca37c

+ 2 - 2
Nextcloud.xcodeproj/project.pbxproj

@@ -2806,8 +2806,8 @@
 			isa = XCRemoteSwiftPackageReference;
 			repositoryURL = "https://github.com/nextcloud/ios-communication-library/";
 			requirement = {
-				kind = revision;
-				revision = 95e2b07f2d177f3e98d2eb5165c924db75790b94;
+				kind = exactVersion;
+				version = 0.90.0;
 			};
 		};
 		F7C4D88B2534887E00C142DA /* XCRemoteSwiftPackageReference "Parchment" */ = {

+ 2 - 2
Nextcloud.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

@@ -24,8 +24,8 @@
         "repositoryURL": "https://github.com/nextcloud/ios-communication-library/",
         "state": {
           "branch": null,
-          "revision": "95e2b07f2d177f3e98d2eb5165c924db75790b94",
-          "version": null
+          "revision": "8a886596b9f11af97e7a5ba296356cfa9c5bf8b5",
+          "version": "0.90.0"
         }
       },
       {

+ 12 - 8
iOSClient/AppDelegate.m

@@ -166,7 +166,7 @@
     // Auto upload
     self.networkingAutoUpload = [NCNetworkingAutoUpload new];
     
-    // Background task
+    // Background task: register
     if (@available(iOS 13.0, *)) {
         [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:NCBrandGlobal.shared.backgroudTask usingQueue:nil launchHandler:^(BGTask *task) {
             [self handleBackgroundTask:task];
@@ -204,7 +204,7 @@
     [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
     
     // Initialize Auto upload
-    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^{ }];
+    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^(NSInteger items) { }];
     
     // Read active directory
     [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterReloadDataSourceNetworkForced object:nil];
@@ -280,7 +280,7 @@
     [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterMenuDetailClose object:nil];
     
     // Start Auto Upload
-    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^{ }];
+    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^(NSInteger items) { }];
     
     // Start services
     [[NCService shared] startRequestServicesServer];
@@ -555,8 +555,8 @@
     [[NCCommunicationCommon shared] writeLog:@"Start handler background task"];
     
     // Verify new photo
-    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^{
-        [[NCCommunicationCommon shared] writeLog:@"Completition handler background task"];
+    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^(NSInteger items) {
+        [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Completition handler background task with %lu uploads", (unsigned long)items]];
         [task setTaskCompletedWithSuccess:true];
     }];
 }
@@ -573,9 +573,13 @@
     [[NCCommunicationCommon shared] writeLog:@"Start perform Fetch"];
     
     // Verify new photo
-    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^{
-        [[NCCommunicationCommon shared] writeLog:@"Completition perform Fetch"];
-        completionHandler(UIBackgroundFetchResultNoData);
+    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^(NSInteger items) {
+        [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Completition perform Fetch with %lu uploads", (unsigned long)items]];
+        if (items == 0) {
+            completionHandler(UIBackgroundFetchResultNoData);
+        } else {
+            completionHandler(UIBackgroundFetchResultNewData);
+        }
     }];
 }
 

+ 16 - 14
iOSClient/Networking/NCAutoUpload.swift

@@ -68,7 +68,7 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
             if account.autoUpload && account.autoUploadBackground && UIApplication.shared.applicationState == UIApplication.State.background {
                 NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: nil) { (hasPermission) in
                     if hasPermission {
-                        self.uploadAssetsNewAndFull(viewController: nil, selector: NCBrandGlobal.shared.selectorUploadAutoUpload) { }
+                        self.uploadAssetsNewAndFull(viewController: nil, selector: NCBrandGlobal.shared.selectorUploadAutoUpload) { (items) in }
                     }
                 }
             }
@@ -93,13 +93,13 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
     
     // MARK: -
     
-    @objc func initAutoUpload(viewController: UIViewController?, completion: @escaping ()->()) {
+    @objc func initAutoUpload(viewController: UIViewController?, completion: @escaping (_ items: Int)->()) {
         if let account = NCManageDatabase.shared.getAccountActive() {
             if account.autoUpload {
                 NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: viewController) { (hasPermission) in
                     if hasPermission {
-                        self.uploadAssetsNewAndFull(viewController:viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUpload) {
-                            completion()
+                        self.uploadAssetsNewAndFull(viewController:viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUpload) { (items) in
+                            completion(items)
                         }
                         if account.autoUploadBackground {
                             NCAskAuthorization.shared.askAuthorizationLocationManager() { (hasFullPermissions) in
@@ -114,15 +114,15 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                     } else {
                         NCManageDatabase.shared.setAccountAutoUploadProperty("autoUpload", state: false)
                         self.stopSignificantChangeUpdates()
-                        completion()
+                        completion(0)
                     }
                 }
             } else {
-                completion()
+                completion(0)
             }
         } else {
             stopSignificantChangeUpdates()
-            completion()
+            completion(0)
         }
     }
     
@@ -133,21 +133,21 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                 NCContentPresenter.shared.messageNotification("_attention_", description: "_create_full_upload_", delay: NCBrandGlobal.shared.dismissAfterSecondLong, type: .info, errorCode: 0, forced: true)
                 self.hud?.visibleHudTitle(NSLocalizedString("_wait_", comment: ""), mode: MBProgressHUDMode.indeterminate, color: NCBrandColor.shared.brand)
                 
-                self.uploadAssetsNewAndFull(viewController: viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll) {
+                self.uploadAssetsNewAndFull(viewController: viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll) { (items) in
                     self.hud?.hideHud()
                 }
             }
         }
     }
     
-    private func uploadAssetsNewAndFull(viewController: UIViewController?, selector: String, completion: @escaping ()->()) {
+    private func uploadAssetsNewAndFull(viewController: UIViewController?, selector: String, completion: @escaping (_ items: Int)->()) {
         
         if appDelegate.account == nil || appDelegate.account.count == 0 { return }
         guard let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) else { return }
         let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: account.urlBase, account: account.account)
         var counterLivePhoto: Int = 0
         var metadataFull: [tableMetadata] = []
-        
+        var counterItemsUpload: Int = 0
         DispatchQueue.global(qos: .background).async {
         
             self.getCameraRollAssets(viewController: viewController, account: account, selector: selector, alignPhotoLibrary: false) { (assets) in
@@ -155,7 +155,7 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                 if assets == nil || assets?.count == 0 {
                     NCCommunicationCommon.shared.writeLog("Automatic upload, no new assets found")
                     DispatchQueue.main.async {
-                        completion()
+                        completion(counterItemsUpload)
                     }
                     return
                 } else {
@@ -169,7 +169,7 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                         if selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
                             NCContentPresenter.shared.messageNotification("_error_", description: "_error_createsubfolders_upload_", delay: NCBrandGlobal.shared.dismissAfterSecond, type: .error, errorCode: NCBrandGlobal.shared.ErrorInternalError, forced: true)
                         }
-                        completion()
+                        completion(counterItemsUpload)
                         return
                     }
                 }
@@ -247,6 +247,7 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                         } else if selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
                             metadataFull.append(metadataForUpload)
                         }
+                        counterItemsUpload += 1
                         
                         /* INSERT METADATA MOV LIVE PHOTO FOR UPLOAD */
                         if livePhoto {
@@ -271,6 +272,7 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                                     } else if selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
                                         metadataFull.append(metadataForUpload)
                                     }
+                                    counterItemsUpload += 1
                                 }
                                 counterLivePhoto -= 1
                                 if counterLivePhoto == 0 && self.endForAssetToUpload {
@@ -278,7 +280,7 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                                         if selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
                                             NCManageDatabase.shared.addMetadatas(metadataFull)
                                         }
-                                        completion()
+                                        completion(counterItemsUpload)
                                     }
                                 }
                             }
@@ -293,7 +295,7 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                         if selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
                             NCManageDatabase.shared.addMetadatas(metadataFull)
                         }
-                        completion()
+                        completion(counterItemsUpload)
                     }
                 }
             }