Music_Player3/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Center(个人曲库页)/MPPositive_BatchDeletionViewController.swift
2024-12-25 10:02:00 +08:00

114 lines
4.1 KiB
Swift

//
// 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()
}
}