AlbumModel.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*****************************************************************************
  2. * AlbumModel.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 AlbumModel: MLBaseModel {
  12. typealias MLType = VLCMLAlbum
  13. var sortModel = SortModel([.alpha, .duration, .releaseDate, .trackNumber])
  14. var updateView: (() -> Void)?
  15. var files = [VLCMLAlbum]()
  16. var cellType: BaseCollectionViewCell.Type { return MediaCollectionViewCell.self }
  17. var medialibrary: MediaLibraryService
  18. var indicatorName: String = NSLocalizedString("ALBUMS", comment: "")
  19. required init(medialibrary: MediaLibraryService) {
  20. self.medialibrary = medialibrary
  21. medialibrary.addObserver(self)
  22. files = medialibrary.albums()
  23. }
  24. func append(_ item: VLCMLAlbum) {
  25. files.append(item)
  26. }
  27. func delete(_ items: [VLCMLObject]) {
  28. preconditionFailure("AlbumModel: Albums 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("AlbumModel: 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 VLCMLAlbum
  40. guard let tracks = files[index.row].tracks(with: .default, desc: false) else {
  41. assertionFailure("AlbumModel: createPlaylist: Fail to retreive tracks.")
  42. return
  43. }
  44. tracks.forEach() {
  45. playlist.appendMedia(withIdentifier: $0.identifier())
  46. }
  47. }
  48. }
  49. }
  50. // MARK: - Sort
  51. extension AlbumModel {
  52. func sort(by criteria: VLCMLSortingCriteria) {
  53. files = medialibrary.albums(sortingCriteria: criteria)
  54. sortModel.currentSort = criteria
  55. updateView?()
  56. }
  57. }
  58. // MARK: - Edit
  59. extension AlbumModel: EditableMLModel {
  60. func editCellType() -> BaseCollectionViewCell.Type {
  61. return MediaEditCell.self
  62. }
  63. }
  64. // MARK: - MediaLibraryObserver
  65. extension AlbumModel: MediaLibraryObserver {
  66. func medialibrary(_ medialibrary: MediaLibraryService, didAddAlbums albums: [VLCMLAlbum]) {
  67. albums.forEach({ append($0) })
  68. updateView?()
  69. }
  70. }
  71. extension VLCMLAlbum: MediaCollectionModel {
  72. func sortModel() -> SortModel? {
  73. return nil
  74. }
  75. func files() -> [VLCMLMedia]? {
  76. return tracks
  77. }
  78. }