LibrarySearchDataSource.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*****************************************************************************
  2. * LibrarySearchDataSource.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2019 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <caro # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. class LibrarySearchDataSource: NSObject {
  13. var searchData = [VLCMLObject]()
  14. var model: MediaLibraryBaseModel
  15. init(model: MediaLibraryBaseModel) {
  16. self.model = model
  17. super.init()
  18. shouldReloadFor(searchString: "")
  19. }
  20. func objectAtIndex(index: Int) -> VLCMLObject? {
  21. return index < searchData.count ? searchData[index] : nil
  22. }
  23. func shouldReloadFor(searchString: String) {
  24. guard searchString != "" else {
  25. searchData = model.anyfiles
  26. return
  27. }
  28. searchData.removeAll()
  29. let lowercaseSearchString = searchString.lowercased()
  30. model.anyfiles.forEach {
  31. guard let searchableFile = $0 as? SearchableMLModel else {
  32. assertionFailure("LibrarySearchDataSource: Unhandled type")
  33. return
  34. }
  35. if searchableFile.contains(lowercaseSearchString) {
  36. searchData.append($0)
  37. }
  38. }
  39. }
  40. }