MediaModel.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. try FileManager.default.removeItem(atPath: media.mainFile().mrl.path)
  22. }
  23. medialibrary.reload()
  24. }
  25. catch let error as NSError {
  26. assertionFailure("MediaModel: Delete failed: \(error.localizedDescription)")
  27. }
  28. }
  29. }
  30. // MARK: - VLCMLMedia
  31. extension VLCMLMedia {
  32. static func == (lhs: VLCMLMedia, rhs: VLCMLMedia) -> Bool {
  33. return lhs.identifier() == rhs.identifier()
  34. }
  35. }
  36. extension VLCMLMedia {
  37. @objc func mediaDuration() -> String {
  38. return String(format: "%@", VLCTime(int: Int32(duration())))
  39. }
  40. @objc func formatSize() -> String {
  41. return ByteCountFormatter.string(fromByteCount: Int64(mainFile().size()),
  42. countStyle: .file)
  43. }
  44. func mediaProgress() -> Float {
  45. guard let string = metadata(of: .progress).str as NSString? else {
  46. return 0.0
  47. }
  48. return string.floatValue
  49. }
  50. func isNew() -> Bool {
  51. let integer = metadata(of: .seen).integer()
  52. return integer == 0
  53. }
  54. }