Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Center/MPPositive_AddSongsShowView.swift
2024-09-26 14:07:13 +08:00

144 lines
5.4 KiB
Swift

//
// MPPositive_AddSongsShowView.swift
// relax.offline.mp3.music
//
// Created by Mr.Zhou on 2024/9/24.
//
import UIKit
///
class MPPositive_AddSongsShowView: UIView, JXSegmentedListContainerViewListDelegate {
private lazy var tableView:UITableView = {
let tableView = UITableView()
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableView.automaticDimension
tableView.dataSource = self
tableView.delegate = self
tableView.contentInset = .init(top: 0, left: 0, bottom: 70*width, right: 0)
tableView.register(MPPositive_AddSongTableViewCell.self, forCellReuseIdentifier: MPPositive_AddSongTableViewCellID)
return tableView
}()
private let MPPositive_AddSongTableViewCellID = "MPPositive_AddSongTableViewCell"
//
var selectedIndex:Int = 0
///
var list:MPPositive_CustomPlayListModel?
//
private var loves:[MPPositive_CollectionSongViewModel] = []
//线
private var offlines:[MPPositive_DownloadViewModel] = []
init(frame: CGRect, playList:MPPositive_CustomPlayListModel) {
super.init(frame: frame)
self.list = playList
backgroundColor = .init(hex: "1A1A1A")
configure()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
//MARK: -
func listView() -> UIView {
return self
}
func listWillAppear() {
reload()
}
//
private func reload() {
if selectedIndex == 0 {
//
MPPositive_LoadCoreModel.shared.reloadCollectionSongViewModel {
[weak self] in
guard let self = self else {return}
loves = MPPositive_LoadCoreModel.shared.songViewModels
tableView.showMessage(loves.count)
tableView.reloadData()
}
}else {
//线
MPPositive_LoadCoreModel.shared.reloadLoadSongViewModel {
[weak self] in
guard let self = self else {return}
offlines = MPPositive_LoadCoreModel.shared.loadViewModels
tableView.showMessage(offlines.count)
tableView.reloadData()
}
}
}
//
private func configure() {
addSubview(tableView)
tableView.snp.makeConstraints { make in
make.left.top.right.bottom.equalToSuperview()
}
}
}
//MARK: - tableView
extension MPPositive_AddSongsShowView:UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return selectedIndex == 0 ? loves.count:offlines.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_AddSongTableViewCellID, for: indexPath) as! MPPositive_AddSongTableViewCell
cell.list = list
if selectedIndex == 0 {
cell.love = loves[indexPath.row]
}else {
cell.offline = offlines[indexPath.row]
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndex == 0 {
let item = loves[indexPath.row]
guard let array = list?.videosArray.filter({$0.videoId == item.collectionSong.videoId}), array.isEmpty == true else {
return
}
//
let video = try? MPPositive_CustomVideoModel.create()
video?.coverImageURL = item.coverURL
video?.title = item.title
video?.subtitle = item.collectionSong.subtitle
video?.videoId = item.collectionSong.videoId
video?.lyricsID = item.collectionSong.lyricsID
video?.relatedID = item.collectionSong.relatedID
video?.addTime = Date().timeZone()
video?.playList = list
MPPositive_CustomVideoModel.save()
MP_HUD.onlytext("Successfully added to the playlist".localizableString(), delay: 1.0) {
[weak self] in
self?.tableView.reloadData()
}
}else {
let item = offlines[indexPath.row]
guard let array = list?.videosArray.filter({$0.videoId == item.loadItem.videoId}), array.isEmpty == true else {
return
}
//
let video = try? MPPositive_CustomVideoModel.create()
video?.coverImageURL = item.reviewURL
video?.title = item.title
video?.subtitle = item.loadItem.shortBylineText
video?.videoId = item.loadItem.videoId
video?.lyricsID = item.loadItem.lyricsID
video?.relatedID = item.loadItem.relatedID
video?.addTime = Date().timeZone()
video?.playList = list
MPPositive_CustomVideoModel.save()
MP_HUD.onlytext("Successfully added to the playlist".localizableString(), delay: 1.0) {
[weak self] in
self?.tableView.reloadData()
}
}
}
}