marinofaggiana 4 years ago
parent
commit
5bb58bc2c1

+ 2 - 194
iOSClient/Main/NCMainCommon.swift

@@ -40,13 +40,6 @@ class NCMainCommon: NSObject, NCAudioRecorderViewControllerDelegate, UIDocumentI
     var metadataEditPhoto: tableMetadata?
     var docController: UIDocumentInteractionController?
 
-    lazy var operationQueueReloadDatasource: OperationQueue = {
-        let queue = OperationQueue()
-        queue.name = "Reload main datasource queue"
-        queue.maxConcurrentOperationCount = 1
-        return queue
-    }()
-    
     //MARK: -
     
     struct NCMainCommonImages {
@@ -225,7 +218,7 @@ class NCMainCommon: NSObject, NCAudioRecorderViewControllerDelegate, UIDocumentI
         
         // Download preview
         if downloadThumbnail {
-            NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: collectionView, indexPath: indexPath)
+            NCOperationQueue.shared.downloadThumbnail(metadata: metadata, activeUrl: appDelegate.activeUrl, view: collectionView, indexPath: indexPath)
         }
         
         var isShare = false
@@ -483,7 +476,7 @@ class NCMainCommon: NSObject, NCAudioRecorderViewControllerDelegate, UIDocumentI
             cell.labelTitle.text = metadata.fileNameView
             
             // Download preview
-            NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: tableView, indexPath: indexPath)
+            NCOperationQueue.shared.downloadThumbnail(metadata: metadata, activeUrl: appDelegate.activeUrl, view: tableView, indexPath: indexPath)
             
             // Share
             var isShare = false
@@ -943,191 +936,6 @@ class CCMainTabBarController : UITabBarController, UITabBarControllerDelegate {
     }
 }
 
-//MARK: - Networking Main
-
-class NCNetworkingMain: NSObject, IMImagemeterViewerDelegate {
-    @objc static let sharedInstance: NCNetworkingMain = {
-        let instance = NCNetworkingMain()
-        return instance
-    }()
-    
-    lazy var operationQueueNetworkingMain: OperationQueue = {
-        let queue = OperationQueue()
-        queue.name = "com.nextcloud.operationQueueNetworkingMain"
-        queue.maxConcurrentOperationCount = 1
-        return queue
-    }()
-    
-    let appDelegate = UIApplication.shared.delegate as! AppDelegate
-
-    #if HC
-    // IMImagemeterViewerDelegate
-    func closeImagemeterViewer(metadata: tableMetadata?, bundleDirectory: String) {
-        guard let metadata = metadata else { return }
-        
-        let ocIdTemp = NSUUID().uuidString.lowercased()
-        let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(ocIdTemp, fileNameView: metadata.fileName)!
-        let bundleDirectoryURL = URL(fileURLWithPath: bundleDirectory)
-        let fileNameZipUrl = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(ocIdTemp, fileNameView: metadata.fileName))
-        
-        // Create IMI
-        try? FileManager.default.removeItem(at: fileNameZipUrl)
-        do {
-            try FileManager().zipItem(at: bundleDirectoryURL, to:fileNameZipUrl)
-        } catch {
-            NCContentPresenter.shared.messageNotification("_error_", description: "Creation of IMI archive failed with error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
-            return
-        }
-        
-        // Verify if is changed
-        if IMUtility.shared.IMIsChange(metadata: metadata, fileNameZipUrl: fileNameZipUrl) {
-            let com = IMCommunication.init()
-            _ = com.uploadFileIMI(serverUrl: metadata.serverUrl, fileName: metadata.fileName, fileNameLocalPath: fileNameLocalPath, ocId: ocIdTemp)
-        }
-        
-        // Remove bundle directory
-        try? FileManager.default.removeItem(atPath: bundleDirectory)
-    }
-    #endif
-    
-    @objc func downloadThumbnail(with metadata: tableMetadata, view: Any, indexPath: IndexPath) {
-        operationQueueNetworkingMain.addOperation(NCOperationNetworkingMain.init(metadata: metadata, view: view, indexPath: indexPath, networkingFunc: "downloadThumbnail"))
-    }
-    
-    func downloadThumbnail(with metadata: tableMetadata, view: Any, indexPath: IndexPath, closure: @escaping () -> ()) {
-        
-        if !metadata.isInvalidated && metadata.hasPreview && (!CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileName) || metadata.typeFile == k_metadataTypeFile_document) {
-                        
-            let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
-            let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
-                    
-            NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNameLocalPath: fileNameLocalPath, width: Int(k_sizePreview), height: Int(k_sizePreview)) { (account, data, errorCode, errorMessage) in
-                
-                if errorCode == 0 && data != nil  {
-                    if let image = UIImage.init(data: data!) {
-                        
-                        if view is UICollectionView && NCMainCommon.sharedInstance.isValidIndexPath(indexPath, view: view) {
-                            if let cell = (view as! UICollectionView).cellForItem(at: indexPath) {
-                                if cell is NCListCell {
-                                    (cell as! NCListCell).imageItem.image = image
-                                } else if cell is NCGridCell {
-                                    (cell as! NCGridCell).imageItem.image = image
-                                } else if cell is NCGridMediaCell {
-                                    (cell as! NCGridMediaCell).imageItem.image = image
-                                }
-                            }
-                        }
-                        
-                        if view is UITableView && CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileName) && NCMainCommon.sharedInstance.isValidIndexPath(indexPath, view: view) {
-                            if let cell = (view as! UITableView).cellForRow(at: indexPath) {
-                                if cell is CCCellMainTransfer {
-                                    (cell as! CCCellMainTransfer).file.image = image
-                                } else if cell is CCCellMain {
-                                    (cell as! CCCellMain).file.image = image
-                                }
-                            }
-                        }
-                    }
-                }
-                return closure()
-            }
-        }
-        return closure()
-    }
-}
-
-//MARK: - Operation Networking Main
-
-class NCOperationNetworkingMain: Operation {
-    
-    private var _executing : Bool = false
-    override var isExecuting : Bool {
-        get { return _executing }
-        set {
-            guard _executing != newValue else { return }
-            willChangeValue(forKey: "isExecuting")
-            _executing = newValue
-            didChangeValue(forKey: "isExecuting")
-        }
-    }
-
-    private var _finished : Bool = false
-    override var isFinished : Bool {
-        get { return _finished }
-        set {
-            guard _finished != newValue else { return }
-            willChangeValue(forKey: "isFinished")
-            _finished = newValue
-            didChangeValue(forKey: "isFinished")
-        }
-    }
-    
-    private var metadata: tableMetadata?
-    private var view: Any?
-    private var indexPath: IndexPath?
-    private var networkingFunc: String = ""
-
-    init(metadata: tableMetadata?, view: Any?, indexPath: IndexPath?, networkingFunc: String) {
-        super.init()
-        
-        if metadata != nil { self.metadata = metadata! }
-        if view != nil { self.view = view! }
-        if indexPath != nil { self.indexPath = indexPath! }
-        
-        self.networkingFunc = networkingFunc
-    }
-    
-    override func start() {
-        if !Thread.isMainThread {
-            
-            self.performSelector(onMainThread:#selector(start), with: nil, waitUntilDone: false)
-            
-        } else {
-        
-            isExecuting = true
-        
-            if isCancelled {
-                finish()
-            } else {
-                poolNetworking()
-            }
-        }
-    }
-    
-    func finish() {
-        isExecuting = false
-        isFinished = true
-    }
-    
-    override func cancel() {
-        super.cancel()
-        
-        if isExecuting {
-            complete()
-        }
-    }
-    
-    func complete() {
-        finish()
-        
-        UIApplication.shared.isNetworkActivityIndicatorVisible = false
-    }
-    
-    func poolNetworking() {
-        UIApplication.shared.isNetworkActivityIndicatorVisible = true
-        
-        switch networkingFunc {
-        case "downloadThumbnail":
-            NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata!, view: view!, indexPath: indexPath!) {
-                self.complete()
-            }
-        default:
-            print("error")
-        }
-    }
-}
-
-
 //MARK: - Function Main
 
 class NCFunctionMain: NSObject {

+ 1 - 1
iOSClient/Media/NCMedia.swift

@@ -751,7 +751,7 @@ extension NCMedia {
         DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
             for item in collectionView.indexPathsForVisibleItems {
                 if let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(item, sectionDataSource: self.sectionDatasource) {
-                    NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: self.collectionView as Any, indexPath: item)
+                    NCOperationQueue.shared.downloadThumbnail(metadata: metadata, activeUrl: self.appDelegate.activeUrl, view: self.collectionView as Any, indexPath: item)
                 }
             }
         }

+ 61 - 0
iOSClient/Networking/NCOperationQueue.swift

@@ -24,6 +24,7 @@
 
 import Foundation
 import Queuer
+import NCCommunication
 
 @objc class NCOperationQueue: NSObject {
     @objc public static let shared: NCOperationQueue = {
@@ -32,10 +33,17 @@ import Queuer
     }()
     
     let downloadQueue = Queuer(name: "downloadQueue", maxConcurrentOperationCount: 5, qualityOfService: .default)
+    let downloadThumbnailQueue = Queuer(name: "downloadThumbnailQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
     
     @objc func download(metadata: tableMetadata, selector: String, setFavorite: Bool) {
         downloadQueue.addOperation(NCOperationDownload.init(metadata: metadata, selector: selector, setFavorite: setFavorite))
     }
+    
+    @objc func downloadThumbnail(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
+        if metadata.hasPreview && (!CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileName) || metadata.typeFile == k_metadataTypeFile_document) {
+            downloadThumbnailQueue.addOperation(NCOperationDownloadThumbnail.init(metadata: metadata, activeUrl: activeUrl, view: view, indexPath: indexPath))
+        }
+    }
 }
 
 class NCOperationDownload: ConcurrentOperation {
@@ -57,3 +65,56 @@ class NCOperationDownload: ConcurrentOperation {
     }
 }
 
+class NCOperationDownloadThumbnail: ConcurrentOperation {
+   
+    private var metadata: tableMetadata
+    private var activeUrl: String
+    private var view: Any
+    private var indexPath: IndexPath
+    
+    init(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
+        self.metadata = metadata
+        self.activeUrl = activeUrl
+        self.view = view
+        self.indexPath = indexPath
+    }
+    
+    override func start() {
+            
+        let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
+        let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
+            
+        NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNameLocalPath: fileNameLocalPath, width: Int(k_sizePreview), height: Int(k_sizePreview)) { (account, data, errorCode, errorMessage) in
+            
+            if errorCode == 0 && data != nil  {
+                if let image = UIImage.init(data: data!) {
+                    
+                    if self.view is UICollectionView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
+                        if let cell = (self.view as! UICollectionView).cellForItem(at: self.indexPath) {
+                            if cell is NCListCell {
+                                (cell as! NCListCell).imageItem.image = image
+                            } else if cell is NCGridCell {
+                                (cell as! NCGridCell).imageItem.image = image
+                            } else if cell is NCGridMediaCell {
+                                (cell as! NCGridMediaCell).imageItem.image = image
+                            }
+                        }
+                    }
+                    
+                    if self.view is UITableView && CCUtility.fileProviderStorageIconExists(self.metadata.ocId, fileNameView: self.metadata.fileName) && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
+                        if let cell = (self.view as! UITableView).cellForRow(at: self.indexPath) {
+                            if cell is CCCellMainTransfer {
+                                (cell as! CCCellMainTransfer).file.image = image
+                            } else if cell is CCCellMain {
+                                (cell as! CCCellMain).file.image = image
+                            }
+                        }
+                    }
+                }
+            }
+            
+            self.finish()
+        }
+    }
+}
+

+ 1 - 1
iOSClient/Shares/NCShares.m

@@ -286,7 +286,7 @@
                 
                 cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
                 
-                [[NCNetworkingMain sharedInstance] downloadThumbnailWith:metadata view:tableView indexPath:indexPath];
+                [[NCOperationQueue shared] downloadThumbnailWithMetadata:metadata activeUrl:appDelegate.activeUrl view:tableView indexPath:indexPath];
             }
         }