PlaylistModel.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 case let playlist as VLCMLPlaylist in items {
  34. if !(medialibrary.deletePlaylist(with: playlist.identifier())) {
  35. assertionFailure("PlaylistModel: Failed to delete playlist: \(playlist.identifier())")
  36. }
  37. if playlist.isReadOnly {
  38. do {
  39. if let path = playlist.mrl?.path, !path.isEmpty {
  40. try FileManager.default.removeItem(atPath: path)
  41. }
  42. } catch let error as NSError {
  43. assertionFailure("PlaylistModel: Delete failed: \(error.localizedDescription)")
  44. }
  45. }
  46. }
  47. // Update directly the UI without waiting the delegate to avoid showing 'ghost' items
  48. filterFilesFromDeletion(of: items)
  49. updateView?()
  50. }
  51. // Creates a VLCMLPlaylist appending it and updates linked view
  52. func create(name: String) {
  53. guard let playlist = medialibrary.createPlaylist(with: name) else {
  54. assertionFailure("PlaylistModel: create: Failed to create a playlist.")
  55. return
  56. }
  57. append(playlist)
  58. updateView?()
  59. }
  60. }
  61. // MARK: - Sort
  62. extension PlaylistModel {
  63. func sort(by criteria: VLCMLSortingCriteria, desc: Bool) {
  64. files = medialibrary.playlists(sortingCriteria: criteria, desc: desc)
  65. sortModel.currentSort = criteria
  66. sortModel.desc = desc
  67. updateView?()
  68. }
  69. }
  70. // MARK: - Search
  71. extension VLCMLPlaylist: SearchableMLModel {
  72. func contains(_ searchString: String) -> Bool {
  73. return name.lowercased().contains(searchString)
  74. }
  75. }
  76. // MARK: - MediaLibraryObserver
  77. extension PlaylistModel: MediaLibraryObserver {
  78. func medialibrary(_ medialibrary: MediaLibraryService, didAddPlaylists playlists: [VLCMLPlaylist]) {
  79. playlists.forEach({ append($0) })
  80. updateView?()
  81. }
  82. func medialibrary(_ medialibrary: MediaLibraryService,
  83. didModifyPlaylistsWithIds playlistsIds: [NSNumber]) {
  84. var playlists = [VLCMLPlaylist]()
  85. playlistsIds.forEach() {
  86. guard let safePlaylist = medialibrary.medialib.playlist(withIdentifier: $0.int64Value)
  87. else {
  88. return
  89. }
  90. playlists.append(safePlaylist)
  91. }
  92. files = swapModels(with: playlists)
  93. updateView?()
  94. }
  95. func medialibrary(_ medialibrary: MediaLibraryService, didDeletePlaylistsWithIds playlistsIds: [NSNumber]) {
  96. files = files.filter() {
  97. for id in playlistsIds where $0.identifier() == id.int64Value {
  98. return false
  99. }
  100. return true
  101. }
  102. updateView?()
  103. }
  104. func medialibraryDidStartRescan() {
  105. files.removeAll()
  106. }
  107. }
  108. // MARK: - Helpers
  109. extension VLCMLPlaylist {
  110. func numberOfTracksString() -> String {
  111. let mediaCount = media?.count ?? 0
  112. let tracksString = mediaCount > 1 ? NSLocalizedString("TRACKS", comment: "") : NSLocalizedString("TRACK", comment: "")
  113. return String(format: tracksString, mediaCount)
  114. }
  115. func accessibilityText() -> String? {
  116. return name + " " + numberOfTracksString()
  117. }
  118. }
  119. extension VLCMLPlaylist: MediaCollectionModel {
  120. func sortModel() -> SortModel? {
  121. return nil
  122. }
  123. func files(with criteria: VLCMLSortingCriteria = .alpha,
  124. desc: Bool = false) -> [VLCMLMedia]? {
  125. return media
  126. }
  127. func title() -> String {
  128. return name
  129. }
  130. }