GenreModel.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*****************************************************************************
  2. * GenreModel.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 GenreModel: MLBaseModel {
  12. typealias MLType = VLCMLGenre
  13. var sortModel = SortModel([.alpha])
  14. var updateView: (() -> Void)?
  15. var files = [VLCMLGenre]()
  16. var cellType: BaseCollectionViewCell.Type { return MediaCollectionViewCell.self }
  17. var medialibrary: MediaLibraryService
  18. var indicatorName: String = NSLocalizedString("GENRES", comment: "")
  19. required init(medialibrary: MediaLibraryService) {
  20. self.medialibrary = medialibrary
  21. medialibrary.addObserver(self)
  22. files = medialibrary.genres()
  23. }
  24. func append(_ item: VLCMLGenre) {
  25. files.append(item)
  26. }
  27. func delete(_ items: [VLCMLObject]) {
  28. preconditionFailure("GenreModel: Genres can not be deleted, they disappear when their last title got deleted")
  29. }
  30. }
  31. // MARK: - Sort
  32. extension GenreModel {
  33. func sort(by criteria: VLCMLSortingCriteria) {
  34. files = medialibrary.genres(sortingCriteria: criteria)
  35. sortModel.currentSort = criteria
  36. updateView?()
  37. }
  38. }
  39. // MARK: - MediaLibraryObserver
  40. extension GenreModel: MediaLibraryObserver {
  41. func medialibrary(_ medialibrary: MediaLibraryService, didAddGenres genres: [VLCMLGenre]) {
  42. genres.forEach({ append($0) })
  43. updateView?()
  44. }
  45. }
  46. // MARK: - Edit
  47. extension GenreModel: EditableMLModel {
  48. func editCellType() -> BaseCollectionViewCell.Type {
  49. return MediaEditCell.self
  50. }
  51. }
  52. // MARK: - Helpers
  53. extension VLCMLGenre {
  54. @objc func numberOfTracksString() -> String {
  55. let numberOftracks = numberOfTracks()
  56. if numberOftracks != 1 {
  57. return String(format: NSLocalizedString("TRACKS", comment: ""), numberOftracks)
  58. }
  59. return String(format: NSLocalizedString("TRACK", comment: ""), numberOftracks)
  60. }
  61. }
  62. extension VLCMLGenre: MediaCollectionModel {
  63. func sortModel() -> SortModel? {
  64. return SortModel([.alpha])
  65. }
  66. func files() -> [VLCMLMedia]? {
  67. return tracks()
  68. }
  69. func title() -> String? {
  70. return name
  71. }
  72. }