Music_Player3/MusicPlayer/MP/MPPositive/ViewControllers/Base(基类,导航栏,标签栏)/MPPositive_MoreSongOperationsViewController.swift
2024-05-29 17:31:45 +08:00

197 lines
8.3 KiB
Swift

//
// MPPositive_MoreOperationsViewController.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/5/29.
//
import UIKit
import Kingfisher
///
class MPPositive_MoreSongOperationsViewController: UIViewController {
//View
private var loadView = CircularProgressView()
//
private lazy var indictorImageView:UIImageView = .init(image: .init(named: "Player_Indictor'logo"))
//ICON
private lazy var iconImageView:UIImageView = {
let imageView:UIImageView = .init()
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 10*width
return imageView
}()
//Label
private lazy var titleLabel:UILabel = createLabel("Title", font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .white, textAlignment: .left)
//
private lazy var subtitleLabel:UILabel = createLabel("Title", font: .systemFont(ofSize: 12*width, weight: .regular), textColor: .init(hex: "#666666"), textAlignment: .left)
//
private lazy var collectionBtn:UIButton = {
let btn = UIButton()
btn.setImage(UIImage(named: "Artist_Collection'logo"), for: .normal)
btn.setImage(UIImage(named: "Artist_Collectioned'logo"), for: .selected)
btn.setBackgroundImage(UIImage(named: "Artist_Collection'bg"), for: .normal)
btn.setBackgroundImage(UIImage(named: "Artist_Collectioned'bg"), for: .selected)
btn.addTarget(self, action: #selector(collectionClick(_ :)), for: .touchUpInside)
return btn
}()
//线
private lazy var lineView:UIView = {
let lineView:UIView = UIView()
lineView.backgroundColor = .init(hex: "#444444")
return lineView
}()
//tableView
private lazy var tableView:UITableView = {
let tableView = UITableView(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), style: .plain)
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.contentInset = .init(top: 0, left: 0, bottom: bottomPadding, right: 0)
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableView.automaticDimension
tableView.dataSource = self
tableView.delegate = self
tableView.register(MPPositive_MoreOperationDownLoadTableViewCell.self, forCellReuseIdentifier: MPPositive_MoreOperationTableViewCellID)
return tableView
}()
private let MPPositive_MoreOperationTableViewCellID = "MPPositive_MoreOperationTableViewCell"
private var song:MPPositive_SongItemModel!{
didSet{
iconImageView.kf.setImage(with: URL(string: song.reviewUrls?.last ?? ""), placeholder: placeholderImage)
titleLabel.text = song.title
subtitleLabel.text = song.shortBylineText
//
collectionBtn.isSelected = MPPositive_CollectionSongModel.fetch(.init(format: "videoId == %@", song.videoId)).count != 0
//
isLoaded = MPPositive_DownloadItemModel.fetch(.init(format: "videoId == %@", song.videoId)).count != 0
}
}
private var isLoaded:Bool = false{
didSet{
DispatchQueue.main.async {
[weak self] in
self?.tableView.reloadData()
}
}
}
init(_ song:MPPositive_SongItemModel) {
super.init(nibName: nil, bundle: nil)
self.song = song
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .init(hex: "#282A2C")
view.layer.masksToBounds = true
view.layer.maskedCorners = [.layerMinXMinYCorner,.layerMinXMaxYCorner]
view.layer.cornerRadius = 18*width
configure()
}
private func configure() {
view.addSubview(indictorImageView)
indictorImageView.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.width.equalTo(29*width)
make.height.equalTo(4*width)
make.top.equalToSuperview().offset(8*width)
}
view.addSubview(iconImageView)
iconImageView.snp.makeConstraints { make in
make.width.height.equalTo(50*width)
make.left.equalToSuperview().offset(18*width)
make.top.equalToSuperview().offset(32*width)
}
view.addSubview(collectionBtn)
collectionBtn.snp.makeConstraints { make in
make.width.height.equalTo(32*width)
make.centerY.equalTo(iconImageView)
make.right.equalToSuperview().offset(-18*width)
}
view.addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
make.top.equalTo(iconImageView).offset(8*width)
make.left.equalTo(iconImageView.snp.right).offset(12*width)
make.right.equalTo(collectionBtn.snp.left).offset(-12*width)
}
view.addSubview(subtitleLabel)
subtitleLabel.snp.makeConstraints { make in
make.left.right.equalTo(titleLabel)
make.bottom.equalTo(iconImageView).offset(-8*width)
}
view.addSubview(lineView)
lineView.snp.makeConstraints { make in
make.width.equalTo(339*width)
make.height.equalTo(1*width)
make.centerX.equalToSuperview()
make.top.equalTo(iconImageView.snp.bottom).offset(15*width)
}
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.top.equalTo(lineView.snp.bottom).offset(8*width)
make.left.right.bottom.equalToSuperview()
}
}
//
@objc private func collectionClick(_ sender:UIButton) {
if self.collectionBtn.isSelected == true{
self.collectionBtn.isSelected = false
MPPositive_CollectionSongModel.fetch(.init(format: "videoId == %@", song.videoId)).forEach { item in
if item.videoId == song.videoId {
MPPositive_CollectionSongModel.delete(item)
}
}
MPPositive_LoadCoreModel.shared.reloadCollectionSongViewModel(nil)
}else{
self.collectionBtn.isSelected = true
let item = MPPositive_CollectionSongModel.create()
item.coverImage = URL(string: song.reviewUrls?.last ?? "")
item.title = song.title
item.subtitle = song.shortBylineText
item.videoId = song.videoId
item.lyricsID = song.lyricsID
item.relatedID = song.relatedID
MPPositive_CollectionSongModel.save()
MPPositive_LoadCoreModel.shared.reloadCollectionSongViewModel(nil)
}
}
}
//MARK: - tableView
extension MPPositive_MoreSongOperationsViewController:UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_MoreOperationTableViewCellID, for: indexPath) as! MPPositive_MoreOperationDownLoadTableViewCell
cell.title = isLoaded ? "Remove Download":"Add Download"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if isLoaded {
//
MPPositive_DownloadItemModel.fetch(.init(format: "videoId == %@", song.videoId)).forEach { item in
if item.videoId == song.videoId {
MPPositive_DownloadItemModel.delete(item)
}
}
MPPositive_LoadCoreModel.shared.reloadLoadSongViewModel(nil)
DownloadManager.shared.deleteFileDocuments(song.videoId) { [weak self] videoId in
guard let self = self else {return}
MP_HUD.progress("Loading...", delay: 0.5) {
self.isLoaded = false
MP_HUD.text("Removed", delay: 1.0, completion: nil)
}
}
}else {
//
}
}
}
}