MediaModel.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. let isDarktheme = PresentationTheme.current == PresentationTheme.darkTheme
  45. if subtype() == .albumTrack {
  46. image = isDarktheme ? UIImage(named: "song-placeholder-dark") : UIImage(named: "song-placeholder-white")
  47. } else {
  48. image = isDarktheme ? UIImage(named: "movie-placeholder-dark") : UIImage(named: "movie-placeholder-white")
  49. }
  50. }
  51. return image
  52. }
  53. func accessibilityText(editing: Bool) -> String? {
  54. if editing {
  55. return title + " " + mediaDuration() + " " + formatSize()
  56. }
  57. return title + " " + albumTrackArtistName() + " " + (isNew ? NSLocalizedString("NEW", comment: "") : "")
  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. let image = UIImage(contentsOfFile: thumbnail()?.path ?? "")
  74. attributeSet.thumbnailData = image?.jpegData(compressionQuality: 0.9)
  75. }
  76. attributeSet.codecs = codecs()
  77. attributeSet.languages = languages()
  78. if let audioTracks = audioTracks {
  79. for track in audioTracks {
  80. attributeSet.audioBitRate = NSNumber(value: track.bitrate())
  81. attributeSet.audioChannelCount = NSNumber(value: track.nbChannels())
  82. attributeSet.audioSampleRate = NSNumber(value: track.sampleRate())
  83. }
  84. }
  85. if let albumTrack = albumTrack {
  86. if let genre = albumTrack.genre {
  87. attributeSet.genre = genre.name
  88. }
  89. if let artist = albumTrack.artist {
  90. attributeSet.artist = artist.name
  91. }
  92. attributeSet.audioTrackNumber = NSNumber(value:albumTrack.trackNumber())
  93. if let album = albumTrack.album {
  94. attributeSet.artist = album.title
  95. }
  96. }
  97. return attributeSet
  98. }
  99. func codecs() -> [String] {
  100. var codecs = [String]()
  101. if let videoTracks = videoTracks {
  102. for track in videoTracks {
  103. codecs.append(track.codec)
  104. }
  105. }
  106. if let audioTracks = audioTracks {
  107. for track in audioTracks {
  108. codecs.append(track.codec)
  109. }
  110. }
  111. if let subtitleTracks = subtitleTracks {
  112. for track in subtitleTracks {
  113. codecs.append(track.codec)
  114. }
  115. }
  116. return codecs
  117. }
  118. func languages() -> [String] {
  119. var languages = [String]()
  120. if let videoTracks = videoTracks {
  121. for track in videoTracks where track.language != "" {
  122. languages.append(track.language)
  123. }
  124. }
  125. if let audioTracks = audioTracks {
  126. for track in audioTracks where track.language != "" {
  127. languages.append(track.language)
  128. }
  129. }
  130. if let subtitleTracks = subtitleTracks {
  131. for track in subtitleTracks where track.language != "" {
  132. languages.append(track.language)
  133. }
  134. }
  135. return languages
  136. }
  137. func updateCoreSpotlightEntry() {
  138. if !KeychainCoordinator.passcodeLockEnabled {
  139. let groupIdentifier = ProcessInfo.processInfo.environment["GROUP_IDENTIFIER"]
  140. let item = CSSearchableItem(uniqueIdentifier: "\(identifier())", domainIdentifier: groupIdentifier, attributeSet: coreSpotlightAttributeSet())
  141. CSSearchableIndex.default().indexSearchableItems([item], completionHandler: nil)
  142. }
  143. }
  144. }
  145. // MARK: - Search
  146. extension VLCMLMedia: SearchableMLModel {
  147. func contains(_ searchString: String) -> Bool {
  148. return title.lowercased().contains(searchString)
  149. }
  150. }
  151. extension VLCMLMedia {
  152. func albumTrackArtistName() -> String {
  153. guard let albumTrack = albumTrack else {
  154. return NSLocalizedString("UNKNOWN_ARTIST", comment: "")
  155. }
  156. return albumTrack.albumArtistName()
  157. }
  158. }