后台下载,歌单,歌手,单曲收藏

This commit is contained in:
忆海16 2024-05-22 17:51:35 +08:00
parent c8cf604c59
commit 907142b008
11 changed files with 413 additions and 118 deletions

View File

@ -29,6 +29,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
switch_lunch()
return true
}
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
DownloadManager.shared.session.getAllTasks { tasks in
for task in tasks {
task.resume()
}
}
completionHandler()
}
//
private func setAudioSupport(){
//

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22757" systemVersion="23E224" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22757" systemVersion="23E214" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="MPPositive_CollectionArtistModel" representedClassName="MPPositive_CollectionArtistModel" syncable="YES">
<attribute name="artistId" optional="YES" attributeType="String"/>
<attribute name="coverImage" optional="YES" attributeType="URI"/>
@ -10,7 +10,6 @@
<attribute name="browseId" optional="YES" attributeType="String"/>
<attribute name="coverImage" optional="YES" attributeType="URI"/>
<attribute name="params" optional="YES" attributeType="String"/>
<attribute name="subtitle" optional="YES" attributeType="String"/>
<attribute name="title" optional="YES" attributeType="String"/>
</entity>
<entity name="MPPositive_CollectionSongModel" representedClassName="MPPositive_CollectionSongModel" syncable="YES">

View File

@ -12,7 +12,7 @@ class CircularProgressView: UIView {
private var progressLayer = CAShapeLayer()
private var trackLayer = CAShapeLayer()
private var progress: CGFloat = 0 {
public var progress: CGFloat = 0 {
didSet {
progressLayer.strokeEnd = progress
}
@ -20,36 +20,65 @@ class CircularProgressView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupLayers()
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupLayers()
setupView()
}
private func setupLayers() {
// Track layer (background circle)
let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
let circularPath = UIBezierPath(arcCenter: center, radius: bounds.width / 2, startAngle: -CGFloat.pi / 2, endAngle: 3 * CGFloat.pi / 2, clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.lineWidth = 3
trackLayer.fillColor = UIColor.clear.cgColor
layer.addSublayer(trackLayer)
// Progress layer (foreground circle)
progressLayer.path = circularPath.cgPath
progressLayer.strokeColor = UIColor.green.cgColor
progressLayer.lineWidth = 3
progressLayer.fillColor = UIColor.clear.cgColor
progressLayer.lineCap = .round
progressLayer.strokeEnd = 0
layer.addSublayer(progressLayer)
}
func setProgress(to progress: CGFloat) {
self.progress = progress
}
// private func setupLayers() {
// // Track layer (background circle)
// let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
// let circularPath = UIBezierPath(arcCenter: center, radius: bounds.width / 2, startAngle: -CGFloat.pi / 2, endAngle: 3 * CGFloat.pi / 2, clockwise: true)
//
// trackLayer.path = circularPath.cgPath
// trackLayer.strokeColor = UIColor.lightGray.cgColor
// trackLayer.lineWidth = 3
// trackLayer.fillColor = UIColor.clear.cgColor
// layer.addSublayer(trackLayer)
//
// // Progress layer (foreground circle)
// progressLayer.path = circularPath.cgPath
// progressLayer.strokeColor = UIColor.green.cgColor
// progressLayer.lineWidth = 3
// progressLayer.fillColor = UIColor.clear.cgColor
// progressLayer.lineCap = .round
// progressLayer.strokeEnd = 0
// layer.addSublayer(progressLayer)
// }
//
// func setProgress(to progress: CGFloat) {
// self.progress = progress
// }
private func setupView() {
//
trackLayer.path = createCircularPath().cgPath
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.lineWidth = 3
trackLayer.strokeEnd = 1.0
layer.addSublayer(trackLayer)
//
progressLayer.path = createCircularPath().cgPath
progressLayer.fillColor = UIColor.clear.cgColor
progressLayer.strokeColor = UIColor.green.cgColor
progressLayer.lineWidth = 3
progressLayer.strokeEnd = 0.0
layer.addSublayer(progressLayer)
}
private func createCircularPath() -> UIBezierPath {
return UIBezierPath(arcCenter: center, radius: bounds.size.width / 2, startAngle: -.pi / 2, endAngle: .pi * 3 / 2, clockwise: true)
}
func setProgress(to progress: CGFloat) {
self.progress = progress
print("Updating progress to: \(progress)")
DispatchQueue.main.async {
self.progressLayer.strokeEnd = progress
}
}
}

View File

@ -9,39 +9,110 @@ import Foundation
import Foundation
import Alamofire
class DownloadManager {
class DownloadManager: NSObject, URLSessionDownloadDelegate {
static let shared = DownloadManager()
private init() {}
func downloadVideo(from url: URL, videoId: String, progressView: CircularProgressView, completion: @escaping (Result<URL, Error>) -> Void) {
let destination: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let downloadsURL = documentsURL.appendingPathComponent("Downloads")
// Downloads
if !FileManager.default.fileExists(atPath: downloadsURL.path) {
do {
try FileManager.default.createDirectory(at: downloadsURL, withIntermediateDirectories: true, attributes: nil)
} catch {
completion(.failure(error))
return (downloadsURL, [.removePreviousFile, .createIntermediateDirectories])
}
var session: URLSession!
var progressHandlers: [URL: (CGFloat) -> Void] = [:]
var completionHandlers: [URL: (Result<URL, Error>) -> Void] = [:]
var downloadTasks: [URL: URLSessionDownloadTask] = [:]
var progressStorage: [URL: CGFloat] = [:] //
private override init() {
super.init()
let configuration = URLSessionConfiguration.background(withIdentifier: "com.yourApp.backgroundDownload")
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}
func downloadVideo(from url: URL, videoId: String, progressHandler: @escaping (CGFloat) -> Void, completion: @escaping (Result<URL, Error>) -> Void) {
let downloadTask = session.downloadTask(with: url)
progressHandlers[url] = progressHandler
completionHandlers[url] = completion
downloadTasks[url] = downloadTask
downloadTask.resume()
}
func getProgress(for url: URL) -> CGFloat? {
return progressStorage[url]
}
// URLSessionDownloadDelegate methods
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
guard let originalURL = downloadTask.originalRequest?.url else { return }
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let downloadsURL = documentsURL.appendingPathComponent("Downloads")
if !FileManager.default.fileExists(atPath: downloadsURL.path) {
do {
try FileManager.default.createDirectory(at: downloadsURL, withIntermediateDirectories: true, attributes: nil)
} catch {
completionHandlers[originalURL]?(.failure(error))
return
}
let fileURL = downloadsURL.appendingPathComponent("\(videoId).mp4")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download(url, to: destination).downloadProgress { progress in
progressView.setProgress(to: CGFloat(progress.fractionCompleted))
}.response { response in
if let error = response.error {
completion(.failure(error))
} else if let filePath = response.fileURL {
completion(.success(filePath))
}
let fileURL = downloadsURL.appendingPathComponent("\(MP_PlayerManager.shared.loadPlayer.currentVideo.song.videoId ?? "").mp4")
do {
try FileManager.default.moveItem(at: location, to: fileURL)
completionHandlers[originalURL]?(.success(fileURL))
progressStorage[originalURL] = nil //
} catch {
completionHandlers[originalURL]?(.failure(error))
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
guard let originalURL = downloadTask.originalRequest?.url else { return }
let progress = CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite)
progressHandlers[originalURL]?(progress)
progressStorage[originalURL] = progress //
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
guard let originalURL = task.originalRequest?.url else { return }
if let error = error {
completionHandlers[originalURL]?(.failure(error))
}
}
}
//class DownloadManager {
//
// static let shared = DownloadManager()
//
// private init() {}
//
// func downloadVideo(from url: URL, videoId: String, progressView: CircularProgressView, completion: @escaping (Result<URL, Error>) -> Void) {
// let destination: DownloadRequest.Destination = { _, _ in
// let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
// let downloadsURL = documentsURL.appendingPathComponent("Downloads")
//
// // Downloads
// if !FileManager.default.fileExists(atPath: downloadsURL.path) {
// do {
// try FileManager.default.createDirectory(at: downloadsURL, withIntermediateDirectories: true, attributes: nil)
// } catch {
// completion(.failure(error))
// return (downloadsURL, [.removePreviousFile, .createIntermediateDirectories])
// }
// }
//
// let fileURL = downloadsURL.appendingPathComponent("\(videoId).mp4")
// return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
// }
//
// AF.download(url, to: destination).downloadProgress { progress in
// progressView.setProgress(to: CGFloat(progress.fractionCompleted))
// }.response { response in
// if let error = response.error {
// completion(.failure(error))
// } else if let filePath = response.fileURL {
// completion(.success(filePath))
// }
// }
// }
//}

View File

@ -15,8 +15,6 @@ class MPPositive_CollectionListModel: NSManagedObject, MP_CoreDataManageableDele
@NSManaged var coverImage:URL!
///
@NSManaged var title:String?
///
@NSManaged var subtitle:String?
///ID
@NSManaged var browseId:String?
///

View File

@ -13,5 +13,7 @@ class MPPositive_ListAlbumListViewModel: NSObject {
///
var items:[MPPositive_BrowseItemViewModel] = []
var isCollection:Bool = false
}

View File

@ -64,7 +64,9 @@ class MPPositive_ArtistShowViewController: MPPositive_BaseViewController {
if artist != nil {
pagingView.isHidden = false
//
headView.artistid = self.browseid
headView.artist = artist
//
dataSource.titles = artist.lists.compactMap({$0.title})
nameLabel.text = artist.header?.title
@ -73,11 +75,14 @@ class MPPositive_ArtistShowViewController: MPPositive_BaseViewController {
}
}
}
var browseid:String = ""
var iscollection:Bool = false
///
/// - Parameter browseId: Id
init(_ browseId:String) {
super.init(nibName: nil, bundle: nil)
browseid = browseId
//
MP_NetWorkManager.shared.requestArtist(browseId) { [weak self] result in
DispatchQueue.main.async {
@ -97,6 +102,7 @@ class MPPositive_ArtistShowViewController: MPPositive_BaseViewController {
setTitle("")
configure()
}
private func configure() {
segmentView.dataSource = dataSource
segmentView.indicators = [indicator]

View File

@ -81,16 +81,21 @@ class MPPositive_ListShowViewController: MPPositive_BaseViewController {
}
}
var browseid:String = ""
var params:String = ""
///
/// - Parameters:
/// - browseId: id
/// - params:
init(_ browseId:String, params:String) {
super.init(nibName: nil, bundle: nil)
self.browseid = browseId
self.params = params
//
MP_NetWorkManager.shared.requestAlbumOrListDatas(browseId, params: params) { [weak self] result in
guard let self = self else {return}
listOrAlbum = result
}
}
@ -105,6 +110,13 @@ class MPPositive_ListShowViewController: MPPositive_BaseViewController {
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
MPPositive_CollectionListModel.fetch(.init(format: "browseId == %@", self.browseid)).forEach { i in
if i.browseId == self.browseid{
if i.params == self.params{
self.collectionListBtn.isSelected = true
}
}
}
}
//
private func reload() {
@ -222,7 +234,27 @@ class MPPositive_ListShowViewController: MPPositive_BaseViewController {
}
//
@objc private func collectionSwitchClick(_ sender:UIButton) {
if self.collectionListBtn.isSelected == true{
self.collectionListBtn.isSelected = false
MPPositive_CollectionListModel.fetch(.init(format: "browseId == %@", self.browseid)).forEach { i in
if i.browseId == self.browseid{
if i.params == self.params{
MPPositive_CollectionListModel.delete(i)
}
}
}
}else{
self.collectionListBtn.isSelected = true
let item = MPPositive_CollectionListModel.create()
item.title = listOrAlbum.header.title
item.browseId = self.browseid
item.params = self.params
item.coverImage = listOrAlbum.header.coverUrl
MPPositive_CollectionListModel.save()
}
}
//
@objc private func collectionStatuClick(_ sender:UIButton) {

View File

@ -165,6 +165,7 @@ class MPPositive_PlayerViewController: MPPositive_BaseViewController, UIViewCont
if MP_PlayerManager.shared.loadPlayer.currentVideo != nil {
uploadUI()
}
coverView.restoreDownloadProgress()
}
//
private func configure() {

View File

@ -14,6 +14,8 @@ class MPPositive_ArtistShowHeaderView: UIView {
imageView.contentMode = .scaleAspectFill
return imageView
}()
var artistid:String = ""
var iscollection:Bool = false
//
private lazy var nameLabel:UILabel = createLabel(font: .systemFont(ofSize: 32*width, weight: .semibold), textColor: .white, textAlignment: .left)
//label
@ -40,16 +42,26 @@ class MPPositive_ArtistShowHeaderView: UIView {
reviewImageView.kf.setImage(with: URL(string: artist.header?.thumbnails?.last ?? ""), placeholder: placeholderImage)
nameLabel.text = artist.header?.title
followersLabel.text = (artist.header?.subscriptions ?? "")+" "+(artist.header?.subscriptionedText ?? "")
let items = MPPositive_CollectionArtistModel.fetch(.init(format: "artistId == %@", self.artistid))
for item in items {
if item.artistId == self.artistid {
self.collectionBtn.isSelected = true
}
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
configure()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
private func configure() {
addSubview(reviewImageView)
@ -78,9 +90,36 @@ class MPPositive_ArtistShowHeaderView: UIView {
make.left.right.equalTo(nameLabel)
make.bottom.equalTo(collectionBtn)
}
}
//
@objc private func collectionClick(_ sender:UIButton) {
if self.collectionBtn.isSelected == true{
self.collectionBtn.isSelected = false
// MPPositive_CollectionArtistModel.fetch(.init(format: "artistId == %@", self.browseid)).forEach { i in
// if i.artistId == self.browseid{
// MPPositive_CollectionArtistModel.delete(i)
// }
// }
let items = MPPositive_CollectionArtistModel.fetch(.init(format: "artistId == %@", self.artistid))
for item in items {
if item.artistId == self.artistid {
MPPositive_CollectionArtistModel.delete(item)
}
}
}else{
self.collectionBtn.isSelected = true
let item = MPPositive_CollectionArtistModel.create()
item.title = artist.header.title
item.coverImage = URL(string: artist.header.thumbnails?.last ?? "")
item.subtitle = artist.header.subscriptionedText
item.artistId = self.artistid
MPPositive_CollectionArtistModel.save()
}
}
}

View File

@ -1,12 +1,9 @@
//
// MPPositive_PlayerCoverView.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/5/8.
//
import UIKit
import SVProgressHUD
//BView(View)
class MPPositive_PlayerCoverView: UIView {
//View
@ -61,15 +58,64 @@ class MPPositive_PlayerCoverView: UIView {
///Label
lazy var maxTimesLabel:UILabel = createLabel("00:00" ,font: .systemFont(ofSize: 12*width, weight: .medium), textColor: .init(hex: "#FFFFFF", alpha: 0.6), textAlignment: .right)
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
configure()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
// NotificationCenter.default.addObserver(self, selector: #selector(updateProgress(_:)), name: Notification.Name("DownloadProgressUpdated"), object: nil)
//
// //
// restoreDownloadProgress()
}
public func restoreDownloadProgress() {
if let currentVideo = MP_PlayerManager.shared.loadPlayer.currentVideo,
let videoURLString = currentVideo.song.resourceUrls?.first,
let videoURL = URL(string: videoURLString) {
if let progress = DownloadManager.shared.getProgress(for: videoURL) {
addCircularProgressBar(over: loadBtn)
loadView.setProgress(to: progress)
// DispatchQueue.main.asyncAfter(deadline: .now() + 2) { // 0.1
self.layoutIfNeeded()
self.loadBtn.setBackgroundImage(UIImage(named: ""), for: .normal)
self.loadBtn.setImage(UIImage(named: "download"), for: .normal)
self.addCircularProgressBar(over: self.loadBtn)
self.loadView.setProgress(to: progress)
// }
}
}
}
@objc private func updateProgress(_ notification: Notification) {
if let userInfo = notification.userInfo,
let url = userInfo["url"] as? URL,
let progress = userInfo["progress"] as? CGFloat,
let currentVideo = MP_PlayerManager.shared.loadPlayer.currentVideo,
let videoURLString = currentVideo.song.resourceUrls?.first,
let videoURL = URL(string: videoURLString),
videoURL == url {
loadView.setProgress(to: progress)
if loadView.progress.isEqual(to: 1.0){
self.loadView.removeFromSuperview()
self.loadBtn.setBackgroundImage(UIImage(named: "Song_Loaded'logo"), for: .normal)
self.loadBtn.setImage(UIImage(named: ""), for: .normal)
}
}
}
//
private func configure() {
//
@ -127,6 +173,10 @@ class MPPositive_PlayerCoverView: UIView {
make.right.equalTo(sliderView.snp.right)
make.top.equalTo(sliderView.snp.bottom).offset(5*width)
}
NotificationCenter.default.addObserver(self, selector: #selector(updateProgress(_:)), name: Notification.Name("DownloadProgressUpdated"), object: nil)
//
restoreDownloadProgress()
}
//
@objc private func seekProgressClick(_ sender: UISlider, forEvent event: UIEvent) {
@ -172,63 +222,118 @@ class MPPositive_PlayerCoverView: UIView {
}
}
}
//
@objc private func loadActionClick(_ sender:UIButton) {
if MP_PlayerManager.shared.loadPlayer.currentVideo?.isDlownd == false{
//
addCircularProgressBar(over: sender)
@objc private func loadActionClick(_ sender: UIButton) {
if MP_PlayerManager.shared.loadPlayer.currentVideo?.isDlownd == false {
addCircularProgressBar(over: sender)
self.loadBtn.setBackgroundImage(UIImage(named: ""), for: .normal)
self.loadBtn.setImage(UIImage(named: "download"), for: .normal)
//
if MP_PlayerManager.shared.loadPlayer.currentVideo != nil {
self.loadBtn.setBackgroundImage(UIImage(named: ""), for: .normal)
self.loadBtn.setImage(UIImage(named: "download"), for: .normal)
//
if let currentVideo = MP_PlayerManager.shared.loadPlayer.currentVideo, let videoURLString = currentVideo.song.resourceUrls?.first, let videoURL = URL(string: videoURLString) {
let videoId = currentVideo.song.videoId ?? "default_video_id"
DownloadManager.shared.downloadVideo(from: videoURL, videoId: videoId, progressView: loadView, completion: { [weak self] result in
switch result {
case .success(let fileURL):
let item = MPPositive_DownloadItemModel.create()
item.resourcePath = "\(fileURL)"
item.coverImage = URL(string: MP_PlayerManager.shared.loadPlayer.currentVideo.song.coverUrls!.first!)
item.reviewImage = URL(string: MP_PlayerManager.shared.loadPlayer.currentVideo.song.reviewUrls!.first!)
item.title = MP_PlayerManager.shared.loadPlayer.currentVideo.song.title
item.longBylineText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.longBylineText
item.lengthText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.lengthText
item.shortBylineText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.shortBylineText
item.lyrics = MP_PlayerManager.shared.loadPlayer.currentVideo.lyrics
item.videoId = MP_PlayerManager.shared.loadPlayer.currentVideo.song.videoId
item.relatedID = MP_PlayerManager.shared.loadPlayer.currentVideo.song.relatedID
MPPositive_DownloadItemModel.save()
DispatchQueue.main.async {
MP_PlayerManager.shared.loadPlayer.currentVideo.reloadCollectionAndDownLoad()
self?.loadBtn.setBackgroundImage(UIImage(named: "Song_Loaded'logo"), for: .normal)
self?.loadBtn.setImage(UIImage(named: ""), for: .normal)
}
self?.loadView.removeFromSuperview()
case .failure(let error):
print("Download failed with error: \(error)")
self?.loadView.removeFromSuperview()
DispatchQueue.main.async {
self?.loadBtn.setBackgroundImage(UIImage(named: "Song_Unload'logo"), for: .normal)
self?.loadBtn.setImage(UIImage(named: ""), for: .normal)
}
MP_HUD.text("下载超时请重新下载", delay: 1.5, completion: nil)
}
})
}
if let currentVideo = MP_PlayerManager.shared.loadPlayer.currentVideo,
let videoURLString = currentVideo.song.resourceUrls?.first,
let videoURL = URL(string: videoURLString) {
let videoId = currentVideo.song.videoId ?? "default_video_id"
DownloadManager.shared.downloadVideo(from: videoURL, videoId: videoId, progressHandler: { [weak self] progress in
DispatchQueue.main.async {
self?.loadView.setProgress(to: progress)
NotificationCenter.default.post(name: Notification.Name("DownloadProgressUpdated"), object: nil, userInfo: ["url": videoURL, "progress": progress])
}
}, completion: { [weak self] result in
switch result {
case .success(let fileURL):
let item = MPPositive_DownloadItemModel.create()
item.resourcePath = "\(fileURL)"
item.coverImage = URL(string: MP_PlayerManager.shared.loadPlayer.currentVideo.song.coverUrls!.first!)
item.reviewImage = URL(string: MP_PlayerManager.shared.loadPlayer.currentVideo.song.reviewUrls!.first!)
item.title = MP_PlayerManager.shared.loadPlayer.currentVideo.song.title
item.longBylineText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.longBylineText
item.lengthText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.lengthText
item.shortBylineText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.shortBylineText
item.lyrics = MP_PlayerManager.shared.loadPlayer.currentVideo.lyrics
item.videoId = MP_PlayerManager.shared.loadPlayer.currentVideo.song.videoId
item.relatedID = MP_PlayerManager.shared.loadPlayer.currentVideo.song.relatedID
MPPositive_DownloadItemModel.save()
DispatchQueue.main.async {
self?.loadView.removeFromSuperview()
MP_PlayerManager.shared.loadPlayer.currentVideo.reloadCollectionAndDownLoad()
self?.loadBtn.setBackgroundImage(UIImage(named: "Song_Loaded'logo"), for: .normal)
self?.loadBtn.setImage(UIImage(named: ""), for: .normal)
}
case .failure(let error):
print("Download failed with error: \(error)")
DispatchQueue.main.async {
self?.loadView.removeFromSuperview()
self?.loadBtn.setBackgroundImage(UIImage(named: "Song_Unload'logo"), for: .normal)
self?.loadBtn.setImage(UIImage(named: ""), for: .normal)
}
MP_HUD.text("Download timed out, please download again", delay: 1.5, completion: nil)
}
})
}
}
}
}
//
private func addCircularProgressBar(over button: UIButton) {
// //
// @objc private func loadActionClick(_ sender:UIButton) {
//
// if MP_PlayerManager.shared.loadPlayer.currentVideo?.isDlownd == false{
//
// //
// addCircularProgressBar(over: sender)
//
// self.loadBtn.setBackgroundImage(UIImage(named: ""), for: .normal)
// self.loadBtn.setImage(UIImage(named: "download"), for: .normal)
//
// //
// if MP_PlayerManager.shared.loadPlayer.currentVideo != nil {
//
// //
// if let currentVideo = MP_PlayerManager.shared.loadPlayer.currentVideo, let videoURLString = currentVideo.song.resourceUrls?.first, let videoURL = URL(string: videoURLString) {
// let videoId = currentVideo.song.videoId ?? "default_video_id"
// DownloadManager.shared.downloadVideo(from: videoURL, videoId: videoId, progressView: loadView, completion: { [weak self] result in
// switch result {
// case .success(let fileURL):
// let item = MPPositive_DownloadItemModel.create()
// item.resourcePath = "\(fileURL)"
// item.coverImage = URL(string: MP_PlayerManager.shared.loadPlayer.currentVideo.song.coverUrls!.first!)
// item.reviewImage = URL(string: MP_PlayerManager.shared.loadPlayer.currentVideo.song.reviewUrls!.first!)
// item.title = MP_PlayerManager.shared.loadPlayer.currentVideo.song.title
// item.longBylineText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.longBylineText
// item.lengthText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.lengthText
// item.shortBylineText = MP_PlayerManager.shared.loadPlayer.currentVideo.song.shortBylineText
// item.lyrics = MP_PlayerManager.shared.loadPlayer.currentVideo.lyrics
// item.videoId = MP_PlayerManager.shared.loadPlayer.currentVideo.song.videoId
// item.relatedID = MP_PlayerManager.shared.loadPlayer.currentVideo.song.relatedID
//
// MPPositive_DownloadItemModel.save()
// DispatchQueue.main.async {
// MP_PlayerManager.shared.loadPlayer.currentVideo.reloadCollectionAndDownLoad()
// self?.loadBtn.setBackgroundImage(UIImage(named: "Song_Loaded'logo"), for: .normal)
// self?.loadBtn.setImage(UIImage(named: ""), for: .normal)
// }
//
// self?.loadView.removeFromSuperview()
// case .failure(let error):
// print("Download failed with error: \(error)")
// self?.loadView.removeFromSuperview()
// DispatchQueue.main.async {
// self?.loadBtn.setBackgroundImage(UIImage(named: "Song_Unload'logo"), for: .normal)
// self?.loadBtn.setImage(UIImage(named: ""), for: .normal)
// }
// MP_HUD.text("Download timed out, please download again", delay: 1.5, completion: nil)
// }
// })
// }
// }
// }
//
// }
// //
private func addCircularProgressBar(over button: UIButton) {
loadView.removeFromSuperview() //
loadView = CircularProgressView(frame: button.bounds)
@ -238,4 +343,6 @@ class MPPositive_PlayerCoverView: UIView {
make.width.height.equalTo(button)
}
}
}