Music_Player3/MusicPlayer/MP/ViewControllers/Center(个人资源)/CenterViewController.swift
Mr.zhou 96147c5e37 项目:Musicoo
版本:A面 1.0
构建:1.1
更新内容:对项目A面功能的实现,经测试确定各项功能无问题。
更新时间:2024年4月12日 11:20
上传状态:已上传App Connect
2024-04-12 11:19:58 +08:00

195 lines
7.5 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.

//
// CenterViewController.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/3/27.
//
import UIKit
///
class CenterViewController: BaseViewController {
@IBOutlet weak var tableView: UITableView!{
didSet{
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableView.automaticDimension
//tableView
tableView.contentInset = .init(top: 27*width, left: 0, bottom: 100*width, right: 0)
tableView.dataSource = self
tableView.delegate = self
tableView.register(UINib(nibName: CenterTableViewCellID, bundle: nil), forCellReuseIdentifier: CenterTableViewCellID)
}
}
@IBOutlet weak var noticeView: UIView!
private let CenterTableViewCellID = "CenterTableViewCell"
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .init(hex: "#000000")
//
nullMusicAction = {
[weak self] in
//
self?.reload()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(successfullCreateAction), notificationName: .creat_music)
NotificationCenter.notificationKey.add(observer: self, selector: #selector(fileMissAction(_ :)), notificationName: .null_music)
reload()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
//
private func reload() {
LoadDataMusic.shared.reloadListData()
//
if LoadDataMusic.shared.userlistMusics.count == 0 {
//
noticeView.isHidden = false
}else {
noticeView.isHidden = true
}
tableView.showMessage(LoadDataMusic.shared.userlistMusics.count)
tableView.reloadData()
}
//
@objc private func successfullCreateAction() {
DispatchQueue.main.async {
[weak self] in
self?.reload()
}
}
//
@IBAction func setActionClick(_ sender: UIButton) {
let setVC = SettingViewController()
navigationController?.pushViewController(setVC, animated: true)
}
///
@IBAction func addClick(_ sender: UIButton) {
MPModalType = .Choice
let addVC = AddViewController()
addVC.modalPresentationStyle = .custom
addVC.transitioningDelegate = self
present(addVC, animated: true)
}
}
//MARK: - tableView
extension CenterViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return LoadDataMusic.shared.userlistMusics.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CenterTableViewCellID, for: indexPath) as! CenterTableViewCell
cell.musicView = LoadDataMusic.shared.userlistMusics[indexPath.row]
cell.moreBlock = {
[weak self] (sender) in
self?.moreAction(sender, music: LoadDataMusic.shared.userlistMusics[indexPath.row])
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let music = LoadDataMusic.shared.userlistMusics[indexPath.row].music
//
guard MPMediaCenterManager.shared.getMusic()?.identifier != music.identifier else {
//
switch MPMediaCenterManager.shared.getPlayerState() {
case .Null://
MPMediaCenterManager.shared.playerStart(music, actionType: .Normal)
case .Playing://
break
case .Pause://
MPMediaCenterManager.shared.playerResume()
}
reload()
return
}
//
MPMediaCenterManager.shared.playerStart(music, actionType: .Normal)
reload()
}
}
//MARK: - Popover
extension CenterViewController: UIPopoverPresentationControllerDelegate, UIViewControllerTransitioningDelegate {
//
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
//
private func moreAction(_ sender:UIButton, music:MusicViewModel) {
//popover
let moreVC = MoreViewController()
//moreVC
moreVC.preferredContentSize = .init(width: 130*width, height: 65*width)
moreVC.modalPresentationStyle = .popover
moreVC.renameBlock = {
[weak self] in
self?.renameAction(music)
}
moreVC.deleteBlock = {
[weak self] in
self?.deleteAction(music)
}
let popoverVC = moreVC.popoverPresentationController
popoverVC?.delegate = self
//
popoverVC?.sourceView = sender
popoverVC?.backgroundColor = .init(hex: "#434343")
//
popoverVC?.permittedArrowDirections = .right
present(moreVC, animated: true)
}
//
private func renameAction(_ music:MusicViewModel) {
MPModalType = .Rename
let renameVC = RenameViewController()
renameVC.renameBlock = {
[weak self] (name) in
music.music.title = name
MusicModel.save()
//
if music.music.identifier == MPMediaCenterManager.shared.getMusic()?.identifier {
//
MPMediaCenterManager.shared.setMusic(music.music)
NotificationCenter.notificationKey.post(notificationName: .rename_music)
}
//
self?.reload()
}
renameVC.transitioningDelegate = self
renameVC.modalPresentationStyle = .custom
present(renameVC, animated: true)
}
//
private func deleteAction(_ music:MusicViewModel) {
MPModalType = .Delete
let deleteVC = DeleteViewController()
deleteVC.deleteBlock = {
[weak self] in
//
if music.music.identifier == MPMediaCenterManager.shared.getMusic()?.identifier {
MPMediaCenterManager.shared.setMusic(nil)
//
MPMediaCenterManager.shared.playerStop()
//
NotificationCenter.notificationKey.post(notificationName: .close_show)
}
MusicModel.delete(music.music)
//
self?.reload()
}
deleteVC.transitioningDelegate = self
deleteVC.modalPresentationStyle = .custom
present(deleteVC, animated: true)
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return MPPresentationController(presentedViewController: presented, presenting: presenting)
}
}