MediaModel.swift 2.1 KB

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