MediaModel.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
  32. guard let playlist = medialibrary.createPlaylist(with: name) else {
  33. assertionFailure("MediaModel: createPlaylist: Failed to create a playlist.")
  34. return
  35. }
  36. guard let fileIndexes = fileIndexes else {
  37. return
  38. }
  39. for index in fileIndexes where index.row < files.count {
  40. playlist.appendMedia(withIdentifier: files[index.row].identifier())
  41. }
  42. }
  43. }
  44. // MARK: - Helpers
  45. extension VLCMLMedia {
  46. static func == (lhs: VLCMLMedia, rhs: VLCMLMedia) -> Bool {
  47. return lhs.identifier() == rhs.identifier()
  48. }
  49. }
  50. extension VLCMLMedia {
  51. @objc func mediaDuration() -> String {
  52. return String(format: "%@", VLCTime(int: Int32(duration())))
  53. }
  54. @objc func formatSize() -> String {
  55. return ByteCountFormatter.string(fromByteCount: Int64(mainFile()?.size() ?? 0),
  56. countStyle: .file)
  57. }
  58. func mediaProgress() -> Float {
  59. guard let string = metadata(of: .progress).str as NSString? else {
  60. return 0.0
  61. }
  62. return string.floatValue
  63. }
  64. func isNew() -> Bool {
  65. let integer = metadata(of: .seen).integer()
  66. return integer == 0
  67. }
  68. }