Browse Source

add activityIndicator utility

Marino Faggiana 6 years ago
parent
commit
794ba2ff82

+ 1 - 1
iOSClient/Favorites/CCFavorites.m

@@ -335,7 +335,7 @@
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
     
     [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
-        [[NCMainCommon sharedInstance] cancelAllTransfer];
+        [[NCMainCommon sharedInstance] cancelAllTransferWithView:self.view];
     }]];
     
     [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];

+ 1 - 1
iOSClient/Main/CCMain.m

@@ -2010,7 +2010,7 @@
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
     
     [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
-        [[NCMainCommon sharedInstance] cancelAllTransfer];
+        [[NCMainCommon sharedInstance] cancelAllTransferWithView:self.view];
     }]];
     
     [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];

+ 23 - 16
iOSClient/Main/NCMainCommon.swift

@@ -164,32 +164,39 @@ class NCMainCommon: NSObject {
         }
     }
     
-    @objc func cancelAllTransfer() {
+    @objc func cancelAllTransfer(view: UIView) {
         
         // Delete k_metadataStatusWaitUpload OR k_metadataStatusUploadError
         NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "account == %@ AND (status == %d OR status == %d)", appDelegate.activeAccount, k_metadataStatusWaitUpload, k_metadataStatusUploadError), clearDateReadDirectoryID: nil)
         
-        if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND status != %d AND status != %d", appDelegate.activeAccount, k_metadataStatusNormal, k_metadataStatusHide), sorted: "fileName", ascending: true)  {
-            
-            for metadata in metadatas {
+        NCUtility.sharedInstance.startActivityIndicator(view: view)
+        
+        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
+            if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND status != %d AND status != %d", self.appDelegate.activeAccount, k_metadataStatusNormal, k_metadataStatusHide), sorted: "fileName", ascending: true)  {
                 
-                // Modify
-                if (metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusDownloadError) {
-                    metadata.session = ""
-                    metadata.sessionSelector = ""
-                    metadata.status = Int(k_metadataStatusNormal)
+                for metadata in metadatas {
                     
-                    _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
-                }
-                
-                // Cancel Task
-                if metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusUploading {
-                    cancelTransferMetadata(metadata, reloadDatasource: false)
+                    // Modify
+                    if (metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusDownloadError) {
+                        metadata.session = ""
+                        metadata.sessionSelector = ""
+                        metadata.status = Int(k_metadataStatusNormal)
+                        
+                        _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
+                    }
+                    
+                    // Cancel Task
+                    if metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusUploading {
+                        self.cancelTransferMetadata(metadata, reloadDatasource: false)
+                    }
                 }
             }
         }
         
-        self.reloadDatasource(ServerUrl: nil, fileID: nil, action: k_action_NULL)
+        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
+            self.reloadDatasource(ServerUrl: nil, fileID: nil, action: k_action_NULL)
+            NCUtility.sharedInstance.stopActivityIndicator()
+        }
     }
     
     //MARK: -

+ 1 - 1
iOSClient/Transfers/CCTransfers.m

@@ -177,7 +177,7 @@
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
     
     [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
-        [[NCMainCommon sharedInstance] cancelAllTransfer];
+        [[NCMainCommon sharedInstance] cancelAllTransferWithView:self.view];
     }]];
     
     [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];

+ 25 - 0
iOSClient/Utility/NCUtility.swift

@@ -31,6 +31,8 @@ class NCUtility: NSObject {
         return instance
     }()
     
+    let activityIndicator = UIActivityIndicatorView()
+    
     @objc func createFileName(_ fileName: String, directoryID: String) -> String {
         
         var resultFileName = fileName
@@ -243,5 +245,28 @@ class NCUtility: NSObject {
             }
         }
     }
+    
+    @objc func startActivityIndicator(view: UIView) {
+        
+        activityIndicator.color = NCBrandColor.sharedInstance.brand
+        activityIndicator.hidesWhenStopped = true
+            
+        view.addSubview(activityIndicator)
+            
+        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
+            
+        let horizontalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0)
+        view.addConstraint(horizontalConstraint)
+            
+        let verticalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0)
+        view.addConstraint(verticalConstraint)
+
+        activityIndicator.startAnimating()
+    }
+    
+    @objc func stopActivityIndicator() {
+        activityIndicator.stopAnimating()
+        activityIndicator.removeFromSuperview()
+    }
 }