Browse Source

coding

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

+ 25 - 7
iOSClient/Data/NCDataSource.swift

@@ -80,9 +80,7 @@ class NCDataSource: NSObject {
 
 
     func addSection(metadatas: [tableMetadata], searchResult: NCCSearchResult?) {
     func addSection(metadatas: [tableMetadata], searchResult: NCCSearchResult?) {
 
 
-        for metadata in metadatas {
-            self.metadatasSource.append(metadata)
-        }
+        self.metadatasSource.append(contentsOf: metadatas)
 
 
         if let searchResult = searchResult {
         if let searchResult = searchResult {
             self.searchResults?.append(searchResult)
             self.searchResults?.append(searchResult)
@@ -161,7 +159,7 @@ class NCDataSource: NSObject {
                                                             metadatas: metadatas,
                                                             metadatas: metadatas,
                                                             shares: self.shares,
                                                             shares: self.shares,
                                                             localFiles: self.localFiles,
                                                             localFiles: self.localFiles,
-                                                            searchResult: searchResult,
+                                                            lastSearchResult: searchResult,
                                                             sort: self.sort,
                                                             sort: self.sort,
                                                             ascending: self.ascending,
                                                             ascending: self.ascending,
                                                             directoryOnTop: self.directoryOnTop,
                                                             directoryOnTop: self.directoryOnTop,
@@ -172,6 +170,26 @@ class NCDataSource: NSObject {
 
 
     // MARK: -
     // MARK: -
 
 
+    func appendMetadatasToSection(_ metadatas: [tableMetadata], metadataForSection: NCMetadataForSection, lastSearchResult: NCCSearchResult) -> [IndexPath] {
+        
+        guard let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == metadataForSection.sectionValue }) else { return [] }
+
+        var indexPaths: [IndexPath] = []
+
+        self.metadatasSource.append(contentsOf: metadatas)
+        metadataForSection.metadatas.append(contentsOf: metadatas)
+        metadataForSection.lastSearchResult = lastSearchResult
+        metadataForSection.createMetadatasForSection()
+
+        for metadata in metadatas {
+            if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
+                indexPaths.append(IndexPath(row: rowIndex, section: sectionIndex))
+            }
+        }
+
+        return indexPaths
+    }
+
     @discardableResult
     @discardableResult
     func addMetadata(_ metadata: tableMetadata) -> (indexPath: IndexPath?, sameSections: Bool) {
     func addMetadata(_ metadata: tableMetadata) -> (indexPath: IndexPath?, sameSections: Bool) {
 
 
@@ -364,7 +382,7 @@ class NCMetadataForSection: NSObject {
     var metadatas: [tableMetadata]
     var metadatas: [tableMetadata]
     var shares: [tableShare]
     var shares: [tableShare]
     var localFiles: [tableLocalFile]
     var localFiles: [tableLocalFile]
-    var searchResult: NCCSearchResult?
+    var lastSearchResult: NCCSearchResult?
     var unifiedSearchInProgress: Bool = false
     var unifiedSearchInProgress: Bool = false
 
 
     private var sort : String
     private var sort : String
@@ -386,13 +404,13 @@ class NCMetadataForSection: NSObject {
     public var metadataOffLine: [String] = []
     public var metadataOffLine: [String] = []
 
 
 
 
-    init(sectionValue: String, metadatas: [tableMetadata], shares: [tableShare], localFiles: [tableLocalFile], searchResult: NCCSearchResult?, sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
+    init(sectionValue: String, metadatas: [tableMetadata], shares: [tableShare], localFiles: [tableLocalFile], lastSearchResult: NCCSearchResult?, sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
 
 
         self.sectionValue = sectionValue
         self.sectionValue = sectionValue
         self.metadatas = metadatas
         self.metadatas = metadatas
         self.shares = shares
         self.shares = shares
         self.localFiles = localFiles
         self.localFiles = localFiles
-        self.searchResult = searchResult
+        self.lastSearchResult = lastSearchResult
         self.sort = sort
         self.sort = sort
         self.ascending = ascending
         self.ascending = ascending
         self.directoryOnTop = directoryOnTop
         self.directoryOnTop = directoryOnTop

+ 10 - 16
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -1094,7 +1094,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
 
     func unifiedSearchMore(metadataForSection: NCMetadataForSection?) {
     func unifiedSearchMore(metadataForSection: NCMetadataForSection?) {
 
 
-        guard let metadataForSection = metadataForSection, let searchResult = metadataForSection.searchResult, let cursor = searchResult.cursor, let term = literalSearch else { return }
+        guard let metadataForSection = metadataForSection, let searchResult = metadataForSection.lastSearchResult, let cursor = searchResult.cursor, let term = literalSearch else { return }
 
 
         metadataForSection.unifiedSearchInProgress = true
         metadataForSection.unifiedSearchInProgress = true
         self.collectionView?.reloadData()
         self.collectionView?.reloadData()
@@ -1104,15 +1104,9 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
             metadataForSection.unifiedSearchInProgress = false
             metadataForSection.unifiedSearchInProgress = false
             guard let searchResult = searchResult, let metadatas = metadatas else { return }
             guard let searchResult = searchResult, let metadatas = metadatas else { return }
 
 
-            metadataForSection.searchResult = searchResult
-            var indexPaths: [IndexPath] = []
-            for metadata in metadatas {
-                self.metadatasSource.append(metadata)
-                let (indexPath, sameSections) = self.dataSource.addMetadata(metadata)
-                if let indexPath = indexPath, sameSections {
-                    indexPaths.append(indexPath)
-                }
-            }
+            self.metadatasSource.append(contentsOf: metadatas)
+            let indexPaths = self.dataSource.appendMetadatasToSection(metadatas, metadataForSection: metadataForSection, lastSearchResult: searchResult)
+
             DispatchQueue.main.async {
             DispatchQueue.main.async {
                 self.collectionView?.performBatchUpdates({
                 self.collectionView?.performBatchUpdates({
                     self.collectionView?.insertItems(at: indexPaths)
                     self.collectionView?.insertItems(at: indexPaths)
@@ -1810,8 +1804,8 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
             let sections = dataSource.numberOfSections()
             let sections = dataSource.numberOfSections()
             let section = indexPath.section
             let section = indexPath.section
             let metadataForSection = self.dataSource.getMetadataForSection(indexPath.section)
             let metadataForSection = self.dataSource.getMetadataForSection(indexPath.section)
-            let isPaginated = metadataForSection?.searchResult?.isPaginated ?? false
-            let entriesCount: Int = metadataForSection?.searchResult?.entries.count ?? 0
+            let isPaginated = metadataForSection?.lastSearchResult?.isPaginated ?? false
+            let metadatasCount: Int = metadataForSection?.metadatas.count ?? 0
             let unifiedSearchInProgress = metadataForSection?.unifiedSearchInProgress ?? false
             let unifiedSearchInProgress = metadataForSection?.unifiedSearchInProgress ?? false
 
 
             footer.delegate = self
             footer.delegate = self
@@ -1827,7 +1821,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
                 if sections > 1 && section != sections - 1 {
                 if sections > 1 && section != sections - 1 {
                     footer.separatorIsHidden(false)
                     footer.separatorIsHidden(false)
                 }
                 }
-                if isSearching && isPaginated && entriesCount > 0 {
+                if isSearching && isPaginated && metadatasCount > 0 {
                     footer.buttonIsHidden(false)
                     footer.buttonIsHidden(false)
                 }
                 }
                 if unifiedSearchInProgress {
                 if unifiedSearchInProgress {
@@ -1901,8 +1895,8 @@ extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
 
 
         let sections = dataSource.numberOfSections()
         let sections = dataSource.numberOfSections()
         let metadataForSection = self.dataSource.getMetadataForSection(section)
         let metadataForSection = self.dataSource.getMetadataForSection(section)
-        let isPaginated = metadataForSection?.searchResult?.isPaginated ?? false
-        let entriesCount: Int = metadataForSection?.searchResult?.entries.count ?? 0
+        let isPaginated = metadataForSection?.lastSearchResult?.isPaginated ?? false
+        let metadatasCount: Int = metadataForSection?.lastSearchResult?.entries.count ?? 0
         var size = CGSize(width: collectionView.frame.width, height: 0)
         var size = CGSize(width: collectionView.frame.width, height: 0)
 
 
         if section == sections - 1 {
         if section == sections - 1 {
@@ -1911,7 +1905,7 @@ extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
             size.height += NCGlobal.shared.heightFooter
             size.height += NCGlobal.shared.heightFooter
         }
         }
 
 
-        if isSearching && isPaginated && entriesCount > 0 {
+        if isSearching && isPaginated && metadatasCount > 0 {
             size.height += NCGlobal.shared.heightFooterButton
             size.height += NCGlobal.shared.heightFooterButton
         }
         }