253 lines
10 KiB
Swift
253 lines
10 KiB
Swift
//
|
||
// MPPositive_LoveArtistsViewController.swift
|
||
// MusicPlayer
|
||
//
|
||
// Created by Mr.Zhou on 2024/5/28.
|
||
//
|
||
|
||
import UIKit
|
||
|
||
class MPPositive_LoveArtistsViewController: MPPositive_BaseViewController, UIViewControllerTransitioningDelegate {
|
||
///广告View
|
||
fileprivate lazy var adContainerView:UIView = {
|
||
let adContainerView:UIView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 300*width))
|
||
adContainerView.backgroundColor = .clear
|
||
return adContainerView
|
||
}()
|
||
private lazy var numbersLabel:UILabel = createLabel(font: .systemFont(ofSize: 18*width, weight: .regular), textColor: .white, textAlignment: .left)
|
||
///tableView
|
||
private lazy var tableView:UITableView = {
|
||
let tableView = UITableView(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), style: .plain)
|
||
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_LoveArtistTableViewCell.self, forCellReuseIdentifier: MPPositive_LoveArtistTableViewCellID)
|
||
tableView.contentInset = .init(top: 0, left: 0, bottom: 70*width, right: 0)
|
||
return tableView
|
||
}()
|
||
//添加右上角排序按钮
|
||
private lazy var rightBtn:UIButton = {
|
||
let btn:UIButton = .init(frame: .init(x: 0, y: 0, width: 24*width, height: 24*width))
|
||
btn.setBackgroundImage(UIImage(named: "Change Sort'logo"), for: .normal)
|
||
btn.addTarget(self, action: #selector(sortTypeClick(_ :)), for: .touchUpInside)
|
||
return btn
|
||
}()
|
||
//搜索View
|
||
private lazy var searchShowView:MPPositive_CenterListSearchView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: 30*width))
|
||
///搜索按钮
|
||
private lazy var searchBtn:UIButton = {
|
||
let btn = UIButton()
|
||
btn.setBackgroundImage(UIImage(named: "Center_Search'logo"), for: .normal)
|
||
btn.addTarget(self, action: #selector(searchActionShowClick(_ :)), for: .touchUpInside)
|
||
return btn
|
||
}()
|
||
private let MPPositive_LoveArtistTableViewCellID = "MPPositive_LoveArtistTableViewCell"
|
||
//是否搜索模式
|
||
private var isSearchStyle:Bool = false
|
||
//全部数据
|
||
private var artists:[MPPositive_CollectionArtistViewModel] = []
|
||
//搜索模式展示的数据组
|
||
private var searchArtists:[MPPositive_CollectionArtistViewModel] = []
|
||
//常态模式展示的数据组
|
||
private var showArtists:[MPPositive_CollectionArtistViewModel] = []
|
||
//当前状态
|
||
private var sortType:Int{
|
||
get{
|
||
if let type = UserDefaults.standard.object(forKey: "Love_Artists_SortType") as? Int {
|
||
return type
|
||
}else {
|
||
return 0
|
||
}
|
||
}
|
||
}
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
setTitle("Love Artists".localizableString())
|
||
setPopBtn()
|
||
configure()
|
||
MP_AdMobManager.shared.layoutLibraryNativeAd(in: adContainerView, index: 1)
|
||
MP_AdMobManager.shared.onLibraryNativeAdBlock = {
|
||
[weak adContainerView] in
|
||
guard let adContainerView = adContainerView else {return}
|
||
MP_AdMobManager.shared.layoutLibraryNativeAd(in: adContainerView, index: 1)
|
||
}
|
||
searchShowView.cancelBlock = {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
cancelSearchAction()
|
||
}
|
||
searchShowView.textBlock = {
|
||
[weak self] (text) in
|
||
guard let self = self else {return}
|
||
isSearchStyle = true
|
||
//获得文本回调,根据文本筛选数据
|
||
reloadSearch(text)
|
||
}
|
||
}
|
||
override func viewWillAppear(_ animated: Bool) {
|
||
super.viewWillAppear(animated)
|
||
reload()
|
||
}
|
||
//刷新列表
|
||
private func reload() {
|
||
MPPositive_LoadCoreModel.shared.reloadCollectionArtistViewModels {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
artists = MPPositive_LoadCoreModel.shared.artistViewModels
|
||
numbersLabel.text = "\(artists.count) \("Artists".localizableString())"
|
||
isSearchStyle ? reloadSearch(self.searchShowView.textField.text ?? ""):reloadShow()
|
||
}
|
||
}
|
||
//刷新常态展示组
|
||
private func reloadShow() {
|
||
tableView.showMessage(artists.count, title: "No Artists")
|
||
switch sortType {
|
||
case 0://从新到旧
|
||
showArtists = artists.sorted(by: { item1, item2 in
|
||
if let date1 = item1.collectionArtist.addTime, let date2 = item2.collectionArtist.addTime {
|
||
return date1 > date2
|
||
}else {
|
||
return false
|
||
}
|
||
})
|
||
default://从旧到新
|
||
showArtists = artists.sorted(by: { item1, item2 in
|
||
if let date1 = item1.collectionArtist.addTime, let date2 = item2.collectionArtist.addTime {
|
||
return date1 < date2
|
||
}else {
|
||
return true
|
||
}
|
||
})
|
||
}
|
||
tableView.reloadData()
|
||
}
|
||
//刷新搜索组
|
||
private func reloadSearch(_ text:String) {
|
||
if text.isEmpty {
|
||
//当前输入文本为空,展示所有数据
|
||
searchArtists = artists
|
||
}else {
|
||
//当前输入文本不为空,筛选文本
|
||
searchArtists = artists.filter({($0.title ?? "").contains(text)})
|
||
}
|
||
tableView.reloadSections(.init(integer: 0), with: .automatic)
|
||
}
|
||
private func configure() {
|
||
navView.addSubview(rightBtn)
|
||
rightBtn.snp.makeConstraints { make in
|
||
make.width.height.equalTo(24*width)
|
||
make.centerY.equalToSuperview()
|
||
make.right.equalToSuperview().offset(-16*width)
|
||
}
|
||
navView.addSubview(searchBtn)
|
||
searchBtn.snp.makeConstraints { make in
|
||
make.right.equalTo(rightBtn.snp.left).offset(-10*width)
|
||
make.width.height.equalTo(24*width)
|
||
make.centerY.equalTo(rightBtn)
|
||
}
|
||
navView.addSubview(searchShowView)
|
||
searchShowView.snp.makeConstraints { make in
|
||
make.height.equalTo(40*width)
|
||
make.left.equalTo(popBtn.snp.right).offset(5*width)
|
||
make.right.equalTo(rightBtn)
|
||
make.centerY.equalTo(searchBtn)
|
||
}
|
||
searchShowView.alpha = 0
|
||
searchShowView.isHidden = true
|
||
searchShowView.isUserInteractionEnabled = false
|
||
view.addSubview(adContainerView)
|
||
adContainerView.snp.makeConstraints { make in
|
||
make.left.right.equalToSuperview()
|
||
make.top.equalTo(navView.snp.bottom)
|
||
make.height.equalTo(0)
|
||
}
|
||
view.addSubview(numbersLabel)
|
||
numbersLabel.snp.makeConstraints { make in
|
||
make.left.equalToSuperview().offset(18*width)
|
||
make.top.equalTo(adContainerView.snp.bottom).offset(32*width)
|
||
}
|
||
view.addSubview(tableView)
|
||
tableView.snp.makeConstraints { make in
|
||
make.top.equalTo(numbersLabel.snp.bottom).offset(18*width)
|
||
make.left.right.bottom.equalToSuperview()
|
||
}
|
||
}
|
||
//弹出排序框
|
||
@objc private func sortTypeClick(_ sender:UIButton) {
|
||
view.endEditing(true)
|
||
//关闭搜索模式
|
||
cancelSearchAction()
|
||
MPPositive_ModalType = .SortType
|
||
let sortVC = MPPositive_SortTypeViewController(sortType)
|
||
sortVC.chooseBlock = {
|
||
[weak self] (type) in
|
||
guard let self = self else {return}
|
||
UserDefaults.standard.set(type, forKey: "Love_Artists_SortType")
|
||
reloadShow()
|
||
}
|
||
sortVC.transitioningDelegate = self
|
||
sortVC.modalPresentationStyle = .custom
|
||
present(sortVC, animated: true)
|
||
}
|
||
//搜索
|
||
@objc private func searchActionShowClick(_ sender:UIButton) {
|
||
//点击搜索时,展示搜索框
|
||
UIView.animate(withDuration: 0.2) {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
searchShowView.isHidden = false
|
||
//调整searchView的透明度
|
||
searchShowView.alpha = 1
|
||
searchBtn.isUserInteractionEnabled = false
|
||
} completion: { [weak self] statu in
|
||
guard let self = self else {return}
|
||
searchShowView.isUserInteractionEnabled = true
|
||
}
|
||
}
|
||
//取消搜索事件
|
||
private func cancelSearchAction() {
|
||
UIView.animate(withDuration: 0.2) {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
searchShowView.isUserInteractionEnabled = false
|
||
searchShowView.alpha = 0
|
||
isSearchStyle = false
|
||
} completion: { [weak self] statu in
|
||
guard let self = self else {return}
|
||
searchShowView.isHidden = true
|
||
searchBtn.isUserInteractionEnabled = true
|
||
reloadShow()
|
||
}
|
||
}
|
||
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
|
||
return MPPositive_PresentationController(presentedViewController: presented, presenting: presenting)
|
||
}
|
||
}
|
||
//MARK: - tableView
|
||
extension MPPositive_LoveArtistsViewController: UITableViewDataSource, UITableViewDelegate {
|
||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||
return isSearchStyle ? searchArtists.count:showArtists.count
|
||
}
|
||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||
let cell = tableView.dequeueReusableCell(withIdentifier: MPPositive_LoveArtistTableViewCellID, for: indexPath) as! MPPositive_LoveArtistTableViewCell
|
||
if isSearchStyle {
|
||
cell.artistViewModel = searchArtists[indexPath.row]
|
||
}else {
|
||
cell.artistViewModel = showArtists[indexPath.row]
|
||
}
|
||
return cell
|
||
}
|
||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||
let item = isSearchStyle ? searchArtists[indexPath.row]:showArtists[indexPath.row]
|
||
//列表专辑
|
||
let artistVC = MPPositive_ArtistShowViewController(item.collectionArtist.artistId ?? "", clickTrackingParams: "")
|
||
navigationController?.pushViewController(artistVC, animated: true)
|
||
}
|
||
}
|