AudioModel.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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, .duration, .fileSize])
  14. var updateView: (() -> Void)?
  15. var files = [VLCMLMedia]()
  16. var cellType: BaseCollectionViewCell.Type { return AudioCollectionViewCell.self }
  17. var medialibrary: MediaLibraryService
  18. var indicatorName: String = NSLocalizedString("SONGS", comment: "")
  19. required init(medialibrary: MediaLibraryService) {
  20. self.medialibrary = medialibrary
  21. medialibrary.addObserver(self)
  22. files = medialibrary.media(ofType: .audio)
  23. }
  24. }
  25. // MARK: - Sort
  26. extension AudioModel {
  27. func sort(by criteria: VLCMLSortingCriteria) {
  28. // FIXME: Currently if sorted by name, the files are sorted by filename but displaying title
  29. files = medialibrary.media(ofType: .audio, sortingCriteria: criteria)
  30. sortModel.currentSort = criteria
  31. updateView?()
  32. }
  33. }
  34. // MARK: - Edit
  35. extension AudioModel: EditableMLModel {
  36. func editCellType() -> BaseCollectionViewCell.Type {
  37. return MediaEditCell.self
  38. }
  39. }
  40. // MARK: - MediaLibraryObserver
  41. extension AudioModel: MediaLibraryObserver {
  42. func medialibrary(_ medialibrary: MediaLibraryService, didAddAudios audios: [VLCMLMedia]) {
  43. audios.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. }