Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Search/MPPositive_SearchResultPreviewShowView.swift

238 lines
12 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_SearchResultPreviewShowView.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/5/13.
//
import UIKit
///
class MPPositive_SearchResultPreviewShowView: MPPositive_BaseShowView, JXSegmentedListContainerViewListDelegate {
//tableView
private lazy var tableView:UITableView = {
let tableView = UITableView()
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableView.automaticDimension
tableView.dataSource = self
tableView.delegate = self
tableView.register(MPPositive_SearchResultShowTableViewCell.self, forCellReuseIdentifier: MPPositive_SearchResultShowTableViewCellID)
tableView.contentInset = .init(top: 0, left: 0, bottom: 70*width, right: 0)
return tableView
}()
private let MPPositive_SearchResultShowTableViewCellID = "MPPositive_SearchResultShowTableViewCell"
private var sectionLists:[MPPositive_SearchResultListViewModel]!{
didSet{
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
if sectionLists != nil {
tableView.reloadData()
}
}
}
}
var scrollBlock:(() -> Void)?
var chooseMoreIndexBlock:((Int) -> Void)?
// var moreBlock:((MPPositive_SearchResultItemViewModel) -> Void)?
// //
// var chooseItemBlock:((MPPositive_SearchResultItemViewModel) -> Void)?
// var deleteBlock:((MPPositive_SearchResultItemViewModel, UITableView) -> Void)?
// var cancelBlock:((MPPositive_SearchResultItemViewModel, UITableView) -> Void)?
init(frame: CGRect, sectionLists:[MPPositive_SearchResultListViewModel]) {
super.init(frame: frame)
backgroundColor = .init(hex: "1A1A1A")
self.sectionLists = sectionLists
configure()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
//MARK: -
func listView() -> UIView {
return self
}
func listWillAppear() {
//
tableView.reloadData()
}
//
private func configure() {
addSubview(tableView)
tableView.snp.makeConstraints { make in
make.left.top.right.bottom.equalToSuperview()
}
}
//
@objc private func moreFindClick(_ sender:UIButton) {
let selectedTag = sender.tag
if chooseMoreIndexBlock != nil {
chooseMoreIndexBlock!(selectedTag)
}
}
func chooseItemClick(_ item:MPPositive_SearchResultItemViewModel) {
switch item.item.itemType {
case .artist:
//
let artistVC = MPPositive_ArtistShowViewController(item.item.artistId ?? "", clickTrackingParams: item.item.clickTrackingParams ?? "")
if let vc = self.parentController(ofType: MPPositive_SearchResultShowViewController.self) {
vc.navigationController?.pushViewController(artistVC, animated: true)
}
case .list:
//
let listVC = MPPositive_ListShowViewController(item.item.browseId ?? "", params: "", title: item.title ?? "", subtitle: item.subtitle ?? "", clickTrackingParams: item.item.clickTrackingParams ?? "")
if let vc = self.parentController(ofType: MPPositive_SearchResultShowViewController.self) {
vc.navigationController?.pushViewController(listVC, animated: true)
}
case .single:
MPPositive_Debouncer.shared.call {
[weak self] in
guard let self = self else {return}
guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
playOfflineSongs()
return
}
MP_AnalyticsManager.shared.song_clickAction("Search")
//
MP_PlayerManager.shared.loadPlayer = nil
///
//
NotificationCenter.notificationKey.post(notificationName: .pup_player_vc)
MP_AnalyticsManager.shared.player_b_impAction()
//next
MP_NetWorkManager.shared.requestNextList(item.item.playListId ?? "", videoId: item.item.videoId ?? "", clickTrackingParams: item.item.clickTrackingParams){ [weak self] listSongs in
guard let self = self else {return}
//playerloadViewModel
let lodaViewModel = MPPositive_PlayerLoadViewModel(listSongs, currentVideoId: item.item.videoId ?? "")
lodaViewModel.improveData(item.item.videoId ?? "")
//
MP_PlayerManager.shared.setPlayType(.normal)
MP_PlayerManager.shared.loadPlayer = lodaViewModel
MP_AnalyticsManager.shared.player_b_listAction()
// NotificationCenter.notificationKey.post(notificationName: .pup_player_vc)
}
}
case .none:
break
}
}
}
//MARK: - tableView
extension MPPositive_SearchResultPreviewShowView:UITableViewDataSource, UITableViewDelegate, UIViewControllerTransitioningDelegate {
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if let block = scrollBlock {
block()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionLists.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionLists[section].previewItemViews.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_SearchResultShowTableViewCellID, for: indexPath) as! MPPositive_SearchResultShowTableViewCell
cell.itemView = sectionLists[indexPath.section].previewItemViews[indexPath.row]
cell.moreBlock = {
[weak self] in
if let itemView = self?.sectionLists[indexPath.section].previewItemViews[indexPath.row]{
MPPositive_Debouncer.shared.call {
MPPositive_ModalType = .MoreOperations
let moreVC = MPPositive_MoreSongOperationsViewController(itemView)
moreVC.disMissBlock = {
DispatchQueue.main.async {
tableView.reloadData()
}
}
moreVC.transitioningDelegate = self
moreVC.modalPresentationStyle = .custom
self?.parentController()?.present(moreVC, animated: true)
}
}
}
cell.cancelBlock = {
[weak self] in
if let itemView = self?.sectionLists[indexPath.section].previewItemViews[indexPath.row]{
//
let alertController = UIAlertController(title: "Cancel Song Download Task".localizableString(), message: "Are you sure you want to cancel the download task of this song?".localizableString(), preferredStyle: .alert)
let cancel = UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(cancel)
let sure = UIAlertAction(title: "Confirm".localizableString(), style: .destructive) {(action) in
guard let videoId = itemView.item.videoId else {return}
//
MP_DownloadManager.shared.cancelDownloadTask(videoId) { videoId in
MP_HUD.text("Cancel".localizableString(), delay: 1.0, completion: nil)
DispatchQueue.main.async {
tableView.reloadData()
}
}
}
alertController.addAction(sure)
self?.parentController()?.present(alertController, animated: true)
}
}
cell.deleteBlock = {
[weak self] in
if let itemView = self?.sectionLists[indexPath.section].previewItemViews[indexPath.row]{
//
let alertController = UIAlertController(title: "Delete This Song".localizableString(), message: "Are you sure you want to delete the offline resources of this song?".localizableString(), preferredStyle: .alert)
let cancel = UIAlertAction(title: "Cancel".localizableString(), style: .cancel)
alertController.addAction(cancel)
let sure = UIAlertAction(title: "Confirm".localizableString(), style: .destructive) {(action) in
guard let videoId = itemView.item.videoId else {return}
//
MP_DownloadManager.shared.deleteFileDocuments(videoId) { videoId in
MP_HUD.progress("Loading...".localizableString(), delay: 0.5) {
MP_HUD.text("Removed".localizableString(), delay: 1.0, completion: nil)
DispatchQueue.main.async {
tableView.reloadData()
}
}
}
}
alertController.addAction(sure)
self?.parentController()?.present(alertController, animated: true)
}
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView:UIView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 50*width))
sectionView.backgroundColor = .init(hex: "#1A1A1A")
let label = createLabel(sectionLists[section].title, font: .systemFont(ofSize: 16*width, weight: .semibold), textColor: .white, textAlignment: .left)
sectionView.addSubview(label)
label.snp.makeConstraints { make in
make.left.equalToSuperview().offset(18*width)
make.centerY.equalToSuperview()
}
if section != 0 {
//
let btn:UIButton = .init()
btn.setBackgroundImage(UIImage(named: "Home_More'logo"), for: .normal)
btn.tag = section
btn.addTarget(self, action: #selector(moreFindClick(_ :)), for: .touchUpInside)
sectionView.addSubview(btn)
btn.snp.makeConstraints { make in
make.width.height.equalTo(24*width)
make.right.equalToSuperview().offset(-18*width)
make.centerY.equalToSuperview()
}
}
return sectionView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50*width
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
chooseItemClick(sectionLists[indexPath.section].previewItemViews[indexPath.row])
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return MPPositive_PresentationController(presentedViewController: presented, presenting: presenting)
}
}