Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Base/MPPositive_MoreOperationDownLoadTableViewCell.swift
2024-10-08 17:34:04 +08:00

158 lines
6.9 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_MoreOperationTableViewCell.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/5/29.
//
import UIKit
import DownloadButton
class MPPositive_MoreOperationDownLoadTableViewCell: UITableViewCell {
//
lazy var loadBtn:MP_DownloadButton = {
let btn:MP_DownloadButton = .init()
btn.isUserInteractionEnabled = false
//
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.isUserInteractionEnabled = false
btn.downloadedButton.setAttributedTitle(nil, for: .normal)
//
btn.stopDownloadButton.stopButton.setImage(UIImage(named: "download"), for: .normal)
btn.stopDownloadButton.isUserInteractionEnabled = false
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
return btn
}()
var song:MPPositive_SongItemModel!{
didSet{
setProgress(song.videoId ?? "")
}
}
private lazy var titleLabel:UILabel = createLabel("Status" ,font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .white, textAlignment: .left)
//videoId
func setProgress(_ videoId:String) {
guard videoId.isEmpty == false, videoId == song?.videoId else {
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.loadBtn.state = .downloading
self.titleLabel.text = "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.loadBtn.stopDownloadButton.progress = progress
}
}
}
}else {
//
DispatchQueue.main.async {
//
self.loadBtn.state = .pending
self.titleLabel.text = "Downloading"
}
}
}
}else {
DispatchQueue.main.async {
//
self.loadBtn.state = .startDownload
self.titleLabel.text = "Download"
}
}
}else {
DispatchQueue.main.async {
//
self.loadBtn.state = .downloaded
self.titleLabel.text = "Remove download"
}
}
}
}
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
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)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
deinit {
NotificationCenter.default.removeObserver(self)
}
//progress
@objc private func downloadProgressAction(_ sender:Notification) {
if let dict = sender.object as? [String:String], let videoId = dict["videoId"], let currentVideoId = song?.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 = song?.videoId, videoId == currentVideoId {
setProgress(videoId)
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
private func configure() {
contentView.addSubview(loadBtn)
loadBtn.snp.makeConstraints { make in
make.width.height.equalTo(24*width)
make.top.equalToSuperview().offset(12*width).priority(999)
make.bottom.equalToSuperview().offset(-12*width)
make.left.equalToSuperview().offset(18*width)
}
contentView.addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.left.equalTo(loadBtn.snp.right).offset(12*width)
}
}
}