Jelajahi Sumber

Show partial unified search + load new metadata

- Show partial search results directly instead of waiting for completion
- Load metadata if a file is not in the db yet and update list after load
  - use filePath or sublime (for fulltextsearch) to get file url

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch 3 tahun lalu
induk
melakukan
48be7cc8ec

+ 3 - 3
iOSClient/Activity/NCActivityTableViewCell.swift

@@ -175,9 +175,9 @@ extension NCActivityTableViewCell: UICollectionViewDelegate {
                     let serverUrl = (serverUrlFileName as NSString).deletingLastPathComponent
                     let fileName = (serverUrlFileName as NSString).lastPathComponent
                     let serverUrlFileName = serverUrl + "/" + fileName
-
-                    NCNetworking.shared.readFile(serverUrlFileName: serverUrlFileName, account: activityPreview.account) { account, metadata, errorCode, _ in
-
+                    
+                    NCNetworking.shared.readFile(serverUrlFileName: serverUrlFileName) { (account, metadata, errorCode, errorDescription) in
+                        
                         NCUtility.shared.stopActivityIndicator()
 
                         if account == self.appDelegate.account && errorCode == 0 {

+ 10 - 10
iOSClient/Files/NCFiles.swift

@@ -69,22 +69,22 @@ class NCFiles: NCCollectionViewCommon {
 
     override func reloadDataSource() {
         super.reloadDataSource()
-
-        DispatchQueue.global(qos: .background).async {
-
+        
+        DispatchQueue.main.async {
+                        
             if !self.isSearching && self.appDelegate.account != "" && self.appDelegate.urlBase != "" {
                 self.metadatasSource = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.serverUrl))
                 if self.metadataFolder == nil {
                     self.metadataFolder = NCManageDatabase.shared.getMetadataFolder(account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, serverUrl: self.serverUrl)
                 }
             }
-
-            self.dataSource = NCDataSource(metadatasSource: self.metadatasSource, sort: self.layoutForView?.sort, ascending: self.layoutForView?.ascending, directoryOnTop: self.layoutForView?.directoryOnTop, favoriteOnTop: true, filterLivePhoto: true)
-
-            DispatchQueue.main.async { [weak self] in
-                self?.refreshControl.endRefreshing()
-                self?.collectionView.reloadData()
-            }
+            
+            self.dataSource = NCDataSource.init(metadatasSource: self.metadatasSource, sort: self.layoutForView?.sort, ascending: self.layoutForView?.ascending, directoryOnTop: self.layoutForView?.directoryOnTop, favoriteOnTop: true, filterLivePhoto: true)
+            
+//            DispatchQueue.main.async { [weak self] in
+                self.refreshControl.endRefreshing()
+                self.collectionView.reloadData()
+//            }
         }
     }
 

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

@@ -948,25 +948,29 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
         isReloadDataSourceNetworkInProgress = true
         collectionView?.reloadData()
 
-        NCNetworking.shared.searchFiles(urlBase: appDelegate.urlBase, user: appDelegate.user, literal: literalSearch) { (account, metadatas, errorCode, errorDescription) in
-
+        NCNetworking.shared.unifiedSearchFiles(urlBase: appDelegate, user: appDelegate.user, literal: literalSearch) { metadatas in
+            guard let metadatas = metadatas else { return }
             DispatchQueue.main.async {
-                if self.searchController?.isActive == true, errorCode == 0, let metadatas = metadatas {
-                    self.metadatasSource = metadatas
-                }
-                self.refreshControl.endRefreshing()
-                self.isReloadDataSourceNetworkInProgress = false
+                self.metadatasSource = Array(metadatas)
                 self.reloadDataSource()
             }
+        } completion: { metadatas, errorCode, errorDescription in
+            if self.searchController?.isActive == true, errorCode == 0, let metadatas = metadatas {
+                self.metadatasSource = Array(metadatas)
+            }
+            self.refreshControl.endRefreshing()
+            self.isReloadDataSourceNetworkInProgress = false
+            self.reloadDataSource()
         }
+
     }
 
     @objc func networkReadFolder(forced: Bool, completion: @escaping(_ tableDirectory: tableDirectory?, _ metadatas: [tableMetadata]?, _ metadatasUpdate: [tableMetadata]?, _ metadatasDelete: [tableMetadata]?, _ errorCode: Int, _ errorDescription: String) -> Void) {
 
         var tableDirectory: tableDirectory?
-
-        NCNetworking.shared.readFile(serverUrlFileName: serverUrl, account: appDelegate.account) { account, metadataFolder, errorCode, errorDescription in
-
+        
+        NCNetworking.shared.readFile(serverUrlFileName: serverUrl) { (account, metadataFolder, errorCode, errorDescription) in
+            
             if errorCode == 0 {
 
                 if let metadataFolder = metadataFolder {

+ 67 - 4
iOSClient/Networking/NCNetworking.swift

@@ -880,7 +880,7 @@ import Queuer
         }
     }
 
-    @objc func readFile(serverUrlFileName: String, account: String, queue: DispatchQueue = NCCommunicationCommon.shared.backgroundQueue, completion: @escaping (_ account: String, _ metadata: tableMetadata?, _ errorCode: Int, _ errorDescription: String) -> Void) {
+    @objc func readFile(serverUrlFileName: String, queue: DispatchQueue = NCCommunicationCommon.shared.backgroundQueue, completion: @escaping (_ account: String, _ metadata: tableMetadata?, _ errorCode: Int, _ errorDescription: String) -> Void) {
 
         NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: CCUtility.getShowHiddenFiles(), queue: queue) { account, files, _, errorCode, errorDescription in
 
@@ -925,6 +925,69 @@ import Queuer
         }
     }
 
+    @objc func unifiedSearchFiles(urlBase: NCUserBaseUrl, user: String, literal: String, update: @escaping (Set<tableMetadata>?) -> Void, completion: @escaping (_ metadatas: Set<tableMetadata>?, _ errorCode: Int, _ errorDescription: String) -> ()) {
+        var seachFiles = Set<tableMetadata>()
+        var errCode = 0
+        var errDescr = ""
+        let dispatchGroup = DispatchGroup()
+        dispatchGroup.notify(queue: .main) {
+            completion(seachFiles, errCode, errDescr)
+        }
+        dispatchGroup.enter()
+
+        NCCommunication.shared.unifiedSearch(term: literal) { parialResult, provider, errorCode, errorDescription in
+            guard let parialResult = parialResult else { return }
+            // WARNING: SUPER HACKY SOLUTION!!
+            // FIXME: Needs to be fixed by FTS team
+            if parialResult.name == "Full Text Search" {
+                parialResult.entries.forEach({ entry in
+                    let comp = entry.subline.split(separator: "/")
+                    let path = comp.dropLast().joined(separator: "/")
+                    guard let lastComp = comp.last else { return }
+                    if let tableFile = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(
+                              format: "serverUrl == %@ && fileName == %@",
+                              urlBase.urlBase + "/remote.php/dav/files/" + user + "/" + path,
+                              String(lastComp))) {
+                        seachFiles.insert(tableFile)
+                    } else {
+                        self.loadMetadata(urlBase: urlBase, filePath: "/" + entry.subline, dispatchGroup: dispatchGroup) { newMetadata in
+                            seachFiles.insert(newMetadata)
+                        }
+                    }
+                })
+            } else if parialResult.name == "Files" {
+                parialResult.entries.forEach({ entry in
+                    if let fileId = entry.fileId,
+                       let result = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "fileId == %@", String(fileId))) {
+                        seachFiles.insert(result)
+                    } else if let filePath = entry.filePath {
+                        self.loadMetadata(urlBase: urlBase, filePath: filePath, dispatchGroup: dispatchGroup) { newMetadata in
+                            seachFiles.insert(newMetadata)
+                        }
+                    } else { print(#function, "[ERROR]: File has no path: \(entry)") }
+                })
+            }
+            update(seachFiles)
+        } completion: { results, errorCode, errorDescription in
+            dispatchGroup.leave()
+            errCode = errorCode
+            errDescr = errorDescription
+        }
+    }
+
+    func loadMetadata(urlBase: NCUserBaseUrl, filePath: String, dispatchGroup: DispatchGroup, completion: @escaping (tableMetadata) -> Void) {
+        let urlPath = urlBase.urlBase + "/remote.php/dav/files/" + urlBase.user + filePath
+        dispatchGroup.enter()
+        self.readFile(serverUrlFileName: urlPath) { account, metadata, errorCode, errorDescription in
+            defer { dispatchGroup.leave() }
+            guard let metadata = metadata else { return }
+            DispatchQueue.main.async {
+                NCManageDatabase.shared.addMetadata(metadata)
+            }
+            completion(metadata)
+        }
+    }
+
     // MARK: - WebDav Create Folder
 
     @objc func createFolder(fileName: String, serverUrl: String, account: String, urlBase: String, overwrite: Bool = false, completion: @escaping (_ errorCode: Int, _ errorDescription: String) -> Void) {
@@ -957,9 +1020,9 @@ import Queuer
         NCCommunication.shared.createFolder(fileNameFolderUrl) { account, ocId, _, errorCode, errorDescription in
 
             if errorCode == 0 {
-
-                self.readFile(serverUrlFileName: fileNameFolderUrl, account: account) { account, metadataFolder, errorCode, errorDescription in
-
+                
+                self.readFile(serverUrlFileName: fileNameFolderUrl) { (account, metadataFolder, errorCode, errorDescription) in
+                    
                     if errorCode == 0 {
 
                         if let metadata = metadataFolder {

+ 2 - 2
iOSClient/Networking/NCNetworkingChunkedUpload.swift

@@ -155,8 +155,8 @@ extension NCNetworking {
                             NCManageDatabase.shared.deleteChunks(account: metadata.account, ocId: metadata.ocId)
                             NCUtilityFileSystem.shared.deleteFile(filePath: directoryProviderStorageOcId)
 
-                            self.readFile(serverUrlFileName: serverUrlFileNameDestination, account: metadata.account) { _, metadata, _, _ in
-
+                            self.readFile(serverUrlFileName: serverUrlFileNameDestination) { (_, metadata, _, _) in
+                                    
                                 if errorCode == 0, let metadata = metadata {
 
                                     metadata.assetLocalIdentifier = assetLocalIdentifier

+ 3 - 3
iOSClient/RichWorkspace/NCViewerRichWorkspace.swift

@@ -59,9 +59,9 @@ import MarkdownKit
 
     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
-
-        NCNetworking.shared.readFile(serverUrlFileName: serverUrl, account: appDelegate.account) { account, metadata, errorCode, _ in
-
+        
+        NCNetworking.shared.readFile(serverUrlFileName: serverUrl) { (account, metadata, errorCode, errorDescription) in
+            
             if errorCode == 0 && account == self.appDelegate.account {
                 guard let metadata = metadata else { return }
                 NCManageDatabase.shared.setDirectory(richWorkspace: metadata.richWorkspace, serverUrl: self.serverUrl, account: account)