VideoModel.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. extension VideoModel: MediaLibraryObserver {
  36. func medialibrary(_ medialibrary: VLCMediaLibraryManager, didAddVideo video: [VLCMLMedia]) {
  37. video.forEach({ append($0) })
  38. updateView?()
  39. }
  40. }
  41. extension VLCMLMedia {
  42. @objc func formatDuration(ofMedia media: VLCMLMedia) -> String {
  43. return String(format: "%@ - %@",
  44. VLCTime(int: Int32(media.duration())),
  45. ByteCountFormatter.string(fromByteCount: Int64(media.mainFile().size()),
  46. countStyle: .file))
  47. }
  48. }