ArtistModel.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 MediaCollectionViewCell.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: Artists can not be deleted, they disappear when their last title got deleted")
  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. // MARK: - MediaLibraryObserver
  46. extension ArtistModel: MediaLibraryObserver {
  47. func medialibrary(_ medialibrary: MediaLibraryService, didAddArtists artists: [VLCMLArtist]) {
  48. artists.forEach({ append($0) })
  49. updateView?()
  50. }
  51. }
  52. extension VLCMLArtist: MediaCollectionModel {
  53. func sortModel() -> SortModel? {
  54. return SortModel([.alpha])
  55. }
  56. func files() -> [VLCMLMedia]? {
  57. return tracks()
  58. }
  59. func title() -> String? {
  60. return name
  61. }
  62. }
  63. extension VLCMLArtist {
  64. func numberOfTracksString() -> String {
  65. let tracksString = tracks()?.count == 1 ? NSLocalizedString("TRACK", comment: "") : NSLocalizedString("TRACKS", comment: "")
  66. return String(format: tracksString, tracks()?.count ?? 0)
  67. }
  68. }