PlaylistModel.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. append(medialibrary.createPlaylist(with: name))
  42. updateView?()
  43. }
  44. func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
  45. let playlist = medialibrary.createPlaylist(with: name)
  46. guard let fileIndexes = fileIndexes else {
  47. return
  48. }
  49. for index in fileIndexes where index.row < files.count {
  50. // Get all tracks from a VLCMLPlaylist
  51. guard let media = files[index.row].media else {
  52. assertionFailure("PlaylistModel: createPlaylist: Failed to retreive media.")
  53. return
  54. }
  55. media.forEach() {
  56. playlist.appendMedia(withIdentifier: $0.identifier())
  57. }
  58. }
  59. }
  60. }
  61. // MARK: - Sort
  62. extension PlaylistModel {
  63. func sort(by criteria: VLCMLSortingCriteria) {
  64. files = medialibrary.playlists(sortingCriteria: criteria)
  65. sortModel.currentSort = criteria
  66. updateView?()
  67. }
  68. }
  69. // MARK: - Edit
  70. extension PlaylistModel: EditableMLModel {
  71. func editCellType() -> BaseCollectionViewCell.Type {
  72. return MediaEditCell.self
  73. }
  74. }
  75. // MARK: - MediaLibraryObserver
  76. extension PlaylistModel: MediaLibraryObserver {
  77. func medialibrary(_ medialibrary: MediaLibraryService, didAddPlaylists playlists: [VLCMLPlaylist]) {
  78. playlists.forEach({ append($0) })
  79. updateView?()
  80. }
  81. func medialibrary(_ medialibrary: MediaLibraryService, didDeletePlaylistsWithIds playlistsIds: [NSNumber]) {
  82. files = files.filter() {
  83. for id in playlistsIds where $0.identifier() == id.int64Value {
  84. return false
  85. }
  86. return true
  87. }
  88. updateView?()
  89. }
  90. }
  91. // MARK: - Helpers
  92. extension VLCMLPlaylist {
  93. func numberOfTracksString() -> String {
  94. let tracksString = media.count == 1 ? NSLocalizedString("TRACK", comment: "") : NSLocalizedString("TRACKS", comment: "")
  95. return String(format: tracksString, media.count)
  96. }
  97. }
  98. extension VLCMLPlaylist: MediaCollectionModel {
  99. func sortModel() -> SortModel? {
  100. return nil
  101. }
  102. func files() -> [VLCMLMedia] {
  103. return media
  104. }
  105. }