MediaLibraryBaseModel.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*****************************************************************************
  2. * MediaLibraryBaseModel.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. // Expose a "shadow" version without associatedType in order to use it as a type
  12. protocol MediaLibraryBaseModel {
  13. init(medialibrary: MediaLibraryService)
  14. var anyfiles: [VLCMLObject] { get }
  15. var sortModel: SortModel { get }
  16. var updateView: (() -> Void)? { get set }
  17. var indicatorName: String { get }
  18. var cellType: BaseCollectionViewCell.Type { get }
  19. func append(_ item: VLCMLObject)
  20. func delete(_ items: [VLCMLObject])
  21. func sort(by criteria: VLCMLSortingCriteria, desc: Bool)
  22. }
  23. protocol MLBaseModel: AnyObject, MediaLibraryBaseModel {
  24. associatedtype MLType where MLType: VLCMLObject
  25. init(medialibrary: MediaLibraryService)
  26. var files: [MLType] { get set }
  27. var medialibrary: MediaLibraryService { get }
  28. var updateView: (() -> Void)? { get set }
  29. var indicatorName: String { get }
  30. func append(_ item: MLType)
  31. // FIXME: Ideally items should be MLType but Swift isn't happy so it will always fail
  32. func delete(_ items: [VLCMLObject])
  33. func sort(by criteria: VLCMLSortingCriteria, desc: Bool)
  34. }
  35. extension MLBaseModel {
  36. var anyfiles: [VLCMLObject] {
  37. return files
  38. }
  39. func append(_ item: VLCMLObject) {
  40. fatalError()
  41. }
  42. func delete(_ items: [VLCMLObject]) {
  43. fatalError()
  44. }
  45. func sort(by criteria: VLCMLSortingCriteria, desc: Bool) {
  46. fatalError()
  47. }
  48. }
  49. protocol EditableMLModel {
  50. func editCellType() -> BaseCollectionViewCell.Type
  51. }
  52. protocol SearchableMLModel {
  53. func contains(_ searchString: String) -> Bool
  54. }
  55. protocol MediaCollectionModel {
  56. func files() -> [VLCMLMedia]?
  57. func sortModel() -> SortModel?
  58. func title() -> String
  59. }
  60. // MARK: - Helper methods
  61. extension MLBaseModel {
  62. /// Swap the given [MLType] to the cached array.
  63. /// This only swaps models with the same VLCMLIdentifiers
  64. /// - Parameter models: To be swapped models
  65. /// - Returns: New array of `MLType` if changes have been made, else return a unchanged cached version.
  66. func swapModels(with models: [MLType]) -> [MLType] {
  67. var newFiles = files
  68. // FIXME: This should be handled in a thread safe way
  69. for var model in models {
  70. for (currentMediaIndex, file) in files.enumerated()
  71. where file.identifier() == model.identifier() {
  72. swap(&newFiles[currentMediaIndex], &model)
  73. break
  74. }
  75. }
  76. return newFiles
  77. }
  78. }
  79. extension VLCMLObject {
  80. static func == (lhs: VLCMLObject, rhs: VLCMLObject) -> Bool {
  81. return lhs.identifier() == rhs.identifier()
  82. }
  83. }