MediaModel.swift 5.8 KB

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