MediaModel.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. extension VLCMLMedia {
  39. @objc func mediaDuration() -> String {
  40. return String(format: "%@", VLCTime(int: Int32(duration())))
  41. }
  42. @objc func formatSize() -> String {
  43. return ByteCountFormatter.string(fromByteCount: Int64(mainFile()?.size() ?? 0),
  44. countStyle: .file)
  45. }
  46. func mediaProgress() -> Float {
  47. guard let string = metadata(of: .progress).str as NSString? else {
  48. return 0.0
  49. }
  50. return string.floatValue
  51. }
  52. func isNew() -> Bool {
  53. let integer = metadata(of: .seen).integer()
  54. return integer == 0
  55. }
  56. }