ArtistModel.swift 3.0 KB

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