Music_Player3/relax.offline.mp3.music/MP/MPSideA/ViewControllers/Base(基类-导航栏-标签栏-计时器-播放器)/MPSideA_TabBarController.swift
2024-08-09 17:48:28 +08:00

327 lines
14 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.

//
// MPTabBarController.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/3/27.
//
import UIKit
///A
class MPSideA_TabBarController: UITabBarController, GADFullScreenContentDelegate {
//tabBar
private lazy var customTabBar:MPSideA_CustomTabBar = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 72*width))
//View
private lazy var bottomView:MPSideA_BottomShowView = .instanceFromNib()
var pushPlayerBlock:(() -> Void)?
//bottomShowView
private var isbottomShow:Bool = false{
willSet{
if isbottomShow != newValue {
switch_bottomShow(newValue)
}
//
if newValue == true {
guard let music = MPSideA_MediaCenterManager.shared.getMusic() else {
print("No Data Music")
return
}
bottomView.coverImageView.image = UIImage(data: music.cover)
bottomView.titleLabel.text = music.title
bottomView.subtitleLabel.text = setTimesToMinSeconds(music.duration)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
tabBar.isHidden = true
self.setValue(customTabBar, forKey: "tabBar")
//
let homeVC = MPSideA_NavigationController(rootViewController: MPSideA_HomeViewController())
let centerVC = MPSideA_NavigationController(rootViewController: MPSideA_CenterViewController())
//
viewControllers = [homeVC,centerVC]
//tabBaritem
tabBar.items?.forEach({ item in
item.isEnabled = false
})
tabBar.barTintColor = .clear
UITabBar.appearance().backgroundColor = .clear
tabBar.shadowImage = UIImage()
// tabBar.shadowImage = image(UIColor.red) // 线
tabBar.backgroundImage = UIImage()
view.addSubview(bottomView)
//View
bottomView.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.bottom.equalToSuperview().offset(82*width)
make.width.equalTo(351*width)
make.height.equalTo(82*width)
}
//View
//
bottomView.modalTimerBlock = {
[weak self] in
self?.modalTimer()
}
//
bottomView.switchPlayerBlock = {
[weak self] in
self?.switchState()
}
//
bottomView.pushPlayerBlock = {
[weak self] in
self?.pushPlayer()
}
//View
isbottomShow = MPSideA_MediaCenterManager.shared.getMusic() != nil
//
NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchAction(_:)), notificationName: .switch_tabBarItem)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(timesAction(_ :)), notificationName: .sideA_time_times)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(playMusicAction(_ :)), notificationName: .sideA_play_music)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(pauseMusicAction(_ :)), notificationName: .sideA_pause_music)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(resumeMusicAction(_ :)), notificationName: .sideA_resume_music)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(stopMusicAction(_ :)), notificationName: .sideA_stop_music)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(closeBottonViewAction(_ :)), notificationName: .sideA_close_show)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(displayBottomViewAction(_ :)), notificationName: .sideA_display_show)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(hiddenBottomViewAction(_ :)), notificationName: .sideA_hidden_show)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(renameMusicAction(_ :)), notificationName: .sideA_rename_music)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
deinit {
//
NotificationCenter.default.removeObserver(self)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
//View
view.bringSubviewToFront(bottomView)
}
//BottomView
private func switch_bottomShow(_ state:Bool) {
UIView.animate(withDuration: 0.3) {
[weak self] in
guard let self = self else { return }
if state {
//
bottomView.transform = .init(translationX: 0, y: -139*width)
}else {
//
bottomView.transform = .identity
}
}
}
}
//MARK: -
extension MPSideA_TabBarController: UIViewControllerTransitioningDelegate {
//
@objc private func switchAction(_ sender:Notification) {
let tag = sender.object as! Int
selectedIndex = tag
}
//
private func modalTimer() {
MPSideA_ModalType = .Timer
let timerVC = MPSideA_CountTimerViewController()
timerVC.transitioningDelegate = self
timerVC.modalPresentationStyle = .custom
present(timerVC, animated: true)
}
//
private func switchState() {
guard let music = MPSideA_MediaCenterManager.shared.getMusic() else {
//
print("No Data Music")
return
}
switch MPSideA_MediaCenterManager.shared.getPlayerState() {
case .Null://
MPSideA_MediaCenterManager.shared.playerStart(music, actionType: .Normal)
case .Playing://
MPSideA_MediaCenterManager.shared.playerPause()
case .Pause://
MPSideA_MediaCenterManager.shared.playerResume()
}
}
//
private func pushPlayer() {
print("Go to player")
MP_AdMobManager.shared.showPlayInterstitialAdIfAvailable { [weak self] ad in
guard let self = self else {return}
if let ad = ad {
//
MP_AdMobManager.shared.isShowingPlayInterstitialAd = true
//广
MP_AdMobManager.shared.setInterstitialSwitch(true)
ad.fullScreenContentDelegate = self
ad.present(fromRootViewController: self)
}else {
if let block = self.pushPlayerBlock {
block()
}
}
}
self.pushPlayerBlock = {
[weak self] in
//
DispatchQueue.main.async {
[weak self] in
//
let playerVC = MPSideA_PlayerViewController()
self?.present(playerVC, animated: true)
}
}
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return MPSideA_PresentationController(presentedViewController: presented, presenting: presenting)
}
}
//MARK: -
extension MPSideA_TabBarController {
//
@objc private func timesAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
//View
bottomView.timerBtn.isHidden = true
bottomView.timerView.isHidden = false
let time = sender.object as! TimeInterval
bottomView.timerLabel.text = setTimesToMinSeconds(time)
}
}
//
@objc private func playMusicAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
isbottomShow = true
bottomView.stateBtn.isSelected = true
}
}
//
@objc private func pauseMusicAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
bottomView.stateBtn.isSelected = false
}
}
//
@objc private func resumeMusicAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
bottomView.stateBtn.isSelected = true
}
}
//
@objc private func stopMusicAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
bottomView.stateBtn.isSelected = false
bottomView.timerLabel.text = "Time"
//View
bottomView.timerBtn.isHidden = false
bottomView.timerView.isHidden = true
}
}
//
@objc private func closeBottonViewAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
isbottomShow = false
}
}
//(ishidden)
@objc private func displayBottomViewAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
bottomView.isHidden = false
}
}
//(ishidden)
@objc private func hiddenBottomViewAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
bottomView.isHidden = true
}
}
@objc private func renameMusicAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else { return }
isbottomShow = isbottomShow
}
}
//MARK: - 广 GADFullScreenContentDelegate
//广
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
if ad === MP_AdMobManager.shared.playInterstitialAd {//广
print("当前展示的广告是播放插页广告广告ID--\(MP_AdMobManager.shared.playInterstitialAd?.adUnitID ?? "")")
if MP_AnalyticsManager.shared.infoFromAdMobSource(MP_AdMobManager.shared.playInterstitialAd?.responseInfo) {
MP_AdMobManager.shared.playInterstitialAd?.paidEventHandler = { adValue in
if let response = MP_AdMobManager.shared.playInterstitialAd?.responseInfo {
MP_AnalyticsManager.shared.play_ads_showSuccessAction(response, adValue: adValue)
}
}
}else {
if let response = MP_AdMobManager.shared.playInterstitialAd?.responseInfo {
MP_AnalyticsManager.shared.play_ads_showSuccessAction(response, adValue: .init())
}
}
}
}
//广
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
MP_AdMobManager.shared.interstitialDate = Date()
if ad === MP_AdMobManager.shared.playInterstitialAd {//广
let UUID = MP_AdMobManager.shared.playInterstitialAd?.adUnitID ?? ""
print("当前消失的广告是播放插页广告广告ID--\(UUID)")
//广
if MP_AdMobManager.shared.completePlayInterstitialAdBlock != nil {
MP_AdMobManager.shared.completePlayInterstitialAdBlock!()
}
//
if let block = self.pushPlayerBlock {
block()
}
}
}
//广
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
MP_AdMobManager.shared.interstitialDate = Date()
if ad === MP_AdMobManager.shared.playInterstitialAd {//广
let UUID = MP_AdMobManager.shared.playInterstitialAd?.adUnitID ?? ""
print("播放插页广告展示时出错广告ID--\(UUID),具体错误原因:\(error.localizedDescription)")
MP_AnalyticsManager.shared.play_ads_showFailureAction(error.localizedDescription)
//广
//广
if MP_AdMobManager.shared.completePlayInterstitialAdBlock != nil {
MP_AdMobManager.shared.completePlayInterstitialAdBlock!()
}
//
// DispatchQueue.main.async {
// [weak self] in
// //
// let playerVC = MPSideA_PlayerViewController()
// self?.present(playerVC, animated: true)
// }
}
}
}