VideoModel.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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: MediaModel {
  12. typealias MLType = VLCMLMedia
  13. var sortModel = SortModel([.alpha, .duration, .insertionDate, .releaseDate, .fileSize])
  14. var updateView: (() -> Void)?
  15. var files = [VLCMLMedia]()
  16. var cellType: BaseCollectionViewCell.Type { return MovieCollectionViewCell.self }
  17. var medialibrary: MediaLibraryService
  18. var indicatorName: String = NSLocalizedString("MOVIES", comment: "")
  19. required init(medialibrary: MediaLibraryService) {
  20. self.medialibrary = medialibrary
  21. medialibrary.addObserver(self)
  22. files = medialibrary.media(ofType: .video)
  23. medialibrary.requestThumbnail(for: files)
  24. }
  25. }
  26. // MARK: - Edit
  27. extension VideoModel: EditableMLModel {
  28. func editCellType() -> BaseCollectionViewCell.Type {
  29. return MediaEditCell.self
  30. }
  31. }
  32. // MARK: - Sort
  33. extension VideoModel {
  34. func sort(by criteria: VLCMLSortingCriteria) {
  35. files = medialibrary.media(ofType: .video, sortingCriteria: criteria)
  36. sortModel.currentSort = criteria
  37. updateView?()
  38. }
  39. }
  40. // MARK: - MediaLibraryObserver
  41. extension VideoModel: MediaLibraryObserver {
  42. func medialibrary(_ medialibrary: MediaLibraryService, didAddVideos videos: [VLCMLMedia]) {
  43. videos.forEach({ append($0) })
  44. updateView?()
  45. }
  46. func medialibrary(_ medialibrary: MediaLibraryService, didDeleteMediaWithIds ids: [NSNumber]) {
  47. files = files.filter() {
  48. for id in ids where $0.identifier() == id.int64Value {
  49. return false
  50. }
  51. return true
  52. }
  53. updateView?()
  54. }
  55. // MARK: - Thumbnail
  56. func medialibrary(_ medialibrary: MediaLibraryService, thumbnailReady media: VLCMLMedia) {
  57. for (index, file) in files.enumerated() {
  58. if file == media {
  59. files[index] = media
  60. break
  61. }
  62. }
  63. updateView?()
  64. }
  65. }