소스 검색

coding

Signed-off-by: marinofaggiana <ios@nextcloud.com>
marinofaggiana 2 년 전
부모
커밋
b8f1f90559

+ 13 - 9
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -54,6 +54,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
     internal var groupByField = "name"
     internal var providers: [NCCSearchProvider]?
     internal var searchResults: [NCCSearchResult]?
+    internal var timerUnifiedSearch: Timer?
 
     internal var listLayout: NCListLayout!
     internal var gridLayout: NCGridLayout!
@@ -1058,6 +1059,15 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
     @objc func reloadDataSourceNetwork(forced: Bool = false) { }
 
+    @objc func unifiedSearchFilesTimer() {
+        if NCOperationQueue.shared.dataSourceAddSectionCount() == 0 {
+            self.timerUnifiedSearch?.invalidate()
+            self.refreshControl.endRefreshing()
+            self.isReloadDataSourceNetworkInProgress = false
+            self.collectionView.reloadData()
+        }
+    }
+
     @objc func networkSearch() {
         guard !appDelegate.account.isEmpty, let literalSearch = literalSearch, !literalSearch.isEmpty
         else {
@@ -1073,7 +1083,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
         let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
         if serverVersionMajor >= NCGlobal.shared.nextcloudVersion20 {
-
+            self.timerUnifiedSearch?.invalidate()
             NCNetworking.shared.unifiedSearchFiles(urlBase: appDelegate, literal: literalSearch) { allProviders in
                 self.providers = allProviders
                 self.searchResults = []
@@ -1091,14 +1101,8 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
             } update: { id, searchResult, metadatas in
                 guard let metadatas = metadatas, metadatas.count > 0, self.isSearching , let searchResult = searchResult else { return }
                 NCOperationQueue.shared.dataSourceAddSection(collectionViewCommon: self, metadatas: metadatas, searchResult: searchResult)
-            } completion: {searchResults, errorCode, errorDescription in
-//                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
-//                    self.searchResults = searchResults
-//                    self.refreshControl.endRefreshing()
-//                    self.isReloadDataSourceNetworkInProgress = false
-//                    self.collectionView.reloadData()
-//                }
-                
+            } completion: {errorCode, errorDescription in
+                self.timerUnifiedSearch = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.unifiedSearchFilesTimer), userInfo: nil, repeats: true)
             }
 
         } else {

+ 4 - 6
iOSClient/Networking/NCNetworking.swift

@@ -931,15 +931,14 @@ import Queuer
 
     /// Unified Search (NC>=20)
     ///
-    func unifiedSearchFiles(urlBase: NCUserBaseUrl, literal: String, providers: @escaping ([NCCSearchProvider]?) -> Void, update: @escaping (_ id: String, NCCSearchResult?, [tableMetadata]?) -> Void, completion: @escaping ([NCCSearchResult]?, _ errorCode: Int, _ errorDescription: String) -> ()) {
+    func unifiedSearchFiles(urlBase: NCUserBaseUrl, literal: String, providers: @escaping ([NCCSearchProvider]?) -> Void, update: @escaping (_ id: String, NCCSearchResult?, [tableMetadata]?) -> Void, completion: @escaping (_ errorCode: Int, _ errorDescription: String) -> ()) {
 
-        var searchResults: [NCCSearchResult]?
         var errorCode = 0
         var errorDescription = ""
         let dispatchGroup = DispatchGroup()
         dispatchGroup.enter()
         dispatchGroup.notify(queue: .main) {
-            completion(searchResults, errorCode, errorDescription)
+            completion(errorCode, errorDescription)
         }
 
         NCCommunication.shared.unifiedSearch(term: literal, timeout: 30, timeoutProvider: 90) { provider in
@@ -994,10 +993,9 @@ import Queuer
                 })
             }
             update(provider.id, partialResult, metadatas)
-        } completion: { results, code, description in
+        } completion: { err, description in
             self.requestsUnifiedSearch.removeAll()
-            searchResults = results
-            errorCode = code
+            errorCode = err
             errorDescription = description
             dispatchGroup.leave()
         }

+ 10 - 0
iOSClient/Networking/NCOperationQueue.swift

@@ -48,6 +48,7 @@ import NCCommunication
         synchronizationCancelAll()
         downloadThumbnailCancelAll()
         downloadAvatarCancelAll()
+        dataSourceAddSectionCancelAll()
     }
 
     // Download file
@@ -199,9 +200,18 @@ import NCCommunication
     }
 
     // Datasource
+
     func dataSourceAddSection(collectionViewCommon: NCCollectionViewCommon, metadatas: [tableMetadata], searchResult: NCCSearchResult) {
         dataSourceQueue.addOperation(NCOperationDataSource.init(collectionViewCommon: collectionViewCommon, metadatas: metadatas, searchResult: searchResult))
     }
+
+    @objc func dataSourceAddSectionCancelAll() {
+        dataSourceQueue.cancelAll()
+    }
+
+    func dataSourceAddSectionCount() -> Int {
+        return dataSourceQueue.operationCount
+    }
 }
 
 // MARK: -