VideoGroupViewModel.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. required init(medialibrary: MediaLibraryService) {
  25. self.medialibrary = medialibrary
  26. self.files = medialibrary.medialib.videoGroups() ?? []
  27. medialibrary.medialib.setVideoGroupsAllowSingleVideo(false)
  28. }
  29. func append(_ item: VLCMLVideoGroup) {
  30. assertionFailure("VideoGroupViewModel: Cannot append VideoGroups")
  31. }
  32. func delete(_ items: [VLCMLObject]) {
  33. assertionFailure("VideoGroupViewModel: Cannot delete VideoGroups")
  34. }
  35. func updateVideoGroups() {
  36. files = medialibrary.medialib.videoGroups() ?? []
  37. }
  38. func sort(by criteria: VLCMLSortingCriteria, desc: Bool) {
  39. files = medialibrary.medialib.videoGroups(with: criteria, desc: desc) ?? []
  40. sortModel.currentSort = criteria
  41. sortModel.desc = desc
  42. updateView?()
  43. }
  44. }
  45. // MARK: - Edit
  46. extension VideoGroupViewModel: EditableMLModel {
  47. func editCellType() -> BaseCollectionViewCell.Type {
  48. return MediaEditCell.self
  49. }
  50. }
  51. // MARK: - VLCMLVideoGroup - Search
  52. extension VLCMLVideoGroup: SearchableMLModel {
  53. func contains(_ searchString: String) -> Bool {
  54. return name().lowercased().contains(searchString)
  55. }
  56. }
  57. // MARK: - VLCMLVideoGroup - MediaCollectionModel
  58. extension VLCMLVideoGroup: MediaCollectionModel {
  59. func sortModel() -> SortModel? {
  60. return nil
  61. }
  62. func files() -> [VLCMLMedia]? {
  63. return media()
  64. }
  65. func title() -> String {
  66. return name()
  67. }
  68. func sortFilesInCollection(with criteria: VLCMLSortingCriteria,
  69. desc: Bool) -> [VLCMLMedia]? {
  70. return media(with: criteria, desc: desc)
  71. }
  72. }
  73. // MARK: - VLCMLVideoGroup - Helpers
  74. extension VLCMLVideoGroup {
  75. func numberOfTracksString() -> String {
  76. let mediaCount = count()
  77. let tracksString = mediaCount > 1 ? NSLocalizedString("TRACKS", comment: "") : NSLocalizedString("TRACK", comment: "")
  78. return String(format: tracksString, mediaCount)
  79. }
  80. @objc func thumbnail() -> UIImage? {
  81. var image: UIImage?
  82. for media in files() ?? [] where media.isThumbnailGenerated() {
  83. image = UIImage(contentsOfFile: media.thumbnail()?.path ?? "")
  84. break
  85. }
  86. if image == nil {
  87. let isDarktheme = PresentationTheme.current == PresentationTheme.darkTheme
  88. image = isDarktheme ? UIImage(named: "movie-placeholder-dark") : UIImage(named: "movie-placeholder-white")
  89. }
  90. return image
  91. }
  92. func accessibilityText() -> String? {
  93. return name() + " " + numberOfTracksString()
  94. }
  95. }