// // HomeViewController.swift // MusicPlayer // // Created by Mr.Zhou on 2024/3/27. // import UIKit import UserMessagingPlatform class MPSideA_HomeViewController: MPSideA_BaseViewController { @IBOutlet weak var tableView: UITableView!{ didSet{ if #available(iOS 15.0, *) { tableView.sectionHeaderTopPadding = 0 } tableView.contentInsetAdjustmentBehavior = .never tableView.estimatedRowHeight = 200 tableView.rowHeight = UITableView.automaticDimension tableView.tableHeaderView = headView //额外加载一段下边距 tableView.contentInset = .init(top: 0, left: 0, bottom: 100*width, right: 0) tableView.dataSource = self tableView.delegate = self tableView.register(UINib(nibName: Home_RowListsTableViewCellID, bundle: nil), forCellReuseIdentifier: Home_RowListsTableViewCellID) } } private lazy var headView:MPSideA_Home_HeadBannerView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 135*width)) private let Home_RowListsTableViewCellID = "MPSideA_Home_RowListsTableViewCell" //组头标题 private lazy var sectionTitles:[String] = ["", MPSideA_MusicShowType.First.title, MPSideA_MusicShowType.Second.title, MPSideA_MusicShowType.Third.title] override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.asyncAfter(deadline: .now() + 3) { UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: nil) { requestConsentError in if let consentError = requestConsentError { print("[UMP] GoogleUMP error=\(consentError.localizedDescription)") return } print("[UMP] GoogleUMP success") UMPConsentForm.loadAndPresentIfRequired(from: self) { loadAndPresentError in if let consentError = loadAndPresentError { print("[UMP] GoogleUMP error=\(consentError.localizedDescription)") return } print("[UMP] GoogleUMP success") } } } //触发音乐缺失闭包 nullMusicAction = { [weak self] in //刷新页面 self?.reload() } } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) MP_AnalyticsManager.shared.home_a_pvAction() //添加通知 NotificationCenter.notificationKey.add(observer: self, selector: #selector(fileMissAction(_ :)), notificationName: .sideA_null_music) // NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .play_music) // NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .stop_music) // NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .resume_music) // NotificationCenter.notificationKey.add(observer: self, selector: #selector(switchPlayerStateAction(_ :)), notificationName: .pause_music) reload() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) //移除所有通知 NotificationCenter.default.removeObserver(self) } deinit { } //刷新渲染页面 private func reload() { MPSideA_LoadDataMusic.shared.reloadListData{ [weak self] in guard let self = self else {return} tableView.reloadData() } } @IBAction func menueClick(_ sender: UIButton) { let setVC = MPSideA_SettingViewController() setVC.cleanSide = false navigationController?.pushViewController(setVC, animated: true) } @IBAction func vipClick(_ sender: UIButton) { view.endEditing(true) MP_AnalyticsManager.shared.VIP_clickAction() let iapVC = MP_IAPViewController() iapVC.isType = false iapVC.privacyBlock = { [weak self] in guard let self = self else {return} MP_NetWorkManager.shared.requestNetworkPermission(oberve: self) { [weak self] in let privacyVC = MPSideA_PrivacyViewController() self?.navigationController?.pushViewController(privacyVC, animated: true) } } iapVC.termsBlock = { [weak self] in guard let self = self else {return} MP_NetWorkManager.shared.requestNetworkPermission(oberve: self) { [weak self] in let serviceVC = MPSideA_ServiceViewController() self?.navigationController?.pushViewController(serviceVC, animated: true) } } iapVC.modalPresentationStyle = .fullScreen present(iapVC, animated: true) } // //当音乐播放器状态改变时 // @objc private func switchPlayerStateAction(_ sender:Notification) { // //刷新页面内容 // tableView.reloadData() // } } //MARK: - tableView extension MPSideA_HomeViewController:UITableViewDataSource, UITableViewDelegate { func numberOfSections(in tableView: UITableView) -> Int { return sectionTitles.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: Home_RowListsTableViewCellID, for: indexPath) as! MPSideA_Home_RowListsTableViewCell cell.layout = .init(rawValue: indexPath.section) ?? .Zero return cell } //设置组头View func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let sectionView:UIView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 40*width)) sectionView.backgroundColor = .clear //添加一个Label let label = UILabel() label.textColor = .white label.font = .systemFont(ofSize: 20, weight: .regular) label.text = sectionTitles[section] sectionView.addSubview(label) label.snp.makeConstraints { make in make.left.equalToSuperview().offset(16*width) make.top.equalToSuperview() } return sectionView } //设置组头View高度 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return section != 0 ? 40*width:0 } }