MediaModel.swift 6.1 KB

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