AlbumModel.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // MARK: - Edit
  43. extension AlbumModel: EditableMLModel {
  44. func editCellType() -> BaseCollectionViewCell.Type {
  45. return MediaEditCell.self
  46. }
  47. }
  48. extension AlbumModel: MediaLibraryObserver {
  49. func medialibrary(_ medialibrary: MediaLibraryService, didAddAlbums albums: [VLCMLAlbum]) {
  50. albums.forEach({ append($0) })
  51. updateView?()
  52. }
  53. }