PlaylistModel.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*****************************************************************************
  2. * PlaylistModel.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 PlaylistModel: MLBaseModel {
  12. typealias MLType = VLCMLPlaylist
  13. var sortModel = SortModel([.alpha, .duration])
  14. var updateView: (() -> Void)?
  15. var files = [VLCMLPlaylist]()
  16. var cellType: BaseCollectionViewCell.Type { return MovieCollectionViewCell.self }
  17. var medialibrary: MediaLibraryService
  18. var indicatorName: String = NSLocalizedString("PLAYLISTS", comment: "")
  19. required init(medialibrary: MediaLibraryService) {
  20. self.medialibrary = medialibrary
  21. medialibrary.addObserver(self)
  22. files = medialibrary.playlists()
  23. }
  24. func append(_ item: VLCMLPlaylist) {
  25. for file in files {
  26. if file.identifier() == item.identifier() {
  27. return
  28. }
  29. }
  30. files.append(item)
  31. }
  32. func delete(_ items: [VLCMLObject]) {
  33. for playlist in items where playlist is VLCMLPlaylist {
  34. if !(medialibrary.deletePlaylist(with: playlist.identifier())) {
  35. assertionFailure("PlaylistModel: Failed to delete playlist: \(playlist.identifier())")
  36. }
  37. }
  38. }
  39. // Creates a VLCMLPlaylist appending it and updates linked view
  40. func create(name: String) {
  41. guard let playlist = medialibrary.createPlaylist(with: name) else {
  42. assertionFailure("PlaylistModel: create: Failed to create a playlist.")
  43. return
  44. }
  45. append(playlist)
  46. updateView?()
  47. }
  48. func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
  49. guard let playlist = medialibrary.createPlaylist(with: name) else {
  50. assertionFailure("PlaylistModel: createPlaylist: Failed to create a playlist.")
  51. return
  52. }
  53. guard let fileIndexes = fileIndexes else {
  54. return
  55. }
  56. for index in fileIndexes where index.row < files.count {
  57. // Get all tracks from a VLCMLPlaylist
  58. guard let media = files[index.row].media else {
  59. assertionFailure("PlaylistModel: createPlaylist: Failed to retreive media.")
  60. return
  61. }
  62. media.forEach() {
  63. playlist.appendMedia(withIdentifier: $0.identifier())
  64. }
  65. }
  66. }
  67. }
  68. // MARK: - Sort
  69. extension PlaylistModel {
  70. func sort(by criteria: VLCMLSortingCriteria) {
  71. files = medialibrary.playlists(sortingCriteria: criteria)
  72. sortModel.currentSort = criteria
  73. updateView?()
  74. }
  75. }
  76. // MARK: - Edit
  77. extension PlaylistModel: EditableMLModel {
  78. func editCellType() -> BaseCollectionViewCell.Type {
  79. return MediaEditCell.self
  80. }
  81. }
  82. // MARK: - MediaLibraryObserver
  83. extension PlaylistModel: MediaLibraryObserver {
  84. func medialibrary(_ medialibrary: MediaLibraryService, didAddPlaylists playlists: [VLCMLPlaylist]) {
  85. playlists.forEach({ append($0) })
  86. updateView?()
  87. }
  88. func medialibrary(_ medialibrary: MediaLibraryService, didDeletePlaylistsWithIds playlistsIds: [NSNumber]) {
  89. files = files.filter() {
  90. for id in playlistsIds where $0.identifier() == id.int64Value {
  91. return false
  92. }
  93. return true
  94. }
  95. updateView?()
  96. }
  97. }
  98. // MARK: - Helpers
  99. extension VLCMLPlaylist {
  100. func numberOfTracksString() -> String {
  101. let mediaCount = media?.count ?? 0
  102. let tracksString = mediaCount > 1 ? NSLocalizedString("TRACKS", comment: "") : NSLocalizedString("TRACK", comment: "")
  103. return String(format: tracksString, mediaCount)
  104. }
  105. }
  106. extension VLCMLPlaylist: MediaCollectionModel {
  107. func sortModel() -> SortModel? {
  108. return nil
  109. }
  110. func files() -> [VLCMLMedia]? {
  111. return media
  112. }
  113. }