Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Home/MPPositive_HomeSingleCollectionViewCell.swift
2024-08-19 14:40:41 +08:00

288 lines
12 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_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: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 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().offset(18*width)
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) {
//
if MP_DownloadManager.shared.isActiveTask(for: videoId) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
self.dowloadBtn.state = .downloading
}
//
if let progress = MP_DownloadManager.shared.getProgress(for: videoId) {
//
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
}
//
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()
}
}
}
}