Commit 09191ac6 authored by Soomin Lee's avatar Soomin Lee

VLCMediaCategoryViewController: Search: Select correct item

Use internal state instead of using the first responder as a state.
Using the first responder can lead to unexpected result.
e.g.: When using a keyboard and pressing search, it removes the first
responder, therefore, using the wrong model.

(closes #531)
parent 900531d9
......@@ -23,6 +23,7 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
private var services: Services
var searchBar = UISearchBar(frame: .zero)
var isSearching: Bool = false
private var searchBarConstraint: NSLayoutConstraint?
private let searchDataSource: LibrarySearchDataSource
private let searchBarSize: CGFloat = 50.0
......@@ -190,7 +191,7 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
}
func updateUIForContent() {
let isEmpty = isEmptyCollectionView() && !searchBar.isFirstResponder
let isEmpty = isEmptyCollectionView() && !isSearching
if isEmpty {
collectionView?.setContentOffset(.zero, animated: false)
}
......@@ -253,7 +254,7 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
// MARK: - UICollectionViewDataSource
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return searchBar.isFirstResponder ? searchDataSource.searchData.count : model.anyfiles.count
return isSearching ? searchDataSource.searchData.count : model.anyfiles.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
......@@ -261,7 +262,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 = searchBar.isFirstResponder ? searchDataSource.objectAtIndex(index: indexPath.row) : 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
......@@ -273,10 +274,12 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
// MARK: - UICollectionViewDelegate
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
let modelContent = isSearching ? searchDataSource.objectAtIndex(index: indexPath.row) : model.anyfiles[indexPath.row]
if let media = modelContent as? VLCMLMedia {
play(media: media, at: indexPath)
createSpotlightItem(media: media)
} else if let mediaCollection = model.anyfiles[indexPath.row] as? MediaCollectionModel {
} else if let mediaCollection = modelContent as? MediaCollectionModel {
let collectionViewController = VLCCollectionCategoryViewController(services, mediaCollection: mediaCollection)
navigationController?.pushViewController(collectionViewController, animated: true)
}
......@@ -301,6 +304,7 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
extension VLCMediaCategoryViewController {
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
reloadData()
isSearching = true
searchBar.setShowsCancelButton(true, animated: true)
}
......@@ -310,6 +314,7 @@ extension VLCMediaCategoryViewController {
searchBar.text = ""
searchDataSource.shouldReloadFor(searchString: "")
searchBar.setShowsCancelButton(false, animated: true)
isSearching = false
reloadData()
}
......@@ -492,7 +497,7 @@ extension VLCMediaCategoryViewController {
if let model = model as? MediaCollectionModel {
tracks = model.files() ?? []
} else {
tracks = model.anyfiles as? [VLCMLMedia] ?? []
tracks = (isSearching ? searchDataSource.searchData : model.anyfiles) as? [VLCMLMedia] ?? []
}
playbackController.playMedia(at: indexPath.row, fromCollection: tracks)
}
......
......@@ -67,7 +67,8 @@ class VLCMediaViewController: VLCPagingViewController<VLCLabelCell>, MediaCatego
func updateButtonsFor(_ viewController: UIViewController) {
var showButtons = false
if let mediaCategoryViewController = viewController as? VLCMediaCategoryViewController,
!mediaCategoryViewController.isEmptyCollectionView() && !mediaCategoryViewController.searchBar.isFirstResponder {
!mediaCategoryViewController.isEmptyCollectionView()
&& !mediaCategoryViewController.isSearching {
showButtons = true
}
navigationItem.rightBarButtonItems = showButtons ? rigthBarButtons : nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment