AudioModel.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*****************************************************************************
  2. * AudioModel.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 AudioModel: MediaModel {
  12. typealias MLType = VLCMLMedia
  13. var sortModel = SortModel(alpha: true,
  14. duration: true,
  15. fileSize: true)
  16. var updateView: (() -> Void)?
  17. var files = [VLCMLMedia]()
  18. var cellType: BaseCollectionViewCell.Type { return AudioCollectionViewCell.self }
  19. var medialibrary: MediaLibraryService
  20. var indicatorName: String = NSLocalizedString("SONGS", comment: "")
  21. required init(medialibrary: MediaLibraryService) {
  22. self.medialibrary = medialibrary
  23. medialibrary.addObserver(self)
  24. files = medialibrary.media(ofType: .audio)
  25. }
  26. }
  27. // MARK: - Sort
  28. extension AudioModel {
  29. func sort(by criteria: VLCMLSortingCriteria) {
  30. // FIXME: Currently if sorted by name, the files are sorted by filename but displaying title
  31. files = medialibrary.media(ofType: .audio, sortingCriteria: criteria)
  32. sortModel.currentSort = criteria
  33. updateView?()
  34. }
  35. }
  36. // MARK: - Edit
  37. extension AudioModel: EditableMLModel {
  38. func editCellType() -> BaseCollectionViewCell.Type {
  39. return MediaEditCell.self
  40. }
  41. }
  42. // MARK: - MediaLibraryObserver
  43. extension AudioModel: MediaLibraryObserver {
  44. func medialibrary(_ medialibrary: MediaLibraryService, didAddAudios audios: [VLCMLMedia]) {
  45. audios.forEach({ append($0) })
  46. updateView?()
  47. }
  48. func medialibrary(_ medialibrary: MediaLibraryService, didDeleteMediaWithIds ids: [NSNumber]) {
  49. files = files.filter() {
  50. for id in ids where $0.identifier() == id.int64Value {
  51. return false
  52. }
  53. return true
  54. }
  55. updateView?()
  56. }
  57. }