Browse Source

Adapt new preview loading to NCOperationQueue

Signed-off-by: Philippe Weidmann <philippe.weidmann@infomaniak.com>
Philippe Weidmann 4 years ago
parent
commit
335a0a8027

+ 2 - 2
Cartfile.resolved

@@ -8,7 +8,7 @@ github "SwiftyJSON/SwiftyJSON" "5.0.0"
 github "WeTransfer/WeScan" "1.2.0"
 github "WenchaoD/FSCalendar" "2.8.0"
 github "dzenbot/DZNEmptyDataSet" "v1.8.1"
-github "getsentry/sentry-cocoa" "5.1.2"
+github "getsentry/sentry-cocoa" "5.1.3"
 github "huri000/QuickLayout" "3.0.0"
 github "huri000/SwiftEntryKit" "1.2.3"
 github "ivanbruel/MarkdownKit" "1.6"
@@ -20,7 +20,7 @@ github "marinofaggiana/AFNetworking" "2967678c3e0e98c9b8d7e06222ad12d1f49c26f2"
 github "marinofaggiana/KTVHTTPCache" "2.0.2"
 github "marinofaggiana/TOPasscodeViewController" "0.0.6"
 github "marinofaggiana/XLForm" "eb9381ad8129f60402bf412250fb31b95a628a08"
-github "nextcloud/ios-communication-library" "4ae73a504e793ec9856daefef4415488fb35dd0f"
+github "nextcloud/ios-communication-library" "ca1c77cfcfa8bbf097f79409fca633071c836c14"
 github "realm/realm-cocoa" "v4.4.1"
 github "rechsteiner/Parchment" "v1.7.0"
 github "scenee/FloatingPanel" "v1.7.5"

+ 1 - 0
iOSClient/Main/Cell/NCGridMediaCell.swift

@@ -43,6 +43,7 @@ class NCGridMediaCell: UICollectionViewCell, NCImageCellProtocol {
 
     override func prepareForReuse() {
         super.prepareForReuse()
+        imageItem.image = nil
         imageItem.backgroundColor = UIColor.lightGray
     }
 }

+ 3 - 1
iOSClient/Main/NCMainCommon.swift

@@ -466,6 +466,8 @@ class NCMainCommon: NSObject, NCAudioRecorderViewControllerDelegate, UIDocumentI
             
             let cell = tableView.dequeueReusableCell(withIdentifier: "CellMain", for: indexPath) as! CCCellMain
             cell.labelTitle.text = metadata.fileNameView
+            cell.filePreviewImageView.image = nil
+            cell.filePreviewImageView.backgroundColor = UIColor.lightGray
             
             // Download preview
             NCOperationQueue.shared.downloadThumbnail(metadata: metadata, activeUrl: appDelegate.activeUrl, view: tableView, indexPath: indexPath)
@@ -483,7 +485,7 @@ class NCMainCommon: NSObject, NCAudioRecorderViewControllerDelegate, UIDocumentI
                 
                 // lable Info
                 cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date)
-                
+                cell.file.backgroundColor = nil
                 // File Image & Image Title Segue
                 if metadata.e2eEncrypted {
                     cell.file.image = NCMainCommonImages.cellFolderEncryptedImage

+ 16 - 3
iOSClient/Media/NCMedia.swift

@@ -472,12 +472,12 @@ extension NCMedia: UICollectionViewDataSource {
         }
         
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridMediaCell
-        
-        NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: self.collectionView as Any, indexPath: indexPath)
-
+        NCOperationQueue.shared.downloadThumbnail(metadata: metadata, activeUrl: self.appDelegate.activeUrl, view: self.collectionView as Any, indexPath: indexPath)
+        cell.imageItem.backgroundColor = UIColor.lightGray
         cell.imageStatus.image = nil
         cell.imageLocal.image = nil
         cell.imageFavorite.image = nil
+        cell.imageItem.image = nil
         cell.imageItem.layer.masksToBounds = true
         cell.imageItem.layer.cornerRadius = 6
         cell.imageVisualEffect.layer.cornerRadius = 6
@@ -571,6 +571,7 @@ extension NCMedia {
                 }
                 
                 self.reloadDataThenPerform {
+                    self.downloadThumbnail()
                 }
                 
                 completion()
@@ -741,6 +742,18 @@ extension NCMedia {
             }
         }
     }
+    
+    private func downloadThumbnail() {
+        guard let collectionView = self.collectionView else { return }
+        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
+            for item in collectionView.indexPathsForVisibleItems {
+                if let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(item, sectionDataSource: self.sectionDatasource) {
+                    NCOperationQueue.shared.downloadThumbnail(metadata: metadata, activeUrl: self.appDelegate.activeUrl, view: self.collectionView as Any, indexPath: item)
+                }
+            }
+        }
+    }
+
 }
 
 // MARK: - ScrollView

+ 26 - 25
iOSClient/Networking/NCOperationQueue.swift

@@ -134,39 +134,40 @@ class NCOperationDownloadThumbnail: ConcurrentOperation {
     }
     
     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
-                            }
-                        }
+            var cell: NCImageCellProtocol?
+            if self.view is UICollectionView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
+                cell = (self.view as! UICollectionView).cellForItem(at: self.indexPath) as? NCImageCellProtocol
+            } else if self.view is UITableView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
+                cell = (self.view as! UITableView).cellForRow(at: self.indexPath) as? NCImageCellProtocol
+            }
+
+            if (cell != nil) {
+                var previewImage: UIImage!
+                if errorCode == 0 && data != nil {
+                    if let image = UIImage(data: data!) {
+                        previewImage = 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
-                            }
-                        }
+                } else {
+                    if self.metadata.iconName.count > 0 {
+                        previewImage = UIImage(named: self.metadata.iconName)
+                    } else {
+                        previewImage = UIImage(named: "file")
                     }
                 }
+                cell!.filePreviewImageView.backgroundColor = nil
+                UIView.transition(with: cell!.filePreviewImageView,
+                    duration: 0.75,
+                    options: .transitionCrossDissolve,
+                    animations: { cell!.filePreviewImageView.image = previewImage! },
+                    completion: nil)
             }
             self.finish()
+
         }
     }
 }