LibrarySearchDataSource.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if let media = $0 as? VLCMLMedia {
  32. if media.contains(lowercaseSearchString) { searchData.append($0) }
  33. } else if let album = $0 as? VLCMLAlbum {
  34. if album.contains(lowercaseSearchString) { searchData.append($0) }
  35. } else if let genre = $0 as? VLCMLGenre {
  36. if genre.contains(lowercaseSearchString) { searchData.append($0) }
  37. } else if let playlist = $0 as? VLCMLPlaylist {
  38. if playlist.contains(lowercaseSearchString) { searchData.append($0) }
  39. } else if let artist = $0 as? VLCMLArtist {
  40. if artist.contains(lowercaseSearchString) { searchData.append($0) }
  41. } else if let albumtrack = $0 as? VLCMLAlbumTrack {
  42. if albumtrack.contains(lowercaseSearchString) { searchData.append($0) }
  43. } else {
  44. assertionFailure("unhandled type")
  45. }
  46. }
  47. }
  48. }
  49. extension VLCMLObject {
  50. func contains(_ searchString: String) -> Bool {
  51. return false
  52. }
  53. }
  54. extension VLCMLMedia {
  55. func contains(_ searchString: String) -> Bool {
  56. return title.lowercased().contains(searchString)
  57. }
  58. }
  59. extension VLCMLAlbum {
  60. func contains(_ searchString: String) -> Bool {
  61. var matches = false
  62. matches = matches || title.lowercased().contains(searchString)
  63. matches = matches || String(releaseYear()).lowercased().contains(searchString)
  64. matches = matches || shortSummary.lowercased().contains(searchString)
  65. matches = matches || albumArtist?.contains(searchString) ?? false
  66. matches = matches || tracks?.filter({ $0.contains(searchString)}).isEmpty == false
  67. return matches
  68. }
  69. }
  70. extension VLCMLGenre {
  71. func contains(_ searchString: String) -> Bool {
  72. return name.lowercased().contains(searchString)
  73. }
  74. }
  75. extension VLCMLPlaylist {
  76. func contains(_ searchString: String) -> Bool {
  77. return name.lowercased().contains(searchString)
  78. }
  79. }
  80. extension VLCMLArtist {
  81. func contains(_ searchString: String) -> Bool {
  82. return name.lowercased().contains(searchString)
  83. }
  84. }
  85. extension VLCMLAlbumTrack {
  86. func contains(_ searchString: String) -> Bool {
  87. var matches = false
  88. matches = matches || artist?.contains(searchString) ?? false
  89. matches = matches || genre?.contains(searchString) ?? false
  90. matches = matches || album?.contains(searchString) ?? false
  91. return matches
  92. }
  93. }