Music_Player3/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Base(基类,导航栏,标签栏)/MPPositive_MoreSongOperationsViewController.swift

297 lines
14 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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.estimatedRowHeight = 200
tableView.rowHeight = UITableView.automaticDimension
tableView.dataSource = self
tableView.delegate = self
tableView.register(MPPositive_MoreOperationDownLoadTableViewCell.self, forCellReuseIdentifier: MPPositive_MoreOperationDownLoadTableViewCellID)
return tableView
}()
private let MPPositive_MoreOperationDownLoadTableViewCellID = "MPPositive_MoreOperationDownLoadTableViewCell"
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()
}
}
}
var removeBlock:(() -> Void)?
var disMissBlock:(() -> Void)?
var collectionBlock:(() -> Void)?
init(_ song:MPPositive_SongItemModel) {
super.init(nibName: nil, bundle: nil)
DispatchQueue.main.async {
[weak self] in
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,.layerMaxXMinYCorner]
view.layer.cornerRadius = 18*width
configure()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if disMissBlock != nil {
disMissBlock!()
}
}
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)
MP_AnalyticsManager.shared.player_b_unlove_clickAction(song.videoId, videoname: song.title ?? "", artistname: song.shortBylineText ?? "")
}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)
MP_AnalyticsManager.shared.player_b_love_clickAction(song.videoId, videoname: song.title ?? "", artistname: song.shortBylineText ?? "")
}
if collectionBlock != nil {
collectionBlock!()
}
}
}
//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_MoreOperationDownLoadTableViewCellID, for: indexPath) as! MPPositive_MoreOperationDownLoadTableViewCell
cell.title = isLoaded ? "Remove Download":"Add Download"
cell.restoreDownloadProgress(song)
cell.progressBlock = {
[weak self] in
guard let self = self else {return}
tableView.reloadRows(at: [.init(item: 0, section: 0)], with: .none)
}
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)
MP_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)
if self.removeBlock != nil {
self.removeBlock!()
}
}
}
}else {
//
let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_MoreOperationDownLoadTableViewCellID, for: indexPath) as! MPPositive_MoreOperationDownLoadTableViewCell
//
switch cell.loadBtn.state {
case .startDownload://
MP_AnalyticsManager.shared.player_b_download_clickAction(song.videoId ?? "", videoname: song.title ?? "", artistname: song.shortBylineText ?? "")
//
cell.loadBtn.state = .pending
tableView.reloadData()
//
guard let videoURL = URL(string: song.resourceUrls?.first ?? "") else {
MP_HUD.text("An error occurred while downloading. Please download again.", delay: 1.0) {[weak self] in
cell.loadBtn.state = .startDownload
self?.isLoaded = false
}
return
}
//
MP_DownloadManager.shared.downloadVideo(from: videoURL, song: song) { [weak self] progress in
DispatchQueue.main.async {
guard let self = self else {
//
cell.loadBtn.state = (MPPositive_DownloadItemModel.fetch(.init(format: "videoId == %@", (self?.song.videoId ?? ""))).count != 0) ? .downloaded:.startDownload
tableView.reloadData()
return
}
cell.loadBtn.state = .downloading
cell.loadBtn.stopDownloadButton.progress = progress
tableView.reloadData()
}
} completion: { [weak self] result in
//
switch result {
case .success(let song):
//
let item = MPPositive_DownloadItemModel.create()
item.coverImage = song.coverUrls!.last
item.reviewImage = song.reviewUrls!.last
item.title = song.title
item.longBylineText = song.longBylineText
item.lengthText = song.lengthText
item.shortBylineText = song.shortBylineText
item.lyrics = song.lyrics
item.lyricsID = song.lyricsID
item.videoId = song.videoId
item.relatedID = song.relatedID
MPPositive_DownloadItemModel.save()
DispatchQueue.main.async {
//线
if song.videoId == self?.song.videoId {
//
self?.isLoaded = true
}else {
//
self?.isLoaded = false
}
print("完成了对\(song.title ?? "")的下载")
//
cell.loadBtn.state = .downloaded
tableView.reloadData()
//
MPPositive_LoadCoreModel.shared.reloadLoadSongViewModel(nil)
MP_AnalyticsManager.shared.player_b_downloadsuccess_actionAction(item.videoId, videoname: item.title ?? "", artistname: item.shortBylineText ?? "")
}
case .failure(let error):
//
print("下载报错,错误详情\(error)")
DispatchQueue.main.async {
//
cell.loadBtn.state = .startDownload
tableView.reloadData()
}
MP_HUD.text("An error occurred while downloading. Please download again.", delay: 1.5, completion: nil)
}
}
default:
break
}
}
}
}
}