VideoModel.swift 1.6 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 append(_ item: VLCMLMedia) {
  23. files.append(item)
  24. }
  25. }
  26. // MARK: - Sort
  27. extension VideoModel {
  28. func sort(by criteria: VLCMLSortingCriteria) {
  29. files = medialibrary.media(ofType: .video, sortingCriteria: criteria, desc: false)
  30. updateView?()
  31. }
  32. }
  33. // MARK: - MediaLibraryObserver
  34. extension VideoModel: MediaLibraryObserver {
  35. func medialibrary(_ medialibrary: VLCMediaLibraryManager, didAddVideos videos: [VLCMLMedia]) {
  36. videos.forEach({ append($0) })
  37. updateView?()
  38. }
  39. }
  40. extension VLCMLMedia {
  41. @objc func formatDuration() -> String {
  42. return String(format: "%@", VLCTime(int: Int32(duration())))
  43. }
  44. @objc func formatSize() -> String {
  45. return ByteCountFormatter.string(fromByteCount: Int64(mainFile().size()),
  46. countStyle: .file)
  47. }
  48. }