Browse Source

MEGA coding

Signed-off-by: marinofaggiana <ios@nextcloud.com>
marinofaggiana 2 years ago
parent
commit
cda03de243

+ 26 - 1
iOSClient/Data/NCDataSource.swift

@@ -110,9 +110,17 @@ class NCDataSource: NSObject {
                 return nil
             }
         } else {
+            // NEW section
             createSections()
             let sectionValue = getSectionValue(metadata: metadata)
             createMetadataForSection(sectionValue: sectionValue)
+            // get IndexPath of new section
+            if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
+                let metadataForSection = metadatasForSection[sectionIndex]
+                if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
+                    return IndexPath(row: rowIndex, section: sectionIndex)
+                }
+            }
         }
 
         return nil
@@ -121,12 +129,19 @@ class NCDataSource: NSObject {
     func deleteMetadata(ocId: String) -> IndexPath? {
 
         var indexPathReturn: IndexPath?
+        var removeMetadataForSection = false
+        var sectionValue = ""
 
         // DELETE metadataForSection (IMPORTANT FIRST)
         let (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocId)
         if let indexPath = indexPath, let metadataForSection = metadataForSection {
             metadataForSection.metadatas.remove(at: indexPath.row)
-            metadataForSection.createMetadatasForSection()
+            if metadataForSection.metadatas.count == 0 {
+                sectionValue = metadataForSection.sectionValue
+                removeMetadataForSection = true
+            } else {
+                metadataForSection.createMetadatasForSection()
+            }
             indexPathReturn = indexPath
         }
 
@@ -135,6 +150,16 @@ class NCDataSource: NSObject {
             self.metadatasSource.remove(at: rowIndex)
         }
 
+        // REMOVE sectionsValue / metadatasForSection
+        if removeMetadataForSection {
+            if let index = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
+                self.sectionsValue.remove(at: index)
+            }
+            if let index = self.metadatasForSection.firstIndex(where: {$0.sectionValue == sectionValue }) {
+                self.metadatasForSection.remove(at: index)
+            }
+        }
+
         return indexPathReturn
     }
 

+ 66 - 64
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -339,15 +339,14 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
     @objc func reloadDataSourceNetworkForced(_ notification: NSNotification) {
 
-        if let userInfo = notification.userInfo as NSDictionary? {
-            if let serverUrl = userInfo["serverUrl"] as? String {
-                if serverUrl == self.serverUrl {
-                    reloadDataSourceNetwork(forced: true)
-                }
-            }
-        } else {
-            reloadDataSourceNetwork(forced: true)
+        guard let userInfo = notification.userInfo as NSDictionary?,
+              let serverUrl = userInfo["serverUrl"] as? String,
+              serverUrl == self.serverUrl
+        else {
+            return
         }
+
+        reloadDataSourceNetwork(forced: true)
     }
 
     @objc func changeStatusFolderE2EE(_ notification: NSNotification) {
@@ -542,64 +541,67 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
     @objc func triggerProgressTask(_ notification: NSNotification) {
 
-        if let userInfo = notification.userInfo as NSDictionary?, let progressNumber = userInfo["progress"] as? NSNumber, let totalBytes = userInfo["totalBytes"] as? Int64, let totalBytesExpected = userInfo["totalBytesExpected"] as? Int64, let ocId = userInfo["ocId"] as? String {
-
-            let status = userInfo["status"] as? Int ?? NCGlobal.shared.metadataStatusNormal
-            let (indexPath, _) = self.dataSource.getIndexPathMetadata(ocId: ocId)
-
-            if let indexPath = indexPath {
-                if let cell = collectionView?.cellForItem(at: indexPath) {
-                    if cell is NCListCell {
-                        let cell = cell as! NCListCell
-                        if progressNumber.floatValue == 1 {
-                            cell.progressView?.isHidden = true
-                            cell.progressView?.progress = .zero
-                            cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
-                            if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
-                                cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
-                            } else {
-                                cell.labelInfo.text = ""
-                            }
-                        } else if progressNumber.floatValue > 0 {
-                            cell.progressView?.isHidden = false
-                            cell.progressView?.progress = progressNumber.floatValue
-                            cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
-                            if status == NCGlobal.shared.metadataStatusInDownload {
-                                cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
-                            } else if status == NCGlobal.shared.metadataStatusInUpload {
-                                cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
-                            }
-                        }
-                    } else if cell is NCTransferCell {
-                        let cell = cell as! NCTransferCell
-                        if progressNumber.floatValue == 1 {
-                            cell.progressView?.isHidden = true
-                            cell.progressView?.progress = .zero
-                            cell.buttonMore.isHidden = true
-                            cell.labelInfo.text = ""
-                        } else if progressNumber.floatValue > 0 {
-                            cell.progressView?.isHidden = false
-                            cell.progressView?.progress = progressNumber.floatValue
-                            cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
-                            if status == NCGlobal.shared.metadataStatusInDownload {
-                                cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
-                            } else if status == NCGlobal.shared.metadataStatusInUpload {
-                                cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
-                            }
-                        }
-                    } else if cell is NCGridCell {
-                        let cell = cell as! NCGridCell
-                        if progressNumber.floatValue == 1 {
-                            cell.progressView.isHidden = true
-                            cell.progressView.progress = .zero
-                            cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
-                        } else if progressNumber.floatValue > 0 {
-                            cell.progressView.isHidden = false
-                            cell.progressView.progress = progressNumber.floatValue
-                            cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
-                        }
+        guard let userInfo = notification.userInfo as NSDictionary?,
+              let progressNumber = userInfo["progress"] as? NSNumber,
+              let totalBytes = userInfo["totalBytes"] as? Int64,
+              let totalBytesExpected = userInfo["totalBytesExpected"] as? Int64,
+              let ocId = userInfo["ocId"] as? String,
+              let (indexPath, _) = self.dataSource.getIndexPathMetadata(ocId: ocId) as? (IndexPath, NCMetadatasForSection?)
+        else {
+            return
+        }
+        let status = userInfo["status"] as? Int ?? NCGlobal.shared.metadataStatusNormal
+
+        if let cell = collectionView?.cellForItem(at: indexPath) {
+            if cell is NCListCell {
+                let cell = cell as! NCListCell
+                if progressNumber.floatValue == 1 {
+                    cell.progressView?.isHidden = true
+                    cell.progressView?.progress = .zero
+                    cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
+                    if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
+                        cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
+                    } else {
+                        cell.labelInfo.text = ""
+                    }
+                } else if progressNumber.floatValue > 0 {
+                    cell.progressView?.isHidden = false
+                    cell.progressView?.progress = progressNumber.floatValue
+                    cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
+                    if status == NCGlobal.shared.metadataStatusInDownload {
+                        cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
+                    } else if status == NCGlobal.shared.metadataStatusInUpload {
+                        cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
                     }
                 }
+            } else if cell is NCTransferCell {
+                let cell = cell as! NCTransferCell
+                if progressNumber.floatValue == 1 {
+                    cell.progressView?.isHidden = true
+                    cell.progressView?.progress = .zero
+                    cell.buttonMore.isHidden = true
+                    cell.labelInfo.text = ""
+                } else if progressNumber.floatValue > 0 {
+                    cell.progressView?.isHidden = false
+                    cell.progressView?.progress = progressNumber.floatValue
+                    cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
+                    if status == NCGlobal.shared.metadataStatusInDownload {
+                        cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
+                    } else if status == NCGlobal.shared.metadataStatusInUpload {
+                        cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
+                    }
+                }
+            } else if cell is NCGridCell {
+                let cell = cell as! NCGridCell
+                if progressNumber.floatValue == 1 {
+                    cell.progressView.isHidden = true
+                    cell.progressView.progress = .zero
+                    cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
+                } else if progressNumber.floatValue > 0 {
+                    cell.progressView.isHidden = false
+                    cell.progressView.progress = progressNumber.floatValue
+                    cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
+                }
             }
         }
     }