VideoGroupViewModel.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*****************************************************************************
  2. * VideoGroupViewModel.swift
  3. *
  4. * Copyright © 2019 VLC authors and VideoLAN
  5. *
  6. * Authors: Soomin Lee <bubu@mikan.io>
  7. *
  8. * Refer to the COPYING file of the official project for license.
  9. *****************************************************************************/
  10. // Fake VLCMLVideoGroup as a medialibrary entity with an id
  11. extension VLCMLVideoGroup: VLCMLObject {
  12. public func identifier() -> VLCMLIdentifier {
  13. return 42
  14. }
  15. }
  16. class VideoGroupViewModel: MLBaseModel {
  17. typealias MLType = VLCMLVideoGroup
  18. var sortModel = SortModel([.alpha, .nbVideo])
  19. var updateView: (() -> Void)?
  20. var files: [VLCMLVideoGroup]
  21. var cellType: BaseCollectionViewCell.Type { return MovieCollectionViewCell.self }
  22. var medialibrary: MediaLibraryService
  23. var indicatorName: String = NSLocalizedString("VIDEO_GROUPS", comment: "")
  24. var prefixLength: UInt32 = 0
  25. required init(medialibrary: MediaLibraryService) {
  26. self.medialibrary = medialibrary
  27. self.files = medialibrary.medialib.videoGroups() ?? []
  28. medialibrary.medialib.setVideoGroupsAllowSingleVideo(false)
  29. setPrefixLength()
  30. }
  31. func append(_ item: VLCMLVideoGroup) {
  32. assertionFailure("VideoGroupViewModel: Cannot append VideoGroups")
  33. }
  34. func delete(_ items: [VLCMLObject]) {
  35. assertionFailure("VideoGroupViewModel: Cannot delete VideoGroups")
  36. }
  37. func updateVideoGroups() {
  38. setPrefixLength()
  39. files = medialibrary.medialib.videoGroups() ?? []
  40. }
  41. func sort(by criteria: VLCMLSortingCriteria, desc: Bool) {
  42. files = medialibrary.medialib.videoGroups(with: criteria, desc: desc) ?? []
  43. sortModel.currentSort = criteria
  44. sortModel.desc = desc
  45. updateView?()
  46. }
  47. }
  48. // MARK: - Private helpers
  49. private extension VideoGroupViewModel {
  50. private func setPrefixLength() {
  51. let settingPrefixLength = UserDefaults.standard.integer(forKey:
  52. kVLCSettingsMediaLibraryVideoGroupPrefixLength)
  53. if prefixLength != settingPrefixLength {
  54. prefixLength = UInt32(settingPrefixLength)
  55. assert(prefixLength != 0, "VideoGroupViewModel: Failed to retrieve setting value.")
  56. medialibrary.medialib.setVideoGroupPrefixLength(prefixLength)
  57. }
  58. }
  59. }
  60. // MARK: - VLCMLVideoGroup - Search
  61. extension VLCMLVideoGroup: SearchableMLModel {
  62. func contains(_ searchString: String) -> Bool {
  63. return name().lowercased().contains(searchString)
  64. }
  65. }
  66. // MARK: - VLCMLVideoGroup - MediaCollectionModel
  67. extension VLCMLVideoGroup: MediaCollectionModel {
  68. func sortModel() -> SortModel? {
  69. return nil
  70. }
  71. func files(with criteria: VLCMLSortingCriteria,
  72. desc: Bool) -> [VLCMLMedia]? {
  73. return media(with: criteria, desc: desc)
  74. }
  75. func title() -> String {
  76. return name()
  77. }
  78. }
  79. // MARK: - VLCMLVideoGroup - Helpers
  80. extension VLCMLVideoGroup {
  81. func numberOfTracksString() -> String {
  82. let mediaCount = count()
  83. let tracksString = mediaCount > 1 ? NSLocalizedString("TRACKS", comment: "") : NSLocalizedString("TRACK", comment: "")
  84. return String(format: tracksString, mediaCount)
  85. }
  86. func accessibilityText() -> String? {
  87. return name() + " " + numberOfTracksString()
  88. }
  89. }