ArtistModel.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
  31. let playlist = medialibrary.createPlaylist(with: name)
  32. guard let fileIndexes = fileIndexes else {
  33. return
  34. }
  35. for index in fileIndexes where index.row < files.count {
  36. // Get all tracks from a VLCMLArtist
  37. guard let tracks = files[index.row].tracks(with: .default, desc: false) else {
  38. assertionFailure("ArtistModel: createPlaylist: Fail to retreive tracks.")
  39. return
  40. }
  41. tracks.forEach() {
  42. playlist.appendMedia(withIdentifier: $0.identifier())
  43. }
  44. }
  45. }
  46. }
  47. // MARK: - Edit
  48. extension ArtistModel: EditableMLModel {
  49. func editCellType() -> BaseCollectionViewCell.Type {
  50. return MediaEditCell.self
  51. }
  52. }
  53. // MARK: - Sort
  54. extension ArtistModel {
  55. func sort(by criteria: VLCMLSortingCriteria) {
  56. files = medialibrary.artists(sortingCriteria: criteria)
  57. sortModel.currentSort = criteria
  58. updateView?()
  59. }
  60. }
  61. // MARK: - MediaLibraryObserver
  62. extension ArtistModel: MediaLibraryObserver {
  63. func medialibrary(_ medialibrary: MediaLibraryService, didAddArtists artists: [VLCMLArtist]) {
  64. artists.forEach({ append($0) })
  65. updateView?()
  66. }
  67. }
  68. extension VLCMLArtist: MediaCollectionModel {
  69. func sortModel() -> SortModel? {
  70. return SortModel([.alpha])
  71. }
  72. func files() -> [VLCMLMedia] {
  73. return tracks()
  74. }
  75. }
  76. extension VLCMLArtist {
  77. func numberOfTracksString() -> String {
  78. let tracksString = tracks()?.count == 1 ? NSLocalizedString("TRACK", comment: "") : NSLocalizedString("TRACKS", comment: "")
  79. return String(format: tracksString, tracks()?.count ?? 0)
  80. }
  81. }