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

195 lines
8.1 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: UIView, 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() {
//
MP_DownloadManager.shared.delegate = self
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)
}
}
}
//MARK: - tableView
extension MPPositive_SearchResultPreviewShowView:UITableViewDataSource, UITableViewDelegate, MP_DownloadManagerDelegate {
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollBlock != nil {
scrollBlock!()
}
}
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 block = self?.moreBlock, let itemView = self?.sectionLists[indexPath.section].previewItemViews[indexPath.row]{
block(itemView)
}
}
cell.cancelBlock = {
[weak self] in
if let block = self?.cancelBlock, let itemView = self?.sectionLists[indexPath.section].previewItemViews[indexPath.row]{
block(itemView, tableView)
}
}
cell.deleteBlock = {
[weak self] in
if let block = self?.deleteBlock, let itemView = self?.sectionLists[indexPath.section].previewItemViews[indexPath.row]{
block(itemView, tableView)
}
}
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) {
if chooseItemBlock != nil {
chooseItemBlock!(sectionLists[indexPath.section].previewItemViews[indexPath.row])
}
}
//
func downloadProgressDidUpdate(for videoId: String, progress: CGFloat) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
for (section, list) in (sectionLists ?? []).enumerated() {
if let first = list.previewItemViews.first {
//VideoIdindexPath
if let index = list.previewItemViews.firstIndex(where: {$0.item.videoId == videoId}) {
let indexPath = IndexPath(row: index, section: section)
//cell
if let cell = tableView.cellForRow(at: indexPath) as? MPPositive_SearchResultShowTableViewCell {
cell.setProgress(videoId)
}
}
}
}
}
}
//
func downloadResult(for videoId: String, result: Result<MPPositive_SongItemModel, any Error>) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
switch result {
case .success(let song):
print("下载\(song.title ?? "")成功")
case .failure(let error):
//
print("下载报错,错误详情\(error)")
MP_HUD.text("An error occurred while downloading. Please download again.", delay: 1.5, completion: nil)
}
for (section, list) in (sectionLists ?? []).enumerated() {
if let first = list.previewItemViews.first {
//VideoIdindexPath
if let index = list.previewItemViews.firstIndex(where: {$0.item.videoId == videoId}) {
let indexPath = IndexPath(row: index, section: section)
//cell
if let cell = tableView.cellForRow(at: indexPath) as? MPPositive_SearchResultShowTableViewCell {
cell.setProgress(videoId)
}
}
}
}
}
}
}