422 lines
20 KiB
Swift
422 lines
20 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.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{
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
// MP_HUD.hideNow()
|
||
view.isUserInteractionEnabled = true
|
||
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
|
||
}
|
||
}
|
||
//通过browseViewModel加载更多框
|
||
init(_ browseViewModel:MPPositive_BrowseItemViewModel) {
|
||
super.init(nibName: nil, bundle: nil)
|
||
view.isUserInteractionEnabled = false
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
// MP_HUD.loading()
|
||
//发起网络请求补全数据
|
||
MP_NetWorkManager.shared.requestNextList("", videoId: browseViewModel.browseItem.videoId ?? "") { [weak self] listSongs in
|
||
guard let first = listSongs.first else {return}
|
||
let group = DispatchGroup()
|
||
group.enter()
|
||
improveDataforLycirsAndRelated(first) {[weak self] (result) in
|
||
first.lyricsID = result.0
|
||
first.relatedID = result.1
|
||
group.leave()
|
||
}
|
||
group.enter()
|
||
//补全资源路径组和封面路径组
|
||
improveDataforResouceAndCover(first) {[weak self] resourceUrls, coverUrls in
|
||
if let resourceUrls = resourceUrls {
|
||
first.resourceUrls = resourceUrls.0
|
||
first.itags = resourceUrls.1
|
||
first.mimeTypes = resourceUrls.2
|
||
}
|
||
first.coverUrls = coverUrls
|
||
group.leave()
|
||
}
|
||
group.notify(queue: .main, execute: {
|
||
[weak self] in
|
||
//补全了数据
|
||
guard let self = self else {return}
|
||
song = first
|
||
})
|
||
}
|
||
}
|
||
}
|
||
//通过SearchResultItemViewModel加载更多框
|
||
init(_ searchResultItemViewModel:MPPositive_SearchResultItemViewModel) {
|
||
super.init(nibName: nil, bundle: nil)
|
||
view.isUserInteractionEnabled = false
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
// MP_HUD.loading()
|
||
//发起网络请求补全数据
|
||
MP_NetWorkManager.shared.requestNextList("", videoId: searchResultItemViewModel.item.videoId ?? "") { [weak self] listSongs in
|
||
guard let first = listSongs.first else {return}
|
||
let group = DispatchGroup()
|
||
group.enter()
|
||
improveDataforLycirsAndRelated(first) {[weak self] (result) in
|
||
first.lyricsID = result.0
|
||
first.relatedID = result.1
|
||
group.leave()
|
||
}
|
||
group.enter()
|
||
//补全资源路径组和封面路径组
|
||
improveDataforResouceAndCover(first) {[weak self] resourceUrls, coverUrls in
|
||
if let resourceUrls = resourceUrls {
|
||
first.resourceUrls = resourceUrls.0
|
||
first.itags = resourceUrls.1
|
||
first.mimeTypes = resourceUrls.2
|
||
}
|
||
first.coverUrls = coverUrls
|
||
group.leave()
|
||
}
|
||
group.notify(queue: .main, execute: {
|
||
[weak self] in
|
||
//补全了数据
|
||
guard let self = self else {return}
|
||
song = first
|
||
})
|
||
}
|
||
}
|
||
}
|
||
//通过collectionSongViewModel加载更多框
|
||
init(_ collectionSongViewModel:MPPositive_CollectionSongViewModel) {
|
||
super.init(nibName: nil, bundle: nil)
|
||
view.isUserInteractionEnabled = false
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
// MP_HUD.loading()
|
||
//发起网络请求补全数据
|
||
MP_NetWorkManager.shared.requestNextList("", videoId: collectionSongViewModel.collectionSong.videoId ?? ""){ [weak self] listSongs in
|
||
guard let first = listSongs.first else {return}
|
||
let group = DispatchGroup()
|
||
group.enter()
|
||
improveDataforLycirsAndRelated(first) {[weak self] (result) in
|
||
first.lyricsID = result.0
|
||
first.relatedID = result.1
|
||
group.leave()
|
||
}
|
||
group.enter()
|
||
//补全资源路径组和封面路径组
|
||
improveDataforResouceAndCover(first) {[weak self] resourceUrls, coverUrls in
|
||
if let resourceUrls = resourceUrls {
|
||
first.resourceUrls = resourceUrls.0
|
||
first.itags = resourceUrls.1
|
||
first.mimeTypes = resourceUrls.2
|
||
}
|
||
first.coverUrls = coverUrls
|
||
group.leave()
|
||
}
|
||
group.notify(queue: .main, execute: {
|
||
[weak self] in
|
||
//补全了数据
|
||
guard let self = self else {return}
|
||
song = first
|
||
})
|
||
}
|
||
}
|
||
}
|
||
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 MP_PlayerManager.shared.loadPlayer?.currentVideo != nil {
|
||
MP_PlayerManager.shared.loadPlayer?.currentVideo?.reloadCollectionAndDownLoad()
|
||
}
|
||
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
|
||
}
|
||
}
|
||
if MP_PlayerManager.shared.loadPlayer?.currentVideo != nil {
|
||
MP_PlayerManager.shared.loadPlayer?.currentVideo?.reloadCollectionAndDownLoad()
|
||
}
|
||
}
|
||
}
|
||
}
|