LibrarySearchDataSource.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, UICollectionViewDataSource {
  13. var searchData = [VLCMLObject]()
  14. var model: MediaLibraryBaseModel
  15. init(model: MediaLibraryBaseModel) {
  16. self.model = model
  17. super.init()
  18. }
  19. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  20. return searchData.count
  21. }
  22. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  23. guard let mediaCell = collectionView.dequeueReusableCell(withReuseIdentifier:model.cellType.defaultReuseIdentifier, for: indexPath) as? BaseCollectionViewCell else {
  24. assertionFailure("you forgot to register the cell or the cell is not a subclass of BaseCollectionViewCell")
  25. return UICollectionViewCell()
  26. }
  27. let mediaObject = searchData[indexPath.row]
  28. if let media = mediaObject as? VLCMLMedia {
  29. assert(media.mainFile() != nil, "The mainfile is nil")
  30. mediaCell.media = media.mainFile() != nil ? media : nil
  31. } else {
  32. mediaCell.media = mediaObject
  33. }
  34. return mediaCell
  35. }
  36. func objectAtIndex(index: Int) -> VLCMLObject? {
  37. return index < searchData.count ? searchData[index] : nil
  38. }
  39. func shouldReloadFor(searchString: String) {
  40. guard searchString != "" else {
  41. searchData = model.anyfiles
  42. return
  43. }
  44. searchData.removeAll()
  45. let lowercaseSearchString = searchString.lowercased()
  46. model.anyfiles.forEach {
  47. if let media = $0 as? VLCMLMedia {
  48. if media.contains(lowercaseSearchString) { searchData.append($0) }
  49. } else if let album = $0 as? VLCMLAlbum {
  50. if album.contains(lowercaseSearchString) { searchData.append($0) }
  51. } else if let genre = $0 as? VLCMLGenre {
  52. if genre.contains(lowercaseSearchString) { searchData.append($0) }
  53. } else if let playlist = $0 as? VLCMLPlaylist {
  54. if playlist.contains(lowercaseSearchString) { searchData.append($0) }
  55. } else if let artist = $0 as? VLCMLArtist {
  56. if artist.contains(lowercaseSearchString) { searchData.append($0) }
  57. } else if let albumtrack = $0 as? VLCMLAlbumTrack {
  58. if albumtrack.contains(lowercaseSearchString) { searchData.append($0) }
  59. } else {
  60. assertionFailure("unhandled type")
  61. }
  62. }
  63. }
  64. }
  65. extension VLCMLObject {
  66. func contains(_ searchString: String) -> Bool {
  67. return false
  68. }
  69. }
  70. extension VLCMLMedia {
  71. func contains(_ searchString: String) -> Bool {
  72. return title.lowercased().contains(searchString)
  73. }
  74. }
  75. extension VLCMLAlbum {
  76. func contains(_ searchString: String) -> Bool {
  77. var matches = false
  78. matches = matches || title.lowercased().contains(searchString)
  79. matches = matches || String(releaseYear()).lowercased().contains(searchString)
  80. matches = matches || shortSummary.lowercased().contains(searchString)
  81. matches = matches || albumArtist?.contains(searchString) ?? false
  82. matches = matches || tracks?.filter({ $0.contains(searchString)}).isEmpty == false
  83. return matches
  84. }
  85. }
  86. extension VLCMLGenre {
  87. func contains(_ searchString: String) -> Bool {
  88. return name.lowercased().contains(searchString)
  89. }
  90. }
  91. extension VLCMLPlaylist {
  92. func contains(_ searchString: String) -> Bool {
  93. return name.lowercased().contains(searchString)
  94. }
  95. }
  96. extension VLCMLArtist {
  97. func contains(_ searchString: String) -> Bool {
  98. return name.lowercased().contains(searchString)
  99. }
  100. }
  101. extension VLCMLAlbumTrack {
  102. func contains(_ searchString: String) -> Bool {
  103. var matches = false
  104. matches = matches || artist?.contains(searchString) ?? false
  105. matches = matches || genre?.contains(searchString) ?? false
  106. matches = matches || album?.contains(searchString) ?? false
  107. return matches
  108. }
  109. }