瀏覽代碼

coding

Signed-off-by: marinofaggiana <ios@nextcloud.com>
marinofaggiana 2 年之前
父節點
當前提交
502b85063c
共有 2 個文件被更改,包括 63 次插入70 次删除
  1. 19 11
      iOSClient/Data/NCDataSource.swift
  2. 44 59
      iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

+ 19 - 11
iOSClient/Data/NCDataSource.swift

@@ -127,7 +127,9 @@ class NCDataSource: NSObject {
     // MARK: -
 
     @discardableResult
-    func addMetadata(_ metadata: tableMetadata) -> IndexPath? {
+    func addMetadata(_ metadata: tableMetadata) -> (indexPath: IndexPath?, sameSections: Bool) {
+
+        let numberOfSections = self.numberOfSections()
 
         // ADD metadatasSource
         if let rowIndex = self.metadatasSource.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
@@ -141,14 +143,14 @@ class NCDataSource: NSObject {
             let metadataForSection = metadatasForSection[sectionIndex]
             if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
                 metadataForSection.metadatas[rowIndex] = metadata
-                return IndexPath(row: rowIndex, section: sectionIndex)
+                return (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
             } else {
                 metadataForSection.metadatas.append(metadata)
                 metadataForSection.createMetadatasForSection()
                 if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
-                    return IndexPath(row: rowIndex, section: sectionIndex)
+                    return (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
                 }
-                return nil
+                return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
             }
         } else {
             // NEW section
@@ -159,16 +161,17 @@ class NCDataSource: NSObject {
             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 (IndexPath(row: rowIndex, section: sectionIndex), self.isSameNumbersOfSections(numberOfSections: numberOfSections))
                 }
             }
         }
 
-        return nil
+        return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
     }
 
-    func deleteMetadata(ocId: String) -> IndexPath? {
+    func deleteMetadata(ocId: String) -> (indexPath: IndexPath?, sameSections: Bool) {
 
+        let numberOfSections = self.numberOfSections()
         var indexPathReturn: IndexPath?
         var removeMetadataForSection = false
         var sectionValue = ""
@@ -201,17 +204,18 @@ class NCDataSource: NSObject {
             }
         }
 
-        return indexPathReturn
+        return (indexPathReturn, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
     }
 
     @discardableResult
-    func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> IndexPath? {
+    func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> (indexPath: IndexPath?, sameSections: Bool) {
 
+        let numberOfSections = self.numberOfSections()
         var ocIdSearch = ocId
         var indexPath: IndexPath?
         var metadataForSection: NCMetadatasForSection?
 
-        guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return nil }
+        guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return (nil, self.isSameNumbersOfSections(numberOfSections: numberOfSections)) }
 
         if let ocIdTemp = ocIdTemp {
             ocIdSearch = ocIdTemp
@@ -229,7 +233,7 @@ class NCDataSource: NSObject {
             self.metadatasSource[rowIndex] = metadata
         }
 
-        return indexPath
+        return (indexPath, self.isSameNumbersOfSections(numberOfSections: numberOfSections))
     }
 
     // MARK: -
@@ -252,6 +256,10 @@ class NCDataSource: NSObject {
         return (nil, nil)
     }
 
+    func isSameNumbersOfSections(numberOfSections: Int) -> Bool {
+        return numberOfSections == self.numberOfSections()
+    }
+
     func numberOfSections() -> Int {
 
         if self.metadatasForSection.count == 0 {

+ 44 - 59
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -377,21 +377,11 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
         if fileNameView.lowercased() == NCGlobal.shared.fileNameRichWorkspace.lowercased() {
             reloadDataSourceNetwork(forced: true)
         } else if onlyLocalCache {
-            if let indexPath = dataSource.reloadMetadata(ocId: ocId), (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
-                collectionView?.reloadItems(at: [indexPath])
-            } else {
-                reloadDataSource()
-            }
+            let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: ocId)
+            self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
         } else {
-            if let indexPath = dataSource.deleteMetadata(ocId: ocId), (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
-                collectionView?.performBatchUpdates({
-                    collectionView?.deleteItems(at: [indexPath])
-                }, completion: { _ in
-                    self.collectionView?.reloadData()
-                })
-            } else {
-                reloadDataSource()
-            }
+            let (indexPath, sameSections) = dataSource.deleteMetadata(ocId: ocId)
+            self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
         }
     }
 
@@ -400,17 +390,12 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
         guard let userInfo = notification.userInfo as NSDictionary?,
               let ocId = userInfo["ocId"] as? String,
               let serverUrlFrom = userInfo["serverUrlFrom"] as? String,
-              serverUrlFrom == self.serverUrl,
-              let indexPath = dataSource.deleteMetadata(ocId: ocId),
-              (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section))
+              serverUrlFrom == self.serverUrl
         else {
             return
         }
-        collectionView?.performBatchUpdates({
-            collectionView?.deleteItems(at: [indexPath])
-        }, completion: { _ in
-            self.collectionView?.reloadData()
-        })
+        let (indexPath, sameSections) = dataSource.deleteMetadata(ocId: ocId)
+        self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
     }
 
     @objc func copyFile(_ notification: NSNotification) {
@@ -441,57 +426,49 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
     @objc func favoriteFile(_ notification: NSNotification) {
 
         guard let userInfo = notification.userInfo as NSDictionary?,
-              let ocId = userInfo["ocId"] as? String,
-              let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
+              let ocId = userInfo["ocId"] as? String
         else {
             reloadDataSource()
             return
         }
-        dataSource.reloadMetadata(ocId: metadata.ocId)
+        dataSource.reloadMetadata(ocId: ocId)
         collectionView?.reloadData()
     }
 
     @objc func downloadStartFile(_ notification: NSNotification) {
 
         guard let userInfo = notification.userInfo as NSDictionary?,
-              let ocId = userInfo["ocId"] as? String,
-              let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
-              let indexPath = dataSource.reloadMetadata(ocId: metadata.ocId),
-              (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section))
+              let ocId = userInfo["ocId"] as? String
         else {
             reloadDataSource()
             return
         }
-        collectionView?.reloadItems(at: [indexPath])
+        let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: ocId)
+        self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
     }
 
     @objc func downloadedFile(_ notification: NSNotification) {
 
         guard let userInfo = notification.userInfo as NSDictionary?,
-              let ocId = userInfo["ocId"] as? String,
-              let _ = userInfo["errorCode"] as? Int,
-              let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
-              let indexPath = dataSource.reloadMetadata(ocId: metadata.ocId),
-              (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section))
+              let ocId = userInfo["ocId"] as? String
         else {
             reloadDataSource()
             return
         }
-        collectionView?.reloadItems(at: [indexPath])
+        let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: ocId)
+        self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
     }
 
     @objc func downloadCancelFile(_ notification: NSNotification) {
 
         guard let userInfo = notification.userInfo as NSDictionary?,
-              let ocId = userInfo["ocId"] as? String,
-              let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
-              let indexPath = dataSource.reloadMetadata(ocId: metadata.ocId),
-              (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section))
+              let ocId = userInfo["ocId"] as? String
         else {
             reloadDataSource()
             return
         }
-        collectionView?.reloadItems(at: [indexPath])
+        let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: ocId)
+        self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
     }
 
     @objc func uploadStartFile(_ notification: NSNotification) {
@@ -499,16 +476,12 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
         guard let userInfo = notification.userInfo as NSDictionary?,
               let ocId = userInfo["ocId"] as? String,
               let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
-              (metadata.serverUrl == serverUrl && metadata.account == appDelegate.account),
-              let indexPath = dataSource.addMetadata(metadata)
+              (metadata.serverUrl == serverUrl && metadata.account == appDelegate.account)
         else {
             return
         }
-        collectionView?.performBatchUpdates({
-            collectionView?.insertItems(at: [indexPath])
-        }, completion: { _ in
-            self.collectionView?.reloadData()
-        })
+        let (indexPath, sameSections) = dataSource.addMetadata(metadata)
+        self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
     }
 
     @objc func uploadedFile(_ notification: NSNotification) {
@@ -517,12 +490,12 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
               let ocId = userInfo["ocId"] as? String,
               let ocIdTemp = userInfo["ocIdTemp"] as? String,
               let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
-              (metadata.serverUrl == serverUrl && metadata.account == appDelegate.account),
-              let indexPath = dataSource.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp)
+              (metadata.serverUrl == serverUrl && metadata.account == appDelegate.account)
         else {
             return
         }
-        collectionView?.reloadItems(at: [indexPath])
+        let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp)
+        self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
     }
 
     @objc func uploadCancelFile(_ notification: NSNotification) {
@@ -531,17 +504,12 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
               let ocId = userInfo["ocId"] as? String,
               let serverUrl = userInfo["serverUrl"] as? String,
               let account = userInfo["account"] as? String,
-              (serverUrl == self.serverUrl && account == appDelegate.account),
-              let indexPath = dataSource.deleteMetadata(ocId: ocId),
-              (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section))
+              (serverUrl == self.serverUrl && account == appDelegate.account)
         else {
             return
         }
-        collectionView?.performBatchUpdates({
-            collectionView?.deleteItems(at: [indexPath])
-        }, completion: { _ in
-            self.collectionView?.reloadData()
-        })
+        let (indexPath, sameSections) = dataSource.deleteMetadata(ocId: ocId)
+        self.reloadCollectionView(indexPath: indexPath, sameSections: sameSections)
     }
 
     @objc func triggerProgressTask(_ notification: NSNotification) {
@@ -984,6 +952,23 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
     // MARK: - DataSource + NC Endpoint
 
+    internal func reloadCollectionView(indexPath: IndexPath?, sameSections: Bool) {
+
+        if let indexPath = indexPath {
+            if sameSections && (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
+                collectionView?.performBatchUpdates({
+                    collectionView?.insertItems(at: [indexPath])
+                }, completion: { _ in
+                    self.collectionView?.reloadData()
+                })
+            } else {
+                self.collectionView?.reloadData()
+            }
+        } else {
+            reloadDataSource()
+        }
+    }
+
     @objc func reloadDataSource() {
 
         if appDelegate.account == "" { return }