MediaModel.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*****************************************************************************
  2. * MediaModel.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. protocol MediaModel: MLBaseModel where MLType == VLCMLMedia { }
  12. extension MediaModel {
  13. func append(_ item: VLCMLMedia) {
  14. if !files.contains { $0 == item } {
  15. files.append(item)
  16. }
  17. }
  18. func delete(_ items: [VLCMLObject]) {
  19. do {
  20. for case let media as VLCMLMedia in items {
  21. if let mainFile = media.mainFile() {
  22. try FileManager.default.removeItem(atPath: mainFile.mrl.path)
  23. }
  24. }
  25. medialibrary.reload()
  26. }
  27. catch let error as NSError {
  28. assertionFailure("MediaModel: Delete failed: \(error.localizedDescription)")
  29. }
  30. }
  31. }
  32. // MARK: - ViewModel
  33. extension VLCMLMedia {
  34. @objc func mediaDuration() -> String {
  35. return String(format: "%@", VLCTime(int: Int32(duration())))
  36. }
  37. @objc func formatSize() -> String {
  38. return ByteCountFormatter.string(fromByteCount: Int64(mainFile()?.size() ?? 0),
  39. countStyle: .file)
  40. }
  41. @objc func thumbnailImage() -> UIImage? {
  42. var image = UIImage(contentsOfFile: thumbnail()?.path ?? "")
  43. if image == nil
  44. || (!UserDefaults.standard.bool(forKey: kVLCSettingShowThumbnails) && subtype() != .albumTrack)
  45. || (!UserDefaults.standard.bool(forKey: kVLCSettingShowArtworks) && subtype() == .albumTrack) {
  46. let isDarktheme = PresentationTheme.current == PresentationTheme.darkTheme
  47. if subtype() == .albumTrack {
  48. image = isDarktheme ? UIImage(named: "song-placeholder-dark") : UIImage(named: "song-placeholder-white")
  49. } else {
  50. image = isDarktheme ? UIImage(named: "movie-placeholder-dark") : UIImage(named: "movie-placeholder-white")
  51. }
  52. }
  53. return image
  54. }
  55. func accessibilityText(editing: Bool) -> String? {
  56. if editing {
  57. return title + " " + mediaDuration() + " " + formatSize()
  58. }
  59. return title + " " + albumTrackArtistName() + " " + (isNew ? NSLocalizedString("NEW", comment: "") : "")
  60. }
  61. }
  62. // MARK: - CoreSpotlight
  63. extension VLCMLMedia {
  64. func coreSpotlightAttributeSet() -> CSSearchableItemAttributeSet {
  65. let attributeSet = CSSearchableItemAttributeSet(itemContentType: "public.audiovisual-content")
  66. attributeSet.title = title
  67. attributeSet.metadataModificationDate = Date()
  68. attributeSet.addedDate = Date()
  69. attributeSet.duration = NSNumber(value: duration() / 1000)
  70. attributeSet.streamable = 0
  71. attributeSet.deliveryType = 0
  72. attributeSet.local = 1
  73. attributeSet.playCount = NSNumber(value: playCount())
  74. if isThumbnailGenerated() {
  75. let image = UIImage(contentsOfFile: thumbnail()?.path ?? "")
  76. attributeSet.thumbnailData = image?.jpegData(compressionQuality: 0.9)
  77. }
  78. attributeSet.codecs = codecs()
  79. attributeSet.languages = languages()
  80. if let audioTracks = audioTracks {
  81. for track in audioTracks {
  82. attributeSet.audioBitRate = NSNumber(value: track.bitrate())
  83. attributeSet.audioChannelCount = NSNumber(value: track.nbChannels())
  84. attributeSet.audioSampleRate = NSNumber(value: track.sampleRate())
  85. }
  86. }
  87. if let albumTrack = albumTrack {
  88. if let genre = albumTrack.genre {
  89. attributeSet.genre = genre.name
  90. }
  91. if let artist = albumTrack.artist {
  92. attributeSet.artist = artist.name
  93. }
  94. attributeSet.audioTrackNumber = NSNumber(value:albumTrack.trackNumber())
  95. if let album = albumTrack.album {
  96. attributeSet.artist = album.title
  97. }
  98. }
  99. return attributeSet
  100. }
  101. func codecs() -> [String] {
  102. var codecs = [String]()
  103. if let videoTracks = videoTracks {
  104. for track in videoTracks {
  105. codecs.append(track.codec)
  106. }
  107. }
  108. if let audioTracks = audioTracks {
  109. for track in audioTracks {
  110. codecs.append(track.codec)
  111. }
  112. }
  113. if let subtitleTracks = subtitleTracks {
  114. for track in subtitleTracks {
  115. codecs.append(track.codec)
  116. }
  117. }
  118. return codecs
  119. }
  120. func languages() -> [String] {
  121. var languages = [String]()
  122. if let videoTracks = videoTracks {
  123. for track in videoTracks where track.language != "" {
  124. languages.append(track.language)
  125. }
  126. }
  127. if let audioTracks = audioTracks {
  128. for track in audioTracks where track.language != "" {
  129. languages.append(track.language)
  130. }
  131. }
  132. if let subtitleTracks = subtitleTracks {
  133. for track in subtitleTracks where track.language != "" {
  134. languages.append(track.language)
  135. }
  136. }
  137. return languages
  138. }
  139. func updateCoreSpotlightEntry() {
  140. if !KeychainCoordinator.passcodeLockEnabled {
  141. let groupIdentifier = ProcessInfo.processInfo.environment["GROUP_IDENTIFIER"]
  142. let item = CSSearchableItem(uniqueIdentifier: "\(identifier())", domainIdentifier: groupIdentifier, attributeSet: coreSpotlightAttributeSet())
  143. CSSearchableIndex.default().indexSearchableItems([item], completionHandler: nil)
  144. }
  145. }
  146. }
  147. // MARK: - Search
  148. extension VLCMLMedia: SearchableMLModel {
  149. func contains(_ searchString: String) -> Bool {
  150. return title.lowercased().contains(searchString)
  151. }
  152. }
  153. extension VLCMLMedia {
  154. func albumTrackArtistName() -> String {
  155. guard let albumTrack = albumTrack else {
  156. return NSLocalizedString("UNKNOWN_ARTIST", comment: "")
  157. }
  158. return albumTrack.albumArtistName()
  159. }
  160. }