Music_Player3/MusicPlayer/MP/MPPositive/Views/Player/MPPositive_PlayerCoverView.swift
2024-05-21 15:33:40 +08:00

242 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_PlayerCoverView.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/5/8.
//
import UIKit
import SVProgressHUD
//BView(View)
class MPPositive_PlayerCoverView: UIView {
//View
private var loadView = CircularProgressView()
///
lazy var coverImageView:UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 16*width
return imageView
}()
///
lazy var titleLabel:UILabel = createLabel(font: .systemFont(ofSize: 22*width, weight: .regular), textColor: .init(hex: "#FFFFFF", alpha: 0.85), textAlignment: .left)
///
lazy var subtitleLabel:UILabel = createLabel(font: .systemFont(ofSize: 12*width, weight: .regular), textColor: .init(hex: "#EEEEEE", alpha: 0.6), textAlignment: .left)
///
lazy var collectionSongBtn:UIButton = {
let btn = UIButton()
btn.setBackgroundImage(UIImage(named: "List_UnCollection'logo"), for: .normal)
btn.setBackgroundImage(UIImage(named: "List_Collectioned'logo"), for: .selected)
btn.addTarget(self, action: #selector(collectionSwitchClick(_ :)), for: .touchUpInside)
return btn
}()
///
lazy var loadBtn:UIButton = {
let btn:UIButton = .init()
btn.setBackgroundImage(UIImage(named: "Song_Unload'logo"), for: .normal)
btn.setBackgroundImage(UIImage(named: "Song_Loaded'logo"), for: .selected)
btn.addTarget(self, action: #selector(loadActionClick(_ :)), for: .touchUpInside)
return btn
}()
///View
lazy var sliderView:MPPositive_PlayerSilder = {
let sliderView:MPPositive_PlayerSilder = .init(frame: .init(x: 0, y: 0, width: 335*width, height: 6*width))
sliderView.addTarget(self, action: #selector(seekProgressClick(_:forEvent:)), for: .valueChanged)
sliderView.addTarget(self, action: #selector(seekProgressClick(_:forEvent:)), for: .touchDown)
sliderView.addTarget(self, action: #selector(seekProgressClick(_:forEvent:)), for: .touchUpInside)
return sliderView
}()
///View
lazy var progressView:UIProgressView = {
let progressView:UIProgressView = .init()
progressView.isUserInteractionEnabled = true
progressView.progressTintColor = .init(hex: "#FFFFFF", alpha: 0.3)
progressView.trackTintColor = .clear
return progressView
}()
///Label
lazy var durationLabel:UILabel = createLabel("00:00" ,font: .systemFont(ofSize: 12*width, weight: .medium), textColor: .init(hex: "#FFFFFF", alpha: 0.85), textAlignment: .left)
///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)
}
//
private func configure() {
//
addSubview(coverImageView)
coverImageView.snp.makeConstraints { make in
make.width.equalTo(335*width)
make.height.equalTo(330*width)
make.centerX.equalToSuperview()
make.top.equalToSuperview().offset(12*width)
}
//
addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
make.left.equalTo(coverImageView.snp.left)
make.top.equalTo(coverImageView.snp.bottom).offset(36*width)
make.right.equalTo(coverImageView.snp.right).offset(-100*width)
}
addSubview(subtitleLabel)
subtitleLabel.snp.makeConstraints { make in
make.left.right.equalTo(titleLabel)
make.top.equalTo(titleLabel.snp.bottom).offset(6*width)
}
//
addSubview(loadBtn)
loadBtn.snp.makeConstraints { make in
make.right.equalTo(coverImageView.snp.right).offset(-12*width)
make.top.equalTo(coverImageView.snp.bottom).offset(47*width)
make.width.height.equalTo(24*width)
}
addSubview(collectionSongBtn)
collectionSongBtn.snp.makeConstraints { make in
make.right.equalTo(loadBtn.snp.left).offset(-20*width)
make.centerY.equalTo(loadBtn.snp.centerY)
make.width.height.equalTo(24*width)
}
addSubview(progressView)
progressView.snp.makeConstraints { make in
make.top.equalTo(subtitleLabel.snp.bottom).offset(25*width)
make.centerX.equalToSuperview()
make.width.equalTo(335*width)
make.height.equalTo(6*width)
}
//label
addSubview(sliderView)
sliderView.snp.makeConstraints { make in
make.left.right.top.bottom.equalTo(progressView)
}
addSubview(durationLabel)
durationLabel.snp.makeConstraints { make in
make.left.equalTo(sliderView.snp.left)
make.top.equalTo(sliderView.snp.bottom).offset(5*width)
}
addSubview(maxTimesLabel)
maxTimesLabel.snp.makeConstraints { make in
make.right.equalTo(sliderView.snp.right)
make.top.equalTo(sliderView.snp.bottom).offset(5*width)
}
}
//
@objc private func seekProgressClick(_ sender: UISlider, forEvent event: UIEvent) {
//touchEvent
let touchEvent = event.allTouches?.first
//
switch touchEvent?.phase {
case .began://
//
MP_PlayerManager.shared.setEditPorgressStatu()
case .moved://
break
case .ended://
let value = sender.value
//
MP_PlayerManager.shared.setEditProgressEnd(value)
default:
break
}
}
//
@objc private func collectionSwitchClick(_ sender:UIButton) {
if self.collectionSongBtn.isSelected == true{
self.collectionSongBtn.isSelected = false
if MP_PlayerManager.shared.loadPlayer.currentVideo != nil{
MPPositive_CollectionSongModel.fetch(.init(format: "videoId == %@", MP_PlayerManager.shared.loadPlayer.currentVideo.song.videoId)).forEach { i in
if i.videoId == MP_PlayerManager.shared.loadPlayer.currentVideo.song.videoId{
MPPositive_CollectionSongModel.delete(i)
}
}
}
}else{
self.collectionSongBtn.isSelected = true
if MP_PlayerManager.shared.loadPlayer.currentVideo != nil{
let item = MPPositive_CollectionSongModel.create()
item.title = MP_PlayerManager.shared.loadPlayer.currentVideo.title
item.videoId = MP_PlayerManager.shared.loadPlayer.currentVideo.song.videoId
item.subtitle = MP_PlayerManager.shared.loadPlayer.currentVideo.subtitle
item.coverImage = MP_PlayerManager.shared.loadPlayer.currentVideo.coverUrl
MPPositive_CollectionSongModel.save()
}
}
}
//
@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 {
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)
}
SVProgressHUD.showInfo(withStatus: "下载超时请重新下载")
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
SVProgressHUD.dismiss()
}
}
})
}
}
}
}
//
private func addCircularProgressBar(over button: UIButton) {
loadView.removeFromSuperview() //
loadView = CircularProgressView(frame: button.bounds)
addSubview(loadView)
loadView.snp.makeConstraints { make in
make.center.equalTo(button)
make.width.height.equalTo(button)
}
}
}