Browse Source

coding

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

+ 30 - 4
iOSClient/Data/NCDataSource.swift

@@ -58,14 +58,11 @@ class NCDataSource: NSObject {
         self.favoriteOnTop = favoriteOnTop ?? true
         self.filterLivePhoto = filterLivePhoto ?? true
         self.groupByField = groupByField
+        // unified search
         self.providers = providers
         self.searchResults = searchResults
 
         createSections()
-
-        for sectionValue in self.sectionsValue {
-            createMetadataForSection(sectionValue: sectionValue)
-        }
     }
 
     // MARK: -
@@ -81,6 +78,19 @@ class NCDataSource: NSObject {
         self.localFiles.removeAll()
     }
 
+    func addSection(metadatas: [tableMetadata], searchResult: NCCSearchResult?) {
+
+        for metadata in metadatas {
+            self.metadatasSource.append(metadata)
+        }
+
+        if let searchResult = searchResult {
+            self.searchResults?.append(searchResult)
+        }
+
+        createSections()
+    }
+
     internal func createSections() {
 
         for metadata in metadatasSource {
@@ -126,6 +136,13 @@ class NCDataSource: NSObject {
                 }
             }
         }
+
+        for sectionValue in self.sectionsValue {
+            if !existsMetadataForSection(sectionValue: sectionValue) {
+                print("DATASOURCE: create metadata for section: " + sectionValue)
+                createMetadataForSection(sectionValue: sectionValue)
+            }
+        }
     }
 
     internal func createMetadataForSection(sectionValue: String) {
@@ -340,6 +357,15 @@ class NCDataSource: NSObject {
         return (directories, files, size)
     }
 
+    func existsMetadataForSection(sectionValue: String) -> Bool {
+        for metadataForSection in self.metadatasForSection {
+            if metadataForSection.sectionValue == sectionValue {
+                return true
+            }
+        }
+        return false
+    }
+
     internal func getSectionValue(metadata: tableMetadata) -> String {
 
         switch self.groupByField {

+ 14 - 30
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -1030,13 +1030,6 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
     // MARK: - DataSource + NC Endpoint
 
-    func reloadDataThenPerform(_ closure: @escaping (() -> Void)) {
-        CATransaction.begin()
-        CATransaction.setCompletionBlock(closure)
-        self.collectionView?.reloadData()
-        CATransaction.commit()
-    }
-
     @objc func reloadDataSource() {
 
         if appDelegate.account == "" { return }
@@ -1083,31 +1076,22 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
             NCNetworking.shared.unifiedSearchFiles(urlBase: appDelegate, literal: literalSearch) { allProviders in
                 self.providers = allProviders
+                self.searchResults = []
+                self.dataSource = NCDataSource(
+                    metadatasSource: self.metadatasSource,
+                    account: self.appDelegate.account,
+                    sort: self.layoutForView?.sort,
+                    ascending: self.layoutForView?.ascending,
+                    directoryOnTop: self.layoutForView?.directoryOnTop,
+                    favoriteOnTop: true,
+                    filterLivePhoto: true,
+                    groupByField: self.groupByField,
+                    providers: self.providers,
+                    searchResults: self.searchResults)
             } update: { id, searchResult, metadatas in
-                guard let metadatas = metadatas, metadatas.count > 0, self.isSearching else { return }
-
-                print("[XXX:]" + id)
-                if id == "files" {
-                    print("files")
-                }
-
-                /*
-                self.searchResults = searchResults
-                self.metadatasSource = metadatas
-                self.dataSource = NCDataSource(metadatasSource: self.metadatasSource,
-                                               account: self.appDelegate.account,
-                                               sort: self.layoutForView?.sort,
-                                               ascending: self.layoutForView?.ascending,
-                                               directoryOnTop: self.layoutForView?.directoryOnTop,
-                                               favoriteOnTop: true,
-                                               filterLivePhoto: true,
-                                               providers: self.providers,
-                                               searchResults: self.searchResults)
-                DispatchQueue.main.sync {
-                    self.collectionView?.reloadData()
-                }
-                */
+                guard let metadatas = metadatas, metadatas.count > 0, self.isSearching , let searchResult = searchResult else { return }
 
+                NCOperationQueue.shared.dataSourceAddSection(dataSource: self.dataSource, metadatas: metadatas, searchResult: searchResult, collectionView: self.collectionView)
             } completion: {searchResults, errorCode, errorDescription in
 
                 self.searchResults = searchResults

+ 44 - 0
iOSClient/Networking/NCOperationQueue.swift

@@ -37,6 +37,7 @@ import NCCommunication
     private let synchronizationQueue = Queuer(name: "synchronizationQueue", maxConcurrentOperationCount: 1, qualityOfService: .default)
     private let downloadThumbnailQueue = Queuer(name: "downloadThumbnailQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
     private let downloadAvatarQueue = Queuer(name: "downloadAvatarQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
+    private let dataSourceQueue = Queuer(name: "dataSourceQueue", maxConcurrentOperationCount: 1, qualityOfService: .default)
 
     private var timerReadFileForMediaQueue: Timer?
 
@@ -196,6 +197,11 @@ import NCCommunication
     @objc func downloadAvatarCancelAll() {
         downloadAvatarQueue.cancelAll()
     }
+
+    // Datasource
+    func dataSourceAddSection(dataSource: NCDataSource, metadatas: [tableMetadata], searchResult: NCCSearchResult, collectionView: UICollectionView) {
+        dataSourceQueue.addOperation(NCOperationDataSource.init(dataSource: dataSource, metadatas: metadatas, searchResult: searchResult, collectionView: collectionView))
+    }
 }
 
 // MARK: -
@@ -523,3 +529,41 @@ class NCOperationDownloadAvatar: ConcurrentOperation {
         }
     }
 }
+
+// MARK: -
+
+class NCOperationDataSource: ConcurrentOperation {
+
+    var dataSource: NCDataSource
+    var metadatas: [tableMetadata]
+    var searchResult: NCCSearchResult
+    var collectionView: UICollectionView
+
+    init(dataSource:NCDataSource, metadatas: [tableMetadata], searchResult: NCCSearchResult, collectionView: UICollectionView) {
+        self.dataSource = dataSource
+        self.metadatas = metadatas
+        self.searchResult = searchResult
+        self.collectionView = collectionView
+    }
+
+    func reloadDataThenPerform(_ closure: @escaping (() -> Void)) {
+        DispatchQueue.main.async {
+            CATransaction.begin()
+            CATransaction.setCompletionBlock(closure)
+            self.collectionView.reloadData()
+            CATransaction.commit()
+        }
+    }
+
+    override func start() {
+
+        if isCancelled {
+            self.finish()
+        } else {
+            self.dataSource.addSection(metadatas: metadatas, searchResult: searchResult)
+            reloadDataThenPerform {
+                self.finish()
+            }
+        }
+    }
+}