MediaLibraryBaseModel.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*****************************************************************************
  2. * MediaLibraryBaseModel.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. // Expose a "shadow" version without associatedType in order to use it as a type
  12. protocol MediaLibraryBaseModel {
  13. init(medialibrary: MediaLibraryService)
  14. var anyfiles: [VLCMLObject] { get }
  15. var sortModel: SortModel { get }
  16. var updateView: (() -> Void)? { get set }
  17. var indicatorName: String { get }
  18. var cellType: BaseCollectionViewCell.Type { get }
  19. func append(_ item: VLCMLObject)
  20. func delete(_ items: [VLCMLObject])
  21. func sort(by criteria: VLCMLSortingCriteria, desc: Bool)
  22. }
  23. protocol MLBaseModel: AnyObject, MediaLibraryBaseModel {
  24. associatedtype MLType where MLType: VLCMLObject
  25. init(medialibrary: MediaLibraryService)
  26. var files: [MLType] { get set }
  27. var medialibrary: MediaLibraryService { get }
  28. var updateView: (() -> Void)? { get set }
  29. var indicatorName: String { get }
  30. func append(_ item: MLType)
  31. // FIXME: Ideally items should be MLType but Swift isn't happy so it will always fail
  32. func delete(_ items: [VLCMLObject])
  33. func sort(by criteria: VLCMLSortingCriteria, desc: Bool)
  34. }
  35. extension MLBaseModel {
  36. var anyfiles: [VLCMLObject] {
  37. return files
  38. }
  39. func append(_ item: VLCMLObject) {
  40. fatalError()
  41. }
  42. func delete(_ items: [VLCMLObject]) {
  43. fatalError()
  44. }
  45. func sort(by criteria: VLCMLSortingCriteria, desc: Bool) {
  46. fatalError()
  47. }
  48. }
  49. protocol SearchableMLModel {
  50. func contains(_ searchString: String) -> Bool
  51. }
  52. protocol MediaCollectionModel {
  53. func files(with criteria: VLCMLSortingCriteria,
  54. desc: Bool) -> [VLCMLMedia]?
  55. func sortModel() -> SortModel?
  56. func title() -> String
  57. }
  58. // MARK: - Helper methods
  59. extension MLBaseModel {
  60. /// Swap the given [MLType] to the cached array.
  61. /// This only swaps models with the same VLCMLIdentifiers
  62. /// - Parameter models: To be swapped models
  63. /// - Returns: New array of `MLType` if changes have been made, else return a unchanged cached version.
  64. func swapModels(with models: [MLType]) -> [MLType] {
  65. var newFiles = files
  66. // FIXME: This should be handled in a thread safe way
  67. for var model in models {
  68. for (currentMediaIndex, file) in files.enumerated()
  69. where file.identifier() == model.identifier() {
  70. swap(&newFiles[currentMediaIndex], &model)
  71. break
  72. }
  73. }
  74. return newFiles
  75. }
  76. func filterFilesFromDeletion(of items: [VLCMLObject]) {
  77. files = files.filter() {
  78. for item in items where $0.identifier() == item.identifier() {
  79. return false
  80. }
  81. return true
  82. }
  83. }
  84. }
  85. extension VLCMLObject {
  86. static func == (lhs: VLCMLObject, rhs: VLCMLObject) -> Bool {
  87. return lhs.identifier() == rhs.identifier()
  88. }
  89. }
  90. extension MediaCollectionModel {
  91. func files(with criteria: VLCMLSortingCriteria = .default,
  92. desc: Bool = false) -> [VLCMLMedia]? {
  93. return files(with: criteria, desc: desc)
  94. }
  95. func thumbnail() -> UIImage? {
  96. var image: UIImage? = nil
  97. if image == nil {
  98. for track in files() ?? [] where track.isThumbnailGenerated() == .available {
  99. image = UIImage(contentsOfFile: track.thumbnail()?.path ?? "")
  100. break
  101. }
  102. }
  103. if image == nil
  104. || (!UserDefaults.standard.bool(forKey: kVLCSettingShowThumbnails) && self is VLCMLVideoGroup)
  105. || (!UserDefaults.standard.bool(forKey: kVLCSettingShowArtworks) && !(self is VLCMLVideoGroup)) {
  106. let isDarktheme = PresentationTheme.current == PresentationTheme.darkTheme
  107. if self is VLCMLVideoGroup {
  108. image = isDarktheme ? UIImage(named: "movie-placeholder-dark") : UIImage(named: "movie-placeholder-white")
  109. } else {
  110. image = isDarktheme ? UIImage(named: "album-placeholder-dark") : UIImage(named: "album-placeholder-white")
  111. }
  112. }
  113. return image
  114. }
  115. }