AlbumModel.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*****************************************************************************
  2. * AlbumModel.swift
  3. *
  4. * Copyright © 2018 VLC authors and VideoLAN
  5. * Copyright © 2018 Videolabs
  6. *
  7. * Authors: Soomin Lee <bubu@mikan.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. class AlbumModel: MLBaseModel {
  12. typealias MLType = VLCMLAlbum
  13. var sortModel = SortModel([.alpha, .duration, .releaseDate, .trackNumber])
  14. var updateView: (() -> Void)?
  15. var files = [VLCMLAlbum]()
  16. var cellType: BaseCollectionViewCell.Type { return MediaCollectionViewCell.self }
  17. var medialibrary: MediaLibraryService
  18. var indicatorName: String = NSLocalizedString("ALBUMS", comment: "")
  19. required init(medialibrary: MediaLibraryService) {
  20. self.medialibrary = medialibrary
  21. medialibrary.addObserver(self)
  22. files = medialibrary.albums()
  23. }
  24. func append(_ item: VLCMLAlbum) {
  25. files.append(item)
  26. }
  27. func delete(_ items: [VLCMLObject]) {
  28. preconditionFailure("AlbumModel: Albums can not be deleted, they disappear when their last title got deleted")
  29. }
  30. }
  31. // MARK: - Sort
  32. extension AlbumModel {
  33. func sort(by criteria: VLCMLSortingCriteria, desc: Bool) {
  34. files = medialibrary.albums(sortingCriteria: criteria, desc: desc)
  35. sortModel.currentSort = criteria
  36. sortModel.desc = desc
  37. updateView?()
  38. }
  39. }
  40. // MARK: - Edit
  41. extension AlbumModel: EditableMLModel {
  42. func editCellType() -> BaseCollectionViewCell.Type {
  43. return MediaEditCell.self
  44. }
  45. }
  46. // MARK: - Search
  47. extension VLCMLAlbum: SearchableMLModel {
  48. func contains(_ searchString: String) -> Bool {
  49. var matches = false
  50. matches = matches || title.lowercased().contains(searchString)
  51. matches = matches || String(releaseYear()).lowercased().contains(searchString)
  52. matches = matches || shortSummary.lowercased().contains(searchString)
  53. matches = matches || albumArtist?.contains(searchString) ?? false
  54. matches = matches || tracks?.filter({ $0.contains(searchString)}).isEmpty == false
  55. return matches
  56. }
  57. }
  58. extension VLCMLAlbumTrack: SearchableMLModel {
  59. func contains(_ searchString: String) -> Bool {
  60. var matches = false
  61. matches = matches || artist?.contains(searchString) ?? false
  62. matches = matches || genre?.contains(searchString) ?? false
  63. matches = matches || album?.contains(searchString) ?? false
  64. return matches
  65. }
  66. }
  67. // MARK: - MediaLibraryObserver
  68. extension AlbumModel: MediaLibraryObserver {
  69. func medialibrary(_ medialibrary: MediaLibraryService, didAddAlbums albums: [VLCMLAlbum]) {
  70. albums.forEach({ append($0) })
  71. updateView?()
  72. }
  73. func medialibrary(_ medialibrary: MediaLibraryService, didDeleteAlbumsWithIds albumsIds: [NSNumber]) {
  74. files.removeAll {
  75. albumsIds.contains(NSNumber(value: $0.identifier()))
  76. }
  77. updateView?()
  78. }
  79. }
  80. extension VLCMLAlbum: MediaCollectionModel {
  81. func sortModel() -> SortModel? {
  82. return nil
  83. }
  84. func files() -> [VLCMLMedia]? {
  85. return tracks
  86. }
  87. func title() -> String? {
  88. return title
  89. }
  90. }
  91. extension VLCMLAlbum {
  92. func numberOfTracksString() -> String {
  93. let trackCount = numberOfTracks()
  94. let tracksString = trackCount > 1 ? NSLocalizedString("TRACKS", comment: "") : NSLocalizedString("TRACK", comment: "")
  95. return String(format: tracksString, trackCount)
  96. }
  97. @objc func thumbnail() -> UIImage? {
  98. var image = UIImage(contentsOfFile: artworkMrl.path)
  99. if image == nil {
  100. for track in files() ?? [] where track.isThumbnailGenerated() {
  101. image = UIImage(contentsOfFile: track.thumbnail.path)
  102. break
  103. }
  104. }
  105. if image == nil {
  106. let isDarktheme = PresentationTheme.current == PresentationTheme.darkTheme
  107. image = isDarktheme ? UIImage(named: "album-placeholder-dark") : UIImage(named: "album-placeholder-white")
  108. }
  109. return image
  110. }
  111. func albumName() -> String {
  112. return isUnknownAlbum() ? NSLocalizedString("UNKNOWN_ALBUM", comment: "") : title
  113. }
  114. func albumArtistName() -> String {
  115. guard let artist = albumArtist else {
  116. return NSLocalizedString("UNKNOWN_ARTIST", comment: "")
  117. }
  118. return artist.artistName()
  119. }
  120. }
  121. extension VLCMLAlbumTrack {
  122. func albumArtistName() -> String {
  123. guard let artist = artist else {
  124. return NSLocalizedString("UNKNOWN_ARTIST", comment: "")
  125. }
  126. return artist.artistName()
  127. }
  128. }