Music_Player3/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Home(首页,各项列表页,艺术家页)/MPPositive_HomeViewController.swift
2024-07-05 17:25:49 +08:00

324 lines
15 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_HomeViewController.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/4/19.
//
import UIKit
class MPPositive_HomeViewController: MPPositive_BaseViewController, UIViewControllerTransitioningDelegate{
//
private lazy var bgImageView:UIImageView = {
let imageView:UIImageView = .init(image: .init(named: "B_Home_BG'bg"))
imageView.contentMode = .scaleAspectFill
return imageView
}()
//
private lazy var memuBtn:UIButton = {
let btn = UIButton()
btn.setBackgroundImage(UIImage(named: "Home_Menu'logo"), for: .normal)
btn.addTarget(self, action: #selector(menuRightClick(_ :)), for: .touchUpInside)
return btn
}()
//tableView
private lazy var tableView:UITableView = {
let tableView = UITableView(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), style: .plain)
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableView.automaticDimension
tableView.dataSource = self
tableView.delegate = self
tableView.register(MPPositive_HomeSinglesTableViewCell.self, forCellReuseIdentifier: MPPositive_HomeSinglesTableViewCellID)
tableView.register(MPPositive_HomeShowTableViewCell.self, forCellReuseIdentifier: MPPositive_HomeShowTableViewCellID)
tableView.contentInset = .init(top: 0, left: 0, bottom: 70*width, right: 0)
return tableView
}()
private let MPPositive_HomeSinglesTableViewCellID = "MPPositive_HomeSinglesTableViewCell"
private let MPPositive_HomeShowTableViewCellID = "MPPositive_HomeShowTableViewCell"
private var isFirstAppearance:Bool = true
// private var loadViewModel:MPPositive_BrowseLoadViewModel!
override func viewDidLoad() {
super.viewDidLoad()
MP_AdMobManager.shared.loadAnyAdMobs()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
//
requestTrackingAuthorization { idfa in
}
}
setTitle("")
confirgue()
NotificationCenter.notificationKey.add(observer: self, selector: #selector(reloadAction(_ :)), notificationName: .positive_browses_reload)
if MPPositive_BrowseLoadViewModel.shared.browseModuleLists.count == 0 {
MP_HUD.loading()
}
errorBlock = {
[weak self] in
guard let self = self else {
return
}
guard MPPositive_BrowseLoadViewModel.shared.browseModuleLists.count == 0 else {
return
}
//navView
view.subviews.forEach { item in
if item != self.navView {
//
if item.superview != nil {
item.removeFromSuperview()
}
}
}
//View
setErrorView()
MP_HUD.hideNow()
}
retryBlock = {
[weak self] in
guard let self = self else {return}
MP_HUD.loading()
DispatchQueue.main.async {
//
MPPositive_BrowseLoadViewModel.shared.reloadBrowseLists()
}
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isFirstAppearance == false {
//
presentGuide()
isFirstAppearance = true
}
MP_AnalyticsManager.shared.home_b_pvAction()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
//
private func createSearchView() -> UIView{
let searchView:UIView = UIView()
searchView.backgroundColor = .init(hex: "#212121")
searchView.isUserInteractionEnabled = true
searchView.layer.masksToBounds = true
searchView.layer.cornerRadius = 16*width
//icon
let iconImageView = UIImageView(image: .init(named: "Search_ICON'logo"))
searchView.addSubview(iconImageView)
iconImageView.snp.makeConstraints { make in
make.height.width.equalTo(16*width)
make.left.equalToSuperview().offset(16*width)
make.centerY.equalToSuperview()
}
let label = createLabel("Search songs,artists,playlists", font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .init(hex: "#666666"), textAlignment: .left)
searchView.addSubview(label)
label.snp.makeConstraints { make in
make.left.equalTo(iconImageView.snp.right).offset(8*width)
make.centerY.equalToSuperview()
}
searchView.isUserInteractionEnabled = true
searchView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(searchClick(_:))))
return searchView
}
//
@objc fileprivate func searchClick(_ sender:UITapGestureRecognizer) {
isFirstAppearance = false
let resultVC = MPPositive_SearchResultShowViewController()
navigationController?.pushViewController(resultVC, animated: false)
}
///
func presentGuide() {
guard UserDefaults.standard.bool(forKey: "isGuide") != true else {
//
print("已经评论过了")
return
}
//3
guard var actives = UserDefaults.standard.object(forKey: "ActiveDays") as? [Date], actives.count >= 3 else {
return
}
//3
MPPositive_ModalType = .Guide
let guideVC = MP_GuideViewController()
guideVC.disMissBlock = {
DispatchQueue.main.async {
//
actives.removeAll()
let now = Date().timeZone()
UserDefaults.standard.setValue([now], forKey: "ActiveDays")
}
}
guideVC.feedBackBlock = {
DispatchQueue.main.async {
let alert = UIAlertController(title: "Feedback", message: "If you have any comments or suggestions, please contact us at the following e-mail address", preferredStyle: .actionSheet)
let email = UIAlertAction(title: "marketing@lux-ad.com", style: .default) { (_) in
//
UIPasteboard.general.string = "marketing@lux-ad.com"
MP_HUD.text("Successfully copied the e-mail address to the clipboard", delay: 1.0, completion: nil)
}
alert.addAction(email)
let cancel = UIAlertAction(title: "Cancel", style: .cancel)
alert.addAction(cancel)
self.present(alert, animated: true)
}
}
guideVC.storeBlock = {
DispatchQueue.main.async {
//AppStore
if let url = URL(string: "https://apps.apple.com/app/6502973957") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
}
guideVC.transitioningDelegate = self
guideVC.modalPresentationStyle = .custom
self.present(guideVC, animated: true)
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return MPPositive_PresentationController(presentedViewController: presented, presenting: presenting)
}
//MARK: - UI
//
private func confirgue() {
let searchView = createSearchView()
navView.addSubview(searchView)
searchView.snp.makeConstraints { make in
make.width.equalTo(300*width)
make.height.equalTo(32*width)
make.left.equalToSuperview().offset(18*width)
make.centerY.equalToSuperview()
}
view.addSubview(bgImageView)
bgImageView.snp.makeConstraints { make in
make.top.right.left.equalToSuperview()
make.height.equalTo(981*width)
}
view.addSubview(tableView)
navView.addSubview(memuBtn)
memuBtn.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-16*width)
make.centerY.equalToSuperview()
make.width.height.equalTo(24*width)
}
tableView.snp.makeConstraints { make in
make.top.equalTo(navView.snp.bottom).offset(32*width)
make.left.right.bottom.equalToSuperview()
}
}
//MARK: -
//
@objc private func reloadAction(_ sender:Notification?) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
if tableView.superview == nil {
confirgue()
}
removeErrorView()
MP_HUD.hideNow()
tableView.showMessage(MPPositive_BrowseLoadViewModel.shared.browseModuleLists.count)
tableView.reloadData()
}
}
//MARK: -
//
@objc private func menuRightClick(_ sender:UIButton) {
isFirstAppearance = false
let setVC = MPSideA_SettingViewController()
navigationController?.pushViewController(setVC, animated: true)
}
}
//MARK: - tableView
extension MPPositive_HomeViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return MPPositive_BrowseLoadViewModel.shared.browseModuleLists.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let first = MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section].items.first, first.browseItem.itemType == .single, first.browseItem.pageType == "MUSIC_VIDEO_TYPE_ATV" {
//
let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_HomeSinglesTableViewCellID, for: indexPath) as! MPPositive_HomeSinglesTableViewCell
cell.browseViewModel = MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section]
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_HomeShowTableViewCellID, for: indexPath) as! MPPositive_HomeShowTableViewCell
cell.browseViewModel = MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section]
if let first = MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section].items.first, first.browseItem.itemType == .single, first.browseItem.pageType == "MUSIC_VIDEO_TYPE_OMV" {
cell.showType = .Fifth
}else {
//
if MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section].items.first?.browseItem.pageType == "MUSIC_PAGE_TYPE_ARTIST" {
cell.showType = .Fourth
}else {
cell.showType = .Third
}
}
cell.requestNextBlock = {
[weak self] (item) in
guard let self = self else {return}
MP_AnalyticsManager.shared.home_b_module_clickAction(MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section].title ?? "")
switch item.browseItem.itemType {
case .single:
///
MPPositive_Debouncer.shared.call {
[weak self] in
guard let self = self else {return}
guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
MP_HUD.text("Weak connection.", delay: 2.0, completion: nil)
return
}
MP_AnalyticsManager.shared.song_clickAction("Home")
//
MP_PlayerManager.shared.loadPlayer = nil
//
NotificationCenter.notificationKey.post(notificationName: .pup_player_vc)
MP_AnalyticsManager.shared.player_b_impAction()
//next
MP_NetWorkManager.shared.requestNextList(item.browseItem.playListId ?? "", videoId: item.browseItem.videoId ?? ""){ [weak self] listSongs in
guard let self = self else {return}
//playerloadViewModel
let lodaViewModel = MPPositive_PlayerLoadViewModel(listSongs, currentVideoId: item.browseItem.videoId ?? "")
lodaViewModel.improveData(item.browseItem.videoId ?? "")
//
MP_PlayerManager.shared.setPlayType(.normal)
MP_PlayerManager.shared.loadPlayer = lodaViewModel
MP_AnalyticsManager.shared.player_b_listAction()
}
}
case .list:
isFirstAppearance = false
//
let listVC = MPPositive_ListShowViewController(item.browseItem.browseId ?? "", params: item.browseItem.params ?? "", title: item.title ?? "", subtitle: item.subtitle ?? "")
navigationController?.pushViewController(listVC, animated: true)
case .artist:
isFirstAppearance = false
//
let artistVC = MPPositive_ArtistShowViewController(item.browseItem.artistId ?? "")
navigationController?.pushViewController(artistVC, animated: true)
default:
break
}
}
cell.findMoreBlock = {
[weak self] in
guard let self = self else {return}
isFirstAppearance = false
let moreVC = MPPositive_MoreContentViewController(MPPositive_BrowseLoadViewModel.shared.browseModuleLists[indexPath.section])
navigationController?.pushViewController(moreVC, animated: true)
}
return cell
}
}
}