Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Home/MPPositive_MusicItemShowTableViewCell.swift
2024-08-15 15:50:48 +08:00

309 lines
13 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_MusicItemShowTableViewCell.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/4/29.
//
import UIKit
import DownloadButton
///bCell
class MPPositive_MusicItemShowTableViewCell: UITableViewCell, 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
}()
//Label
private lazy var rankLabel:UILabel = createLabel(font: .systemFont(ofSize: 14*width, weight: .semibold), textColor: .white, textAlignment: .center)
///
//
private lazy var loadBtn:PKDownloadButton = {
let btn:PKDownloadButton = .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 itemView:MPPositive_BrowseItemViewModel!{
didSet{
itemView.setUrltoImage(coverImageView)
titleLabel.text = itemView.title
subtitleLabel.text = itemView.subtitle
rankLabel.isHidden = (itemView.coverUrl != nil)
setProgress(itemView.browseItem.videoId ?? "")
if (itemView?.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()
}
}
}
var rank:Int = 0 {
didSet{
rankLabel.text = "\(rank)"
}
}
var moreBlock:(() -> Void)?
//
var deleteBlock:(() -> Void)?
//
var cancelBlock:(() -> Void)?
//videoId
func setProgress(_ videoId:String) {
guard videoId.isEmpty == false, videoId == itemView?.browseItem.videoId else {
DispatchQueue.main.async {
//
self.loadBtn.state = .startDownload
}
return
}
MP_DownloadManager.shared.loadQueue.async {
[weak self] in
guard let self = self else {return}
//
MP_DownloadManager.shared.isDownloadedFileDocuments(videoId) { statu in
if statu == false {
//,
if MP_DownloadManager.shared.isTasksQueue(for: videoId) {
//
if MP_DownloadManager.shared.isActiveTask(for: videoId) {
DispatchQueue.main.async {
self.loadBtn.state = .downloading
}
//
if let progress = MP_DownloadManager.shared.getProgress(for: videoId) {
//
DispatchQueue.main.async {
self.loadBtn.stopDownloadButton.progress = progress
}
}
}else {
//
DispatchQueue.main.async {
//
self.loadBtn.state = .pending
}
}
}else {
//
DispatchQueue.main.async {
//
self.loadBtn.state = .startDownload
}
}
}else {
DispatchQueue.main.async {
//
self.loadBtn.state = .downloaded
}
}
}
}
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
backgroundColor = .clear
contentView.backgroundColor = .clear
//
NotificationCenter.notificationKey.add(observer: self, selector: #selector(downloadProgressAction(_ :)), notificationName: .download_progress_source)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(downloadEndAction(_ :)), notificationName: .dowload_end_source)
configure()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
super.prepareForReuse()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
//progress
@objc private func downloadProgressAction(_ sender:Notification) {
if let dict = sender.object as? [String:String], let videoId = dict["videoId"], let currentVideoId = itemView?.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 = itemView?.browseItem.videoId, videoId == currentVideoId {
setProgress(videoId)
}
}
private func configure() {
contentView.addSubview(coverImageView)
//cell
coverImageView.snp.makeConstraints { make in
make.width.height.equalTo(60*width)
make.left.equalToSuperview().offset(18*width)
make.top.equalToSuperview().offset(8*width).priority(999)
make.bottom.equalToSuperview().offset(-8*width).priority(999)
}
contentView.addSubview(maskImageView)
maskImageView.snp.makeConstraints { make in
make.width.height.equalTo(coverImageView)
make.center.equalTo(coverImageView)
}
maskImageView.configure()
maskImageView.isHidden = true
maskImageView.stopAnimation()
contentView.addSubview(rankLabel)
rankLabel.snp.makeConstraints { make in
make.center.equalTo(coverImageView)
make.width.equalTo(coverImageView)
}
contentView.addSubview(moreBtn)
moreBtn.snp.makeConstraints { make in
make.width.height.equalTo(24*width)
make.centerY.equalTo(coverImageView.snp.centerY)
make.right.equalToSuperview().offset(-18*width)
}
contentView.addSubview(loadBtn)
loadBtn.snp.makeConstraints { make in
make.width.height.equalTo(24*width)
make.centerY.equalTo(coverImageView.snp.centerY)
make.right.equalToSuperview().offset(-54*width)
}
contentView.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(loadBtn.snp.left).offset(-12*width)
}
contentView.addSubview(subtitleLabel)
subtitleLabel.snp.makeConstraints { make in
make.bottom.equalTo(coverImageView.snp.bottom).offset(-10*width)
make.left.right.equalTo(titleLabel)
}
}
//
@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!()
}
//
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 = itemView.browseItem.videoId else {
return
}
//
downloadButton.state = .pending
//(Song)
MP_NetWorkManager.shared.requestNextList("", 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()
}
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()
}
}
}
}