// // MPPositive_BatchDeletionViewController.swift // relax.offline.mp3.music // // Created by Mr.Zhou on 2024/12/23. // import UIKit ///批量删除 class MPPositive_BatchDeletionViewController: MPPositive_BaseViewController { //确认按钮 private lazy var confirmBtn:UIButton = { let btn:UIButton = .init() btn.setTitle("Confirm", for: .normal) btn.setTitleColor(.white, for: .normal) btn.titleLabel?.font = .systemFont(ofSize: 16*width, weight: .medium) btn.addTarget(self, action: #selector(deleteAction(_ :)), for: .touchUpInside) return btn }() //tableView private lazy var tableView:MPPositive_DefaultTableView = { let tableView:MPPositive_DefaultTableView = .init(frame: .zero, style: .plain) tableView.dataSource = self tableView.delegate = self tableView.register(MPPositive_AddSongTableViewCell.self, forCellReuseIdentifier: MPPositive_AddSongTableViewCellID) return tableView }() private let MPPositive_AddSongTableViewCellID = "MPPositive_AddSongTableViewCell" private var status:[Bool] = [] private var offlines:[MPPositive_DownloadViewModel] init(withOfflines offlines:[MPPositive_DownloadViewModel]) { self.offlines = offlines super.init(nibName: nil, bundle: nil) refreshStatus() } required init?(coder: NSCoder) { self.offlines = [] super.init(coder: coder) refreshStatus() } override func viewDidLoad() { super.viewDidLoad() setPopBtn() setTitle("Batch Deletion") config() } private func config() { navView.addSubview(confirmBtn) confirmBtn.snp.makeConstraints { make in make.right.equalToSuperview().offset(-16*width) make.centerY.equalToSuperview() } view.addSubview(tableView) tableView.snp.makeConstraints { make in make.left.bottom.right.equalToSuperview() make.top.equalTo(navView.snp.bottom) } } //补充status private func refreshStatus() { //每一个离线资源都产生对应的状态值(默认false) self.status = offlines.compactMap({_ in false}) //刷新页面 DispatchQueue.main.async { [weak self] in guard let self = self else {return} tableView.showMessage(status.count) tableView.reloadData() } } //确认删除 @objc private func deleteAction(_ sender:UIButton) { view.endEditing(true) //将状态值为ture的数据取出 var array:[MPPositive_DownloadViewModel] = [] for (index, item) in status.enumerated() { if item == true { array.append(offlines[index]) } } guard !array.isEmpty else { MP_HUD.showWithStatus(hudStatus: .error, text: "Please select the song", delay: 1.0, completion: nil) return } MP_HUD.loading() array.forEach { item in //确定删除 MP_DownloadManager.shared.deleteFileDocuments(item.loadItem.videoId ?? "") { videoId in } } MP_HUD.success("Removed".localizableString(), delay: 1.0) { [weak self] in self?.navigationController?.popViewController(animated: true) } } } //MARK: - tableView extension MPPositive_BatchDeletionViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return offlines.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_AddSongTableViewCellID, for: indexPath) as! MPPositive_AddSongTableViewCell cell.download = offlines[indexPath.row] cell.statu = status[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { //调整当前选中的状态值 status[indexPath.row] = !status[indexPath.row] tableView.reloadData() } }