298 lines
13 KiB
Swift
298 lines
13 KiB
Swift
//
|
||
// MPPositive_HomeSingleCollectionViewCell.swift
|
||
// relax.offline.mp3.music
|
||
//
|
||
// Created by Mr.Zhou on 2024/7/4.
|
||
//
|
||
|
||
import UIKit
|
||
import DownloadButton
|
||
class MPPositive_HomeSingleCollectionViewCell: UICollectionViewCell, PKDownloadButtonDelegate {
|
||
///封面图片
|
||
private lazy var coverImageView:UIImageView = {
|
||
let imageView:UIImageView = .init()
|
||
imageView.contentMode = .scaleAspectFill
|
||
imageView.layer.masksToBounds = true
|
||
imageView.layer.cornerRadius = 10*width
|
||
return imageView
|
||
}()
|
||
private lazy var maskImageView:MP_WaveAnimationMaskView = .init(frame: .init(x: 0, y: 0, width: 24*width, height: 24*width), cornerRadius: 10*width)
|
||
///标题
|
||
private lazy var titleLabel:UILabel = createLabel(font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .white, textAlignment: .left)
|
||
///副标题
|
||
private lazy var subtitleLabel:UILabel = createLabel(font: .systemFont(ofSize: 12*width, weight: .regular), textColor: .init(hex: "#FFFFFF", alpha: 0.6), textAlignment: .left)
|
||
///更多按钮
|
||
private lazy var moreBtn:UIButton = {
|
||
let btn:UIButton = .init()
|
||
btn.setBackgroundImage(UIImage(named: "Song_More'logo"), for: .normal)
|
||
btn.addTarget(self, action: #selector(moreActionClick(_ :)), for: .touchUpInside)
|
||
return btn
|
||
}()
|
||
///下载按钮
|
||
private lazy var dowloadBtn:MP_DownloadButton = {
|
||
let btn:MP_DownloadButton = .init()
|
||
//开始下载状态
|
||
btn.startDownloadButton.cleanDefaultAppearance()
|
||
btn.startDownloadButton.setBackgroundImage(UIImage(named: "Song_Unload'logo"), for: .normal)
|
||
//下载结束状态
|
||
btn.downloadedButton.setBackgroundImage(UIImage(named: "Song_Loaded'logo"), for: .normal)
|
||
btn.downloadedButton.setAttributedTitle(nil, for: .normal)
|
||
//停止下载状态
|
||
btn.stopDownloadButton.stopButton.setImage(UIImage(named: "download"), for: .normal)
|
||
btn.stopDownloadButton.tintColor = UIColor(hex: "#80F988")
|
||
btn.stopDownloadButton.stopButtonWidth = 2
|
||
btn.stopDownloadButton.filledLineWidth = 3*width
|
||
btn.stopDownloadButton.filledLineStyleOuter = true
|
||
|
||
//加载状态设置
|
||
btn.pendingView.tintColor = UIColor(hex: "#80F988")
|
||
btn.pendingView.radius = 12*width
|
||
btn.pendingView.emptyLineRadians = 2*width
|
||
btn.pendingView.spinTime = 3
|
||
btn.delegate = self
|
||
return btn
|
||
}()
|
||
var moreBlock:(() -> Void)?
|
||
//删除
|
||
var deleteBlock:(() -> Void)?
|
||
//取消
|
||
var cancelBlock:(() -> Void)?
|
||
var itemViewModel:MPPositive_BrowseItemViewModel!{
|
||
didSet{
|
||
itemViewModel?.setUrltoImage(coverImageView)
|
||
titleLabel.text = itemViewModel?.title
|
||
subtitleLabel.text = itemViewModel?.subtitle
|
||
setProgress(itemViewModel?.browseItem.videoId ?? "")
|
||
if (itemViewModel?.browseItem.videoId == MP_PlayerManager.shared.loadPlayer?.currentVideo?.song?.videoId) {
|
||
titleLabel.textColor = greenTextColor
|
||
maskImageView.isHidden = false
|
||
maskImageView.startAnimation()
|
||
}else {
|
||
titleLabel.textColor = .white
|
||
maskImageView.isHidden = true
|
||
maskImageView.stopAnimation()
|
||
}
|
||
}
|
||
}
|
||
override init(frame: CGRect) {
|
||
super.init(frame: frame)
|
||
//注册通知
|
||
NotificationCenter.notificationKey.add(observer: self, selector: #selector(downloadProgressAction(_ :)), notificationName: .download_progress_source)
|
||
NotificationCenter.notificationKey.add(observer: self, selector: #selector(downloadEndAction(_ :)), notificationName: .dowload_end_source)
|
||
confirgue()
|
||
}
|
||
deinit {
|
||
NotificationCenter.default.removeObserver(self)
|
||
}
|
||
required init?(coder: NSCoder) {
|
||
super.init(coder: coder)
|
||
}
|
||
private func confirgue() {
|
||
backgroundColor = .clear
|
||
layer.masksToBounds = true
|
||
layer.cornerRadius = 16*width
|
||
addSubview(coverImageView)
|
||
coverImageView.snp.makeConstraints { make in
|
||
make.width.height.equalTo(58*width)
|
||
make.left.equalToSuperview()
|
||
make.centerY.equalToSuperview()
|
||
}
|
||
addSubview(maskImageView)
|
||
maskImageView.snp.makeConstraints { make in
|
||
make.width.height.equalTo(coverImageView)
|
||
make.center.equalTo(coverImageView)
|
||
}
|
||
maskImageView.configure()
|
||
maskImageView.isHidden = true
|
||
maskImageView.stopAnimation()
|
||
addSubview(moreBtn)
|
||
moreBtn.snp.makeConstraints { make in
|
||
make.width.height.equalTo(24*width)
|
||
make.centerY.equalTo(coverImageView.snp.centerY)
|
||
make.right.equalToSuperview().offset(-5*width)
|
||
}
|
||
addSubview(dowloadBtn)
|
||
dowloadBtn.snp.makeConstraints { make in
|
||
make.width.height.equalTo(24*width)
|
||
make.centerY.equalTo(moreBtn.snp.centerY)
|
||
make.right.equalTo(moreBtn.snp.left).offset(-8*width)
|
||
}
|
||
addSubview(titleLabel)
|
||
titleLabel.snp.makeConstraints { make in
|
||
make.top.equalTo(coverImageView.snp.top).offset(10*width)
|
||
make.left.equalTo(coverImageView.snp.right).offset(12*width)
|
||
make.right.equalTo(dowloadBtn.snp.left).offset(-12*width)
|
||
}
|
||
addSubview(subtitleLabel)
|
||
subtitleLabel.snp.makeConstraints { make in
|
||
make.bottom.equalTo(coverImageView.snp.bottom).offset(-10*width)
|
||
make.left.right.equalTo(titleLabel)
|
||
}
|
||
}
|
||
//收到progress加载
|
||
@objc private func downloadProgressAction(_ sender:Notification) {
|
||
if let dict = sender.object as? [String:String], let videoId = dict["videoId"], let currentVideoId = itemViewModel?.browseItem.videoId, videoId == currentVideoId {
|
||
//比较当前videoId
|
||
setProgress(videoId)
|
||
}
|
||
}
|
||
//收到下载结束
|
||
@objc private func downloadEndAction(_ sender:Notification) {
|
||
if let dict = sender.object as? [String:String], let videoId = dict["videoId"], let currentVideoId = itemViewModel?.browseItem.videoId, videoId == currentVideoId {
|
||
setProgress(videoId)
|
||
}
|
||
}
|
||
//点击更多
|
||
@objc private func moreActionClick(_ sender:UIButton) {
|
||
guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
|
||
MP_HUD.text("Bad connection~".localizableString(), delay: 2.0, completion: nil)
|
||
return
|
||
}
|
||
guard moreBlock != nil else {
|
||
return
|
||
}
|
||
moreBlock!()
|
||
}
|
||
//根据输入videoId检索是否下载
|
||
func setProgress(_ videoId:String) {
|
||
guard videoId.isEmpty == false, videoId == itemViewModel?.browseItem.videoId else {
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
//未加入下载队列
|
||
self.dowloadBtn.state = .startDownload
|
||
}
|
||
return
|
||
}
|
||
MP_DownloadManager.shared.loadQueue.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
//判断是否下载完成
|
||
MP_DownloadManager.shared.isDownloadedFileDocuments(videoId) {[weak self] statu in
|
||
guard let self = self else {return}
|
||
if statu == false {
|
||
//未下载,判断是否加入了下载队列
|
||
if MP_DownloadManager.shared.isTasksQueue(for: videoId) {
|
||
//加入下载队列,确定是否活跃下中
|
||
MP_DownloadManager.shared.isActiveTask(for: videoId){
|
||
[weak self] status in
|
||
guard let self = self else {return}
|
||
if status {
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
self.dowloadBtn.state = .downloading
|
||
}
|
||
//活跃中,获取下载值
|
||
MP_DownloadManager.shared.getProgress(for: videoId) {
|
||
[weak self] value in
|
||
if let progress = value {
|
||
//下载中
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
self.dowloadBtn.stopDownloadButton.progress = progress
|
||
}
|
||
}
|
||
}
|
||
}else {
|
||
//处于队列中,还未开始下载
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
//未加入下载队列
|
||
self.dowloadBtn.state = .pending
|
||
}
|
||
}
|
||
}
|
||
}else {
|
||
//未加入下载队列
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
//未加入下载队列
|
||
self.dowloadBtn.state = .startDownload
|
||
}
|
||
}
|
||
}else {
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
//已经完成下载
|
||
self.dowloadBtn.state = .downloaded
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//下载按钮代理
|
||
func downloadButtonTapped(_ downloadButton: PKDownloadButton!, currentState state: PKDownloadButtonState) {
|
||
var video:MPPositive_SongItemModel!
|
||
//根据当前按钮状态确定进度
|
||
switch state {
|
||
case .startDownload://开始状态,点击后进入下载状态
|
||
//检索当前网络状态
|
||
// guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
|
||
// MP_HUD.text("Bad connection~".localizableString(), delay: 2.0, completion: nil)
|
||
// return
|
||
// }
|
||
//检索当前歌曲是否下载过
|
||
guard let videoId = itemViewModel?.browseItem.videoId else {
|
||
return
|
||
}
|
||
MP_ADSimpleManager.shared.showLibraryInterstitialAdIfAvailable(completion: nil)
|
||
//切换为准备状态
|
||
downloadButton.state = .pending
|
||
//通过网络请求获取当前预览项资源(转为Song)
|
||
MP_NetWorkManager.shared.requestNextList(itemViewModel?.browseItem.playListId ?? "", videoId: videoId, clickTrackingParams: nil){
|
||
[weak self] listSongs in
|
||
guard let self = self, let first = listSongs.first else {
|
||
//获取资源失败
|
||
self?.setProgress(videoId)
|
||
return
|
||
}
|
||
let group = DispatchGroup()
|
||
group.enter()
|
||
improveDataforLycirsAndRelated(first) {(result) in
|
||
first.lyricsID = result.0
|
||
first.relatedID = result.1
|
||
group.leave()
|
||
}
|
||
group.enter()
|
||
//补全资源路径组和封面路径组
|
||
improveDataforResouceAndCover(first) {(resourceUrls, coverUrls) in
|
||
if let resourceUrls = resourceUrls {
|
||
first.resourceUrls = resourceUrls.0
|
||
first.itags = resourceUrls.1
|
||
first.mimeTypes = resourceUrls.2
|
||
}
|
||
first.coverUrls = coverUrls
|
||
group.leave()
|
||
} failure: {_ in
|
||
group.leave()
|
||
}
|
||
group.notify(queue: .main, execute: {
|
||
[weak self] in
|
||
guard let self = self else {
|
||
self?.setProgress(videoId)
|
||
return
|
||
}
|
||
video = first
|
||
//事件打点
|
||
MP_AnalyticsManager.shared.player_b_download_clickAction(video.videoId ?? "", videoname: video.title ?? "", artistname: video.shortBylineText ?? "")
|
||
//补全了数据,执行下载
|
||
MP_DownloadManager.shared.prepareVideoDownloadTask(from: video)
|
||
})
|
||
}
|
||
case .downloaded://已下载
|
||
if let block = deleteBlock {
|
||
block()
|
||
}
|
||
default:
|
||
if let block = cancelBlock {
|
||
block()
|
||
}
|
||
}
|
||
}
|
||
}
|