SongListViewModel.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // MusicEmptyView.swift
  3. // PhysicalWallPaper
  4. //
  5. // Created by nkl on 2024/11/13.
  6. //
  7. import CoreData
  8. import Foundation
  9. import TSVideoKit
  10. import UIKit
  11. import KLTips
  12. class SongListViewModel: NSObject {
  13. @Published var videos: [TSVideo] = []
  14. var isMutiSelectModel: Bool = false
  15. var selectedVideos: [TSVideo] = []
  16. var sortType: SortType = .downTime
  17. var playLists: [TSPlayList] = []
  18. var recentlyVideos : [TSVideo] = []
  19. var isEmptyData: Bool {
  20. videos.isEmpty && unFinishedVideo.isEmpty
  21. }
  22. var favouriteVideos: [TSVideo] {
  23. videos.filter {
  24. $0.isFavorite
  25. }
  26. }
  27. var sortDescriptor: NSSortDescriptor {
  28. let sortDescriptor: NSSortDescriptor
  29. switch sortType {
  30. case .upTime:
  31. sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
  32. case .downTime:
  33. sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: false)
  34. case .upName:
  35. sortDescriptor = NSSortDescriptor(key: "title", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:)))
  36. case .downName:
  37. sortDescriptor = NSSortDescriptor(key: "title", ascending: false, selector: #selector(NSString.localizedStandardCompare(_:)))
  38. }
  39. return sortDescriptor
  40. }
  41. var unFinishedVideo: [SongDownloadCellViewModel] = []
  42. func requestData() {
  43. videos = TSVideoOperator.shared.dataManager.fetchCachedVideos(sort: sortDescriptor)
  44. playLists = TSVideoOperator.shared.dataManager.fetchPlaylists()
  45. recentlyVideos = TSVideoOperator.shared.dataManager.fetchHistoryVideos()
  46. let videos = TSVideoOperator.shared.dataManager.fetchNotCachedVideos()
  47. unFinishedVideo = videos.map {
  48. SongDownloadCellViewModel(dataModel: nil, video: $0)
  49. }
  50. }
  51. }
  52. extension SongListViewModel: UITableViewDataSource {
  53. func numberOfSections(in tableView: UITableView) -> Int {
  54. return 2
  55. }
  56. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  57. if section == 0 {
  58. if isMutiSelectModel {
  59. return 0
  60. } else {
  61. return unFinishedVideo.count
  62. }
  63. } else {
  64. return videos.count
  65. }
  66. }
  67. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  68. if indexPath.section == 0 {
  69. let cell = tableView.dequeueReusableCell(withIdentifier: "SongDownloadCell") as! SongDownloadCell
  70. return cell
  71. } else {
  72. if let cell = tableView.dequeueReusableCell(withIdentifier: "SongListCell") as? SongListCell {
  73. if indexPath.row < videos.count {
  74. let videoModel = videos[indexPath.row]
  75. let isSelected = selectedVideos.contains(where: {
  76. $0.videoId == videoModel.videoId
  77. })
  78. cell.bindData(video: videoModel, isMutiSelected: isMutiSelectModel, isSelected: isSelected,needOperate: true)
  79. }
  80. return cell
  81. }
  82. return SongListCell()
  83. }
  84. }
  85. /// 加入选中列表
  86. /// - Parameter video: video
  87. func addToSelectedList(video: TSVideo) {
  88. // 已经有了,那么删除
  89. if selectedVideos.contains(where: {
  90. $0.videoId == video.videoId
  91. }) {
  92. selectedVideos.removeAll(where: {
  93. $0.videoId == video.videoId
  94. })
  95. } else {
  96. selectedVideos.append(video)
  97. }
  98. }
  99. ///todo.kailen
  100. // func showOperationViewController(video: TSVideo) {
  101. // let operationVc = RingOperateViewController(operateTypes: [.edit, .addPlaylist, .like, .delete, .share, .ring])
  102. // operationVc.video = video
  103. // operationVc.presentVc = PlayRootController.shared
  104. // operationVc.modalPresentationStyle = .overFullScreen
  105. // PlayRootController.shared.present(operationVc, animated: true)
  106. // }
  107. }