marinofaggiana 4 years ago
parent
commit
c506234d95
2 changed files with 36 additions and 39 deletions
  1. 25 26
      iOSClient/Favorites/NCFavorite.swift
  2. 11 13
      iOSClient/Main/NCCollectionCommon.swift

+ 25 - 26
iOSClient/Favorites/NCFavorite.swift

@@ -93,33 +93,45 @@ class NCFavorite: NCCollectionViewCommon  {
     // MARK: - NC API & Algorithm
     
     override func reloadDataSource() {
-        super.reloadDataSource()
-        
+          
         var sort: String
         var ascending: Bool
         var directoryOnTop: Bool
         
         (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey)
         
-        if serverUrl == "" {
-            
-            let metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND favorite == true", appDelegate.account))
-            self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: true)
-            
-        } else {
-            
-            let metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
-            self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: true)
+        if searchController?.isActive ?? false == false {
+       
+            if serverUrl == "" {
+                metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND favorite == true", appDelegate.account))
+            } else {
+                metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
+            }
         }
         
+        self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: true)
+        
         refreshControl.endRefreshing()
         collectionView.reloadData()
     }
     
     override func reloadDataSourceNetwork() {
-        super.reloadDataSourceNetwork()
+       
+        if searchController?.isActive ?? false {
         
-        if serverUrl == "" {
+            if literalSearch == nil || literalSearch?.count ?? 0 < 2 {
+                self.reloadDataSource()
+                return
+            }
+            
+            NCNetworking.shared.searchFiles(urlBase: appDelegate.urlBase, user: appDelegate.user, literal: literalSearch!) { (account, metadatas, errorCode, errorDescription) in
+                if self.searchController?.isActive ?? false && errorCode == 0 {
+                    self.metadatasSource = metadatas!
+                }
+                self.reloadDataSource()
+            }
+            
+        } else if serverUrl == "" {
             
             NCNetworking.shared.listingFavoritescompletion(selector: selectorListingFavorite) { (account, metadatas, errorCode, errorDescription) in
                 if errorCode == 0 {
@@ -154,18 +166,5 @@ class NCFavorite: NCCollectionViewCommon  {
             }
         }
     }
-    
-    override func searchDataSourceNetwork() {
-        super.searchDataSourceNetwork()
-        
-        guard let literalSearch = literalSearch else { return }
-        if literalSearch == "" { return }
-        
-        NCNetworking.shared.searchFiles(urlBase: appDelegate.urlBase, user: appDelegate.user, literal: literalSearch) { (account, metadatas, errorCode, errorDescription) in
-            if self.isSearching {
-                
-            }
-        }
-    }
 }
 

+ 11 - 13
iOSClient/Main/NCCollectionCommon.swift

@@ -627,6 +627,9 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
     @IBOutlet weak var collectionView: UICollectionView!
 
+    internal let refreshControl = UIRefreshControl()
+    internal var searchController: UISearchController?
+    
     @objc var serverUrl = ""
         
     let appDelegate = UIApplication.shared.delegate as! AppDelegate
@@ -634,7 +637,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
     internal var metadataPush: tableMetadata?
     internal var isEditMode = false
     internal var selectOcId: [String] = []
-        
+    internal var metadatasSource: [tableMetadata] = []
     internal var dataSource: NCDataSource?
         
     internal var layout = ""
@@ -657,8 +660,6 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
     internal var isSearching: Bool = false
     internal var isSearchingInProgress: Bool = false
     
-    internal let refreshControl = UIRefreshControl()
-    
     // DECLARE
     internal var layoutKey = ""
     internal var titleCurrentFolder = ""
@@ -674,11 +675,11 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
         self.navigationController?.navigationBar.prefersLargeTitles = true
         
         if enableSearchBar {
-            let search = UISearchController(searchResultsController: nil)
-            search.searchResultsUpdater = self
-            self.navigationItem.searchController = search
-            search.delegate = self
-            search.searchBar.delegate = self
+            searchController = UISearchController(searchResultsController: nil)
+            searchController!.searchResultsUpdater = self
+            self.navigationItem.searchController = searchController
+            searchController!.delegate = self
+            searchController!.searchBar.delegate = self
         }
         
         // Cell
@@ -944,21 +945,19 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
     func updateSearchResults(for searchController: UISearchController) {
 
         timerInputSearch?.invalidate()
-        timerInputSearch = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(searchDataSourceNetwork), userInfo: nil, repeats: false)
+        timerInputSearch = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(reloadDataSourceNetwork), userInfo: nil, repeats: false)
         literalSearch = searchController.searchBar.text
     }
     
     func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
         
         dataSource = NCDataSource.init()
-        isSearching = true
         collectionView.reloadData()
     }
     
     func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
         
         literalSearch = ""
-        isSearching = false
         reloadDataSource()
     }
     
@@ -1053,8 +1052,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
     // MARK: - NC API & Algorithm
     
     @objc func reloadDataSource() { }
-    @objc func reloadDataSourceNetwork() {}
-    @objc func searchDataSourceNetwork() { }
+    @objc func reloadDataSourceNetwork() { }
 }
 
 // MARK: - 3D Touch peek and pop