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

355 lines
17 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 DownloadButton
import MarqueeLabel
//BView(View)
class MPPositive_PlayerCoverView: UIView, PKDownloadButtonDelegate {
///
lazy var coverImageView:UIImageView = {
let imageView = UIImageView()
imageView.image = placeholderImage
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 16*width
return imageView
}()
//
lazy var videoView:UIView = {
let videoView:UIView = .init()
videoView.backgroundColor = .clear
return videoView
}()
///
lazy var titleLabel:MarqueeLabel = createMarQueeLabel("Loading", font: .systemFont(ofSize: 22*width, weight: .regular), textColor: .init(hex: "#FFFFFF", alpha: 0.85))
///
lazy var subtitleLabel:MarqueeLabel = {
let label = createMarQueeLabel("Loading", font: .systemFont(ofSize: 12*width, weight: .regular), textColor: .init(hex: "#EEEEEE", alpha: 0.6))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(searchSubtitleClick(_ :))))
return label
}()
///
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 downloadButton: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.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
btn.delegate = self
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
progressView.progress = 0
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)
//
var deleteBlock:(() -> Void)?
//
var cancelBlock:(() -> Void)?
//
var searchBlock:((String) -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
configure()
NotificationCenter.notificationKey.add(observer: self, selector: #selector(downloadProgressAction(_ :)), notificationName: .download_progress_source)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(downloadEndAction(_ :)), notificationName: .dowload_end_source)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
//
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(videoView)
videoView.snp.makeConstraints { make in
make.left.top.right.equalToSuperview()
make.bottom.equalTo(titleLabel.snp.top).offset(0)
}
videoView.isHidden = true
addSubview(subtitleLabel)
subtitleLabel.snp.makeConstraints { make in
make.left.right.equalTo(titleLabel)
make.top.equalTo(titleLabel.snp.bottom).offset(6*width)
}
//
addSubview(downloadButton)
downloadButton.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(downloadButton.snp.left).offset(-20*width)
make.centerY.equalTo(downloadButton.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)
}
}
//videoId
func setProgress(_ videoId:String) {
guard videoId.isEmpty == false, videoId == MP_PlayerManager.shared.loadPlayer?.currentVideo?.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) {
//
if MP_DownloadManager.shared.isActiveTask(for: videoId) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
self.downloadButton.state = .downloading
}
//
if let progress = MP_DownloadManager.shared.getProgress(for: videoId) {
//
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
self.downloadButton.stopDownloadButton.progress = progress
}
}
}else {
//
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
//
self.downloadButton.state = .pending
}
}
}else {
//
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
//
self.downloadButton.state = .startDownload
}
}
}else {
//
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
//
self.downloadButton.state = .downloaded
}
}
}
}
}
//progress
@objc private func downloadProgressAction(_ sender:Notification) {
if let dict = sender.object as? [String:String], let videoId = dict["videoId"], let currentVideoId = MP_PlayerManager.shared.loadPlayer?.currentVideo?.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 = MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.videoId, videoId == currentVideoId {
setProgress(videoId)
}
}
//
@objc private func searchSubtitleClick(_ sender:UITapGestureRecognizer) {
guard let text = subtitleLabel.text, text.isEmpty != true else {return}
if let block = searchBlock {
block(text)
}
}
//
@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) {
guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
MP_HUD.text("Bad connection~".localizableString(), delay: 2.0, completion: nil)
return
}
if self.collectionSongBtn.isSelected == true{
self.collectionSongBtn.isSelected = false
if MP_PlayerManager.shared.loadPlayer?.currentVideo != nil{
MPPositive_CollectionSongModel.fetch(predicate: .init(format: "videoId == %@", (MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.videoId ?? ""))) { [weak self] results in
results.forEach { i in
if i.videoId == MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.videoId{
MPPositive_CollectionSongModel.delete(i)
}
}
MP_PlayerManager.shared.loadPlayer?.currentVideo?.reloadCollectionAndDownLoad()
MPPositive_LoadCoreModel.shared.reloadCollectionSongViewModel(nil)
}
MP_AnalyticsManager.shared.player_b_unlove_clickAction(MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.videoId ?? "", videoname: MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.title ?? "", artistname: MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.shortBylineText ?? "")
}
}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
item.lyricsID = MP_PlayerManager.shared.loadPlayer?.currentVideo.song.lyricsID
item.relatedID = MP_PlayerManager.shared.loadPlayer?.currentVideo.song.relatedID
item.addTime = Date()
MPPositive_CollectionSongModel.save()
MP_PlayerManager.shared.loadPlayer?.currentVideo.reloadCollectionAndDownLoad()
MPPositive_LoadCoreModel.shared.reloadCollectionSongViewModel(nil)
MP_AnalyticsManager.shared.player_b_love_clickAction(MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.videoId ?? "", videoname: MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.title ?? "", artistname: MP_PlayerManager.shared.loadPlayer?.currentVideo?.song.shortBylineText ?? "")
}
}
}
//
func downloadButtonTapped(_ downloadButton: PKDownloadButton!, currentState state: PKDownloadButtonState) {
//
switch state {
case .startDownload: //
guard MP_PlayerManager.shared.loadPlayer?.currentVideo != nil, MP_PlayerManager.shared.loadPlayer?.currentVideo?.isDlownd == false else {
return
}
guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
MP_HUD.text("Bad connection~".localizableString(), delay: 2.0, completion: nil)
return
}
MP_AdMobManager.shared.showLoadInterstitialAdIfAvailable(completion: nil)
//
downloadButton.state = .pending
//
guard let currentVideo = MP_PlayerManager.shared.loadPlayer?.currentVideo else {
MP_HUD.text("An error occurred while downloading. Please download again.".localizableString(), delay: 1.0) {
downloadButton.state = .startDownload
}
return
}
MP_AnalyticsManager.shared.player_b_download_clickAction(currentVideo.song.videoId, videoname: currentVideo.song.title ?? "", artistname: currentVideo.song.shortBylineText ?? "")
//,
MP_DownloadManager.shared.prepareVideoDownloadTask(from: currentVideo.song)
case .downloaded://
if let block = deleteBlock {
block()
}
default:
if let block = cancelBlock {
block()
}
}
}
}