195 lines
8.1 KiB
Swift
195 lines
8.1 KiB
Swift
//
|
||
// 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 {
|
||
//根据VideoId获取当前列表中的下载的indexPath
|
||
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 {
|
||
//根据VideoId获取当前列表中的下载的indexPath
|
||
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)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|