VideoModel.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*****************************************************************************
  2. * VideoModel.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. class VideoModel: MLBaseModel {
  12. typealias MLType = VLCMLMedia
  13. var updateView: (() -> Void)?
  14. var files = [VLCMLMedia]()
  15. var medialibrary: VLCMediaLibraryManager
  16. var indicatorName: String = NSLocalizedString("MOVIES", comment: "")
  17. required init(medialibrary: VLCMediaLibraryManager) {
  18. self.medialibrary = medialibrary
  19. medialibrary.addObserver(self)
  20. files = medialibrary.media(ofType: .video)
  21. }
  22. func isIncluded(_ item: VLCMLMedia) {
  23. }
  24. func append(_ item: VLCMLMedia) {
  25. files.append(item)
  26. }
  27. }
  28. // MARK: - Sort
  29. extension VideoModel {
  30. func sort(by criteria: VLCMLSortingCriteria) {
  31. files = medialibrary.media(ofType: .video, sortingCriteria: criteria, desc: false)
  32. updateView?()
  33. }
  34. }
  35. // MARK: - MediaLibraryObserver
  36. extension VideoModel: MediaLibraryObserver {
  37. func medialibrary(_ medialibrary: VLCMediaLibraryManager, didAddVideo video: [VLCMLMedia]) {
  38. video.forEach({ append($0) })
  39. updateView?()
  40. }
  41. }
  42. extension VLCMLMedia {
  43. @objc func formatDuration() -> String {
  44. return String(format: "%@", VLCTime(int: Int32(duration())))
  45. }
  46. @objc func formatSize() -> String {
  47. return ByteCountFormatter.string(fromByteCount: Int64(mainFile().size()),
  48. countStyle: .file)
  49. }
  50. }