PlaylistModel.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }
  49. // MARK: - Sort
  50. extension PlaylistModel {
  51. func sort(by criteria: VLCMLSortingCriteria, desc: Bool) {
  52. files = medialibrary.playlists(sortingCriteria: criteria, desc: desc)
  53. sortModel.currentSort = criteria
  54. sortModel.desc = desc
  55. updateView?()
  56. }
  57. }
  58. // MARK: - Edit
  59. extension PlaylistModel: EditableMLModel {
  60. func editCellType() -> BaseCollectionViewCell.Type {
  61. return MediaEditCell.self
  62. }
  63. }
  64. // MARK: - Search
  65. extension VLCMLPlaylist: SearchableMLModel {
  66. func contains(_ searchString: String) -> Bool {
  67. return name.lowercased().contains(searchString)
  68. }
  69. }
  70. // MARK: - MediaLibraryObserver
  71. extension PlaylistModel: MediaLibraryObserver {
  72. func medialibrary(_ medialibrary: MediaLibraryService, didAddPlaylists playlists: [VLCMLPlaylist]) {
  73. playlists.forEach({ append($0) })
  74. updateView?()
  75. }
  76. func medialibrary(_ medialibrary: MediaLibraryService, didDeletePlaylistsWithIds playlistsIds: [NSNumber]) {
  77. files = files.filter() {
  78. for id in playlistsIds where $0.identifier() == id.int64Value {
  79. return false
  80. }
  81. return true
  82. }
  83. updateView?()
  84. }
  85. }
  86. // MARK: - Helpers
  87. extension VLCMLPlaylist {
  88. func numberOfTracksString() -> String {
  89. let mediaCount = media?.count ?? 0
  90. let tracksString = mediaCount > 1 ? NSLocalizedString("TRACKS", comment: "") : NSLocalizedString("TRACK", comment: "")
  91. return String(format: tracksString, mediaCount)
  92. }
  93. @objc func thumbnail() -> UIImage? {
  94. var image = UIImage(contentsOfFile: artworkMrl())
  95. if image == nil {
  96. for track in files() ?? [] where track.isThumbnailGenerated() {
  97. image = UIImage(contentsOfFile: track.thumbnail.path)
  98. break
  99. }
  100. }
  101. if image == nil {
  102. let isDarktheme = PresentationTheme.current == PresentationTheme.darkTheme
  103. image = isDarktheme ? UIImage(named: "movie-placeholder-dark") : UIImage(named: "movie-placeholder-white")
  104. }
  105. return image
  106. }
  107. }
  108. extension VLCMLPlaylist: MediaCollectionModel {
  109. func sortModel() -> SortModel? {
  110. return nil
  111. }
  112. func files() -> [VLCMLMedia]? {
  113. return media
  114. }
  115. func title() -> String? {
  116. return name
  117. }
  118. }