Selaa lähdekoodia

Search: Replace Searchcontroller with searchBar inside CategoryView

Scroll it in and out and set the contentinset accordingly,
Don't set the LibrarySearchDataSource as datasource to remove duplication for cellcode
Carola Nitz 6 vuotta sitten
vanhempi
commit
6bf4918a5c

+ 2 - 20
Sources/LibrarySearchDataSource.swift

@@ -10,7 +10,7 @@
  * Refer to the COPYING file of the official project for license.
  *****************************************************************************/
 
-class LibrarySearchDataSource: NSObject, UICollectionViewDataSource {
+class LibrarySearchDataSource: NSObject {
 
     var searchData = [VLCMLObject]()
     var model: MediaLibraryBaseModel
@@ -18,25 +18,7 @@ class LibrarySearchDataSource: NSObject, UICollectionViewDataSource {
     init(model: MediaLibraryBaseModel) {
         self.model = model
         super.init()
-    }
-
-    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
-        return searchData.count
-    }
-
-    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-        guard let mediaCell = collectionView.dequeueReusableCell(withReuseIdentifier:model.cellType.defaultReuseIdentifier, for: indexPath) as? BaseCollectionViewCell else {
-            assertionFailure("you forgot to register the cell or the cell is not a subclass of BaseCollectionViewCell")
-            return UICollectionViewCell()
-        }
-        let mediaObject = searchData[indexPath.row]
-        if let media = mediaObject as? VLCMLMedia {
-            assert(media.mainFile() != nil, "The mainfile is nil")
-            mediaCell.media = media.mainFile() != nil ? media : nil
-        } else {
-            mediaCell.media = mediaObject
-        }
-        return mediaCell
+        shouldReloadFor(searchString: "")
     }
 
     func objectAtIndex(index: Int) -> VLCMLObject? {

+ 40 - 29
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -17,13 +17,15 @@ protocol MediaCategoryViewControllerDelegate: NSObjectProtocol {
     func needsToUpdateNavigationbarIfNeeded(_ viewController: VLCMediaCategoryViewController)
 }
 
-class VLCMediaCategoryViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchResultsUpdating, UISearchControllerDelegate, IndicatorInfoProvider {
+class VLCMediaCategoryViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, IndicatorInfoProvider {
 
     var model: MediaLibraryBaseModel
-
+    private var searchBar = UISearchBar(frame: .zero)
+    private var searchbarConstraint: NSLayoutConstraint?
     private var services: Services
-    private var searchController: UISearchController?
     private let searchDataSource: LibrarySearchDataSource
+    private var isSearching = false
+    private let searchBarSize: CGFloat = 50.0
     private var rendererButton: UIButton
     private lazy var editController: VLCEditController = {
         let editController = VLCEditController(mediaLibraryService:services.medialibraryService, model: model)
@@ -82,7 +84,9 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
         self.model = model
         self.rendererButton = services.rendererDiscovererManager.setupRendererButton()
         self.searchDataSource = LibrarySearchDataSource(model: model)
+
         super.init(collectionViewLayout: UICollectionViewFlowLayout())
+        setupHeader()
         if let collection = model as? CollectionModel {
             title = collection.mediaCollection.title()
         }
@@ -90,6 +94,19 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
         navigationItem.rightBarButtonItems = [editButtonItem, UIBarButtonItem(customView: rendererButton)]
     }
 
+    func setupHeader() {
+
+        searchBar.translatesAutoresizingMaskIntoConstraints = false
+        view.addSubview(searchBar)
+        searchbarConstraint = searchBar.topAnchor.constraint(equalTo: view.topAnchor, constant: -searchBarSize)
+        NSLayoutConstraint.activate([
+            searchbarConstraint!,
+            searchBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
+            searchBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
+            searchBar.heightAnchor.constraint(equalToConstant: searchBarSize)
+            ])
+    }
+
     override var preferredStatusBarStyle: UIStatusBarStyle {
         return PresentationTheme.current.colors.statusBarStyle
     }
@@ -167,12 +184,6 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
             collectionView?.setContentOffset(.zero, animated: false)
         }
         collectionView?.backgroundView = isEmpty ? emptyView : nil
-        
-        if #available(iOS 11.0, *) {
-            navigationItem.searchController = isEmpty ? nil : searchController
-        } else {
-            navigationItem.titleView = isEmpty ? nil : searchController?.searchBar
-        }
     }
 
     // MARK: Renderer
@@ -186,6 +197,16 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
 
     // MARK: - Edit
 
+    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
+        searchbarConstraint?.constant = -min(scrollView.contentOffset.y, searchBarSize) - searchBarSize
+        if scrollView.contentOffset.y < -searchBarSize && scrollView.contentInset.top != searchBarSize {
+            collectionView.contentInset = UIEdgeInsets(top: searchBarSize, left: 0, bottom: 0, right: 0)
+        }
+        if scrollView.contentOffset.y >= 0 && scrollView.contentInset.top != 0 {
+            collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
+        }
+    }
+
     override func setEditing(_ editing: Bool, animated: Bool) {
         super.setEditing(editing, animated: animated)
         // might have an issue if the old datasource was search
@@ -216,29 +237,19 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
 
     // MARK: - Search
 
-    func updateSearchResults(for searchController: UISearchController) {
-        guard let searchText = searchController.searchBar.text else {
-            return
-        }
+    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
         searchDataSource.shouldReloadFor(searchString: searchText)
+        isSearching = searchText != ""
         collectionView?.reloadData()
     }
 
-    func didPresentSearchController(_ searchController: UISearchController) {
-        collectionView?.dataSource = searchDataSource
-    }
-
-    func didDismissSearchController(_ searchController: UISearchController) {
-        collectionView?.dataSource = self
-    }
-
     func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
         return IndicatorInfo(title:model.indicatorName)
     }
 
     // MARK: - UICollectionViewDataSource
     override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
-        return model.anyfiles.count
+        return isSearching ? searchDataSource.searchData.count : model.anyfiles.count
     }
 
     override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
@@ -246,7 +257,7 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
             assertionFailure("you forgot to register the cell or the cell is not a subclass of BaseCollectionViewCell")
             return UICollectionViewCell()
         }
-        let mediaObject = model.anyfiles[indexPath.row]
+        let mediaObject = isSearching ? searchDataSource.objectAtIndex(index: indexPath.row) : model.anyfiles[indexPath.row]
         if let media = mediaObject as? VLCMLMedia {
             assert(media.mainFile() != nil, "The mainfile is nil")
             mediaCell.media = media.mainFile() != nil ? media : nil
@@ -401,7 +412,6 @@ private extension VLCMediaCategoryViewController {
         }
         collectionView?.backgroundColor = PresentationTheme.current.colors.background
         collectionView?.alwaysBounceVertical = true
-
         longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
         collectionView?.addGestureRecognizer(longPressGesture)
         if #available(iOS 11.0, *) {
@@ -412,11 +422,12 @@ private extension VLCMediaCategoryViewController {
     }
 
     func setupSearchController() {
-        searchController = UISearchController(searchResultsController: nil)
-        searchController?.searchResultsUpdater = self
-        searchController?.dimsBackgroundDuringPresentation = false
-        searchController?.delegate = self
-        if let textfield = searchController?.searchBar.value(forKey: "searchField") as? UITextField {
+        searchBar.delegate = self
+        searchBar.searchBarStyle = .minimal
+        if #available(iOS 11.0, *) {
+            navigationItem.largeTitleDisplayMode = .never
+        }
+        if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
             if let backgroundview = textfield.subviews.first {
                 backgroundview.backgroundColor = UIColor.white
                 backgroundview.layer.cornerRadius = 10

+ 1 - 0
Sources/MediaViewControllers/MediaViewController.swift

@@ -16,6 +16,7 @@ class VLCMediaViewController: VLCPagingViewController<VLCLabelCell>, MediaCatego
 
     var services: Services
     private var rendererButton: UIButton
+    
     private var sortButton: UIBarButtonItem?
     private var rigthBarButtons: [UIBarButtonItem]?
 

+ 1 - 1
Sources/PagerStripViewController.swift

@@ -96,6 +96,7 @@ class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
         }
         isViewAppearing = false
         children.forEach { $0.endAppearanceTransition() }
+        //updateSearchbarController()
     }
 
     override func viewWillDisappear(_ animated: Bool) {
@@ -122,7 +123,6 @@ class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
             preCurrentIndex = index
             return
         }
-
         if animated && abs(currentIndex - index) > 1 {
             var tmpViewControllers = viewControllers
             let currentChildVC = viewControllers[currentIndex]