ArtistModel.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*****************************************************************************
  2. * ArtistModel.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 ArtistModel: MLBaseModel {
  12. typealias MLType = VLCMLArtist
  13. var sortModel = SortModel([.alpha])
  14. var updateView: (() -> Void)?
  15. var files = [VLCMLArtist]()
  16. var cellType: BaseCollectionViewCell.Type { return ArtistCollectionViewCell.self }
  17. var medialibrary: MediaLibraryService
  18. var indicatorName: String = NSLocalizedString("ARTISTS", comment: "")
  19. required init(medialibrary: MediaLibraryService) {
  20. self.medialibrary = medialibrary
  21. medialibrary.addObserver(self)
  22. files = medialibrary.artists()
  23. }
  24. func append(_ item: VLCMLArtist) {
  25. files.append(item)
  26. }
  27. func delete(_ items: [VLCMLObject]) {
  28. preconditionFailure("ArtistModel: Cannot delete artist")
  29. }
  30. }
  31. // MARK: - Edit
  32. extension ArtistModel: EditableMLModel {
  33. func editCellType() -> BaseCollectionViewCell.Type {
  34. return MediaEditCell.self
  35. }
  36. }
  37. // MARK: - Sort
  38. extension ArtistModel {
  39. func sort(by criteria: VLCMLSortingCriteria) {
  40. files = medialibrary.artists(sortingCriteria: criteria)
  41. sortModel.currentSort = criteria
  42. updateView?()
  43. }
  44. }
  45. extension ArtistModel: MediaLibraryObserver {
  46. func medialibrary(_ medialibrary: MediaLibraryService, didAddArtists artists: [VLCMLArtist]) {
  47. artists.forEach({ append($0) })
  48. updateView?()
  49. }
  50. }