AlbumModel.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 AudioCollectionViewCell.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: Cannot delete album")
  29. }
  30. }
  31. // MARK: - Sort
  32. extension AlbumModel {
  33. func sort(by criteria: VLCMLSortingCriteria) {
  34. files = medialibrary.albums(sortingCriteria: criteria)
  35. sortModel.currentSort = criteria
  36. updateView?()
  37. }
  38. }
  39. // MARK: - Edit
  40. extension AlbumModel: EditableMLModel {
  41. func editCellType() -> BaseCollectionViewCell.Type {
  42. return MediaEditCell.self
  43. }
  44. }
  45. extension AlbumModel: MediaLibraryObserver {
  46. func medialibrary(_ medialibrary: MediaLibraryService, didAddAlbums albums: [VLCMLAlbum]) {
  47. albums.forEach({ append($0) })
  48. updateView?()
  49. }
  50. }
  51. extension VLCMLAlbum: MediaCollectionModel {
  52. func files() -> [VLCMLMedia] {
  53. return tracks
  54. }
  55. }