AlbumModel.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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: true,
  14. duration: true,
  15. releaseDate: true,
  16. trackNumber: true)
  17. var updateView: (() -> Void)?
  18. var files = [VLCMLAlbum]()
  19. var cellType: BaseCollectionViewCell.Type { return AudioCollectionViewCell.self }
  20. var medialibrary: MediaLibraryService
  21. var indicatorName: String = NSLocalizedString("ALBUMS", comment: "")
  22. required init(medialibrary: MediaLibraryService) {
  23. self.medialibrary = medialibrary
  24. medialibrary.addObserver(self)
  25. files = medialibrary.albums()
  26. }
  27. func append(_ item: VLCMLAlbum) {
  28. files.append(item)
  29. }
  30. func delete(_ items: [VLCMLObject]) {
  31. preconditionFailure("AlbumModel: Cannot delete album")
  32. }
  33. }
  34. // MARK: - Sort
  35. extension AlbumModel {
  36. func sort(by criteria: VLCMLSortingCriteria) {
  37. files = medialibrary.albums(sortingCriteria: criteria)
  38. sortModel.currentSort = criteria
  39. updateView?()
  40. }
  41. }
  42. extension AlbumModel: MediaLibraryObserver {
  43. func medialibrary(_ medialibrary: MediaLibraryService, didAddAlbums albums: [VLCMLAlbum]) {
  44. albums.forEach({ append($0) })
  45. updateView?()
  46. }
  47. }