Browse Source

added notificationCenterDownloadedThumbnail

marinofaggiana 3 years ago
parent
commit
36c49be76a

+ 2 - 0
iOSClient/NCGlobal.swift

@@ -321,6 +321,8 @@ class NCGlobal: NSObject {
     let notificationStatusAuthorizationChangedLocation          = "statusAuthorizationChangedLocation"
     
     let notificationCenterShareChangePermissions                = "shareChangePermissions"          // userInfo: idShare, permissions, hideDownload
+    
+    let notificationCenterDownloadedThumbnail                   = "DownloadedThumbnail"             // userInfo: ocId
 }
 
 //let rootView = UIApplication.shared.keyWindow?.rootViewController?.view

+ 8 - 7
iOSClient/Networking/NCOperationQueue.swift

@@ -125,15 +125,15 @@ import NCCommunication
     
     // Download Thumbnail
     
-    @objc func downloadThumbnail(metadata: tableMetadata, placeholder: Bool, cell: UIView, view: UIView?) {
+    @objc func downloadThumbnail(metadata: tableMetadata, placeholder: Bool, cell: UIView?, view: UIView?) {
         
-        let cell: NCCellProtocol = cell as! NCCellProtocol
+        let cell: NCCellProtocol? = cell as? NCCellProtocol
         
         if placeholder {
             if metadata.iconName.count > 0 {
-                cell.filePreviewImageView?.image = UIImage.init(named: metadata.iconName)
+                cell?.filePreviewImageView?.image = UIImage.init(named: metadata.iconName)
             } else {
-                cell.filePreviewImageView?.image = NCBrandColor.cacheImages.file
+                cell?.filePreviewImageView?.image = NCBrandColor.cacheImages.file
             }
         }
         
@@ -397,13 +397,13 @@ class NCOperationSynchronization: ConcurrentOperation {
 class NCOperationDownloadThumbnail: ConcurrentOperation {
    
     var metadata: tableMetadata
-    var cell: NCCellProtocol!
+    var cell: NCCellProtocol?
     var view: UIView?
     var fileNamePath: String = ""
     var fileNamePreviewLocalPath: String = ""
     var fileNameIconLocalPath: String = ""
     
-    init(metadata: tableMetadata, cell: NCCellProtocol, view: UIView?) {
+    init(metadata: tableMetadata, cell: NCCellProtocol?, view: UIView?) {
         self.metadata = tableMetadata.init(value: metadata)
         self.cell = cell
         self.view = view
@@ -425,7 +425,7 @@ class NCOperationDownloadThumbnail: ConcurrentOperation {
                 
                 if errorCode == 0 && imageIcon != nil {
                     NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
-                    if self.metadata.ocId == self.cell.fileObjectId {
+                    if self.metadata.ocId == self.cell?.fileObjectId {
                         if let filePreviewImageView = self.cell?.filePreviewImageView  {
                             UIView.transition(with: filePreviewImageView,
                                 duration: 0.75,
@@ -440,6 +440,7 @@ class NCOperationDownloadThumbnail: ConcurrentOperation {
                             (self.view as? UITableView)?.reloadData()
                         }
                     }
+                    NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedThumbnail, userInfo: ["ocId": self.metadata.ocId])
                 }
                 self.finish()
             }

+ 13 - 0
iOSClient/Viewer/NCViewerMedia/NCViewerMedia.swift

@@ -124,6 +124,8 @@ class NCViewerMedia: UIViewController {
         NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object:nil)
+        
+        NotificationCenter.default.addObserver(self, selector: #selector(downloadedThumbnail(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedThumbnail), object: nil)
     }
     
     override func viewWillDisappear(_ animated: Bool) {
@@ -143,6 +145,8 @@ class NCViewerMedia: UIViewController {
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
+        
+        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedThumbnail), object: nil)
     }
     
     @objc func viewUnload() {
@@ -178,6 +182,15 @@ class NCViewerMedia: UIViewController {
         }
     }
     
+    @objc func downloadedThumbnail(_ notification: NSNotification) {
+        
+        if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
+            if let image = getImageMetadata(metadatas[currentIndex]) {
+                currentViewController.reload(image: image, metadata: metadata)
+            }
+        }
+    }
+    
     @objc func uploadedFile(_ notification: NSNotification) {
         
         if let userInfo = notification.userInfo as NSDictionary? {

+ 1 - 18
iOSClient/Viewer/NCViewerMedia/NCViewerMediaZoom.swift

@@ -309,24 +309,7 @@ extension NCViewerMediaZoom {
         // DOWNLOAD preview
         if !CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) && metadata.hasPreview {
             
-            let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
-            let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
-            let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
-            var etagResource: String?
-            
-            if FileManager.default.fileExists(atPath: fileNameIconLocalPath) && FileManager.default.fileExists(atPath: fileNamePreviewLocalPath) {
-                etagResource = metadata.etagResource
-            }
-            
-            NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNamePreviewLocalPath: fileNamePreviewLocalPath, widthPreview: NCGlobal.shared.sizePreview, heightPreview: NCGlobal.shared.sizePreview, fileNameIconLocalPath: fileNameIconLocalPath, sizeIcon: NCGlobal.shared.sizeIcon, etag: etagResource) { (account, imagePreview, imageIcon, imageOriginal, etag, errorCode, errorMessage) in
-                
-                if errorCode == 0 {
-                    NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
-                    if let image = self.viewerMedia?.getImageMetadata(self.metadata) {
-                        self.reload(image: image, metadata: self.metadata)
-                    }
-                }
-            }
+            NCOperationQueue.shared.downloadThumbnail(metadata: metadata, placeholder: false, cell: nil, view: nil)
         }
     }
 }