MediaModel.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. func title() -> String {
  62. if UserDefaults.standard.bool(forKey: kVLCOptimizeItemNamesForDisplay) == true
  63. && ((subtype() == .albumTrack && title.isSupportedAudioMediaFormat())
  64. || (subtype() != .albumTrack && title.isSupportedMediaFormat())) {
  65. return (title as NSString).deletingPathExtension
  66. }
  67. return title
  68. }
  69. }
  70. // MARK: - CoreSpotlight
  71. extension VLCMLMedia {
  72. func coreSpotlightAttributeSet() -> CSSearchableItemAttributeSet {
  73. let attributeSet = CSSearchableItemAttributeSet(itemContentType: "public.audiovisual-content")
  74. attributeSet.title = title
  75. attributeSet.metadataModificationDate = Date()
  76. attributeSet.addedDate = Date()
  77. attributeSet.duration = NSNumber(value: duration() / 1000)
  78. attributeSet.streamable = 0
  79. attributeSet.deliveryType = 0
  80. attributeSet.local = 1
  81. attributeSet.playCount = NSNumber(value: playCount())
  82. if isThumbnailGenerated() {
  83. let image = UIImage(contentsOfFile: thumbnail()?.path ?? "")
  84. attributeSet.thumbnailData = image?.jpegData(compressionQuality: 0.9)
  85. }
  86. attributeSet.codecs = codecs()
  87. attributeSet.languages = languages()
  88. if let audioTracks = audioTracks {
  89. for track in audioTracks {
  90. attributeSet.audioBitRate = NSNumber(value: track.bitrate())
  91. attributeSet.audioChannelCount = NSNumber(value: track.nbChannels())
  92. attributeSet.audioSampleRate = NSNumber(value: track.sampleRate())
  93. }
  94. }
  95. if let albumTrack = albumTrack {
  96. if let genre = albumTrack.genre {
  97. attributeSet.genre = genre.name
  98. }
  99. if let artist = albumTrack.artist {
  100. attributeSet.artist = artist.name
  101. }
  102. attributeSet.audioTrackNumber = NSNumber(value:albumTrack.trackNumber())
  103. if let album = albumTrack.album {
  104. attributeSet.artist = album.title
  105. }
  106. }
  107. return attributeSet
  108. }
  109. func codecs() -> [String] {
  110. var codecs = [String]()
  111. if let videoTracks = videoTracks {
  112. for track in videoTracks {
  113. codecs.append(track.codec)
  114. }
  115. }
  116. if let audioTracks = audioTracks {
  117. for track in audioTracks {
  118. codecs.append(track.codec)
  119. }
  120. }
  121. if let subtitleTracks = subtitleTracks {
  122. for track in subtitleTracks {
  123. codecs.append(track.codec)
  124. }
  125. }
  126. return codecs
  127. }
  128. func languages() -> [String] {
  129. var languages = [String]()
  130. if let videoTracks = videoTracks {
  131. for track in videoTracks where track.language != "" {
  132. languages.append(track.language)
  133. }
  134. }
  135. if let audioTracks = audioTracks {
  136. for track in audioTracks where track.language != "" {
  137. languages.append(track.language)
  138. }
  139. }
  140. if let subtitleTracks = subtitleTracks {
  141. for track in subtitleTracks where track.language != "" {
  142. languages.append(track.language)
  143. }
  144. }
  145. return languages
  146. }
  147. func updateCoreSpotlightEntry() {
  148. if !KeychainCoordinator.passcodeLockEnabled {
  149. let groupIdentifier = ProcessInfo.processInfo.environment["GROUP_IDENTIFIER"]
  150. let item = CSSearchableItem(uniqueIdentifier: "\(identifier())", domainIdentifier: groupIdentifier, attributeSet: coreSpotlightAttributeSet())
  151. CSSearchableIndex.default().indexSearchableItems([item], completionHandler: nil)
  152. }
  153. }
  154. }
  155. // MARK: - Search
  156. extension VLCMLMedia: SearchableMLModel {
  157. func contains(_ searchString: String) -> Bool {
  158. return title.lowercased().contains(searchString)
  159. }
  160. }
  161. extension VLCMLMedia {
  162. func albumTrackArtistName() -> String {
  163. guard let albumTrack = albumTrack else {
  164. return NSLocalizedString("UNKNOWN_ARTIST", comment: "")
  165. }
  166. return albumTrack.albumArtistName()
  167. }
  168. }