Music_Player3/relax.offline.mp3.music/MP/MPPositive/ViewControllers/Search(搜索页)/MPPositive_SearchViewController.swift

307 lines
14 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_SearchViewController.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/4/19.
//
import UIKit
class MPPositive_SearchViewController: MPPositive_BaseViewController {
//
private lazy var bgImageView:UIImageView = {
let imageView:UIImageView = .init(image: .init(named: "B_Home_BG'bg"))
imageView.contentMode = .scaleAspectFill
return imageView
}()
//
private lazy var historyCollectionView:UICollectionView = {
let layout = MPPositive_TagFlowLayout()
layout.delegate = self
let collectionView:UICollectionView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), collectionViewLayout: layout)
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.backgroundColor = .clear
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(MPPositive_SearchTagCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_SearchTagCollectionViewCellID)
return collectionView
}()
private let MPPositive_SearchTagCollectionViewCellID = "MPPositive_SearchTagCollectionViewCell"
//
private lazy var grideCollectionView:UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.itemSize = .init(width: 162*width, height: 60*width)
layout.sectionInset = .init(top: 20*width, left: 18*width, bottom: 70*width, right: 18*width)
let collectionView = UICollectionView(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), collectionViewLayout: layout)
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.backgroundColor = .clear
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(MPPositive_SearchGrideCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_SearchGrideCollectionViewCellID)
return collectionView
}()
fileprivate let MPPositive_SearchGrideCollectionViewCellID = "MPPositive_SearchGrideCollectionViewCell"
//
private lazy var historyLabel:UILabel = createLabel("History", font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .init(hex: "#666666"), textAlignment: .left)
//
private lazy var deleteBtn:UIButton = {
let btn:UIButton = .init()
btn.setBackgroundImage(UIImage(named: "Tag_Delete'logo"), for: .normal)
btn.addTarget(self, action: #selector(deleteClick(_ :)), for: .touchUpInside)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
setTitle("")
configure()
//广
MP_AdMobManager.shared.configureSreachNativeAd(rootController: self)
MP_AdMobManager.shared.loadSearchNativeAd()
NotificationCenter.notificationKey.add(observer: self, selector: #selector(grideReloadAction(_ :)), notificationName: .search_gride_reload)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
MP_AnalyticsManager.shared.search_pvAction()
MPPositive_LoadCoreModel.shared.reloadSearchTags {
[weak self] in
guard let self = self else {return}
historyCollectionView.reloadData()
guard let layout = historyCollectionView.collectionViewLayout as? MPPositive_TagFlowLayout, let height = layout.layoutAttributeds.last?.frame.maxY else {return}
historyCollectionView.snp.updateConstraints { make in
make.height.equalTo(height)
}
}
grideCollectionView.reloadData()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
deinit{
NotificationCenter.default.removeObserver(self)
}
//
private func configure() {
let searchView = createSearchView()
navView.addSubview(searchView)
searchView.snp.makeConstraints { make in
make.width.equalTo(339*width)
make.height.equalTo(32*width)
make.center.equalToSuperview()
}
view.addSubview(bgImageView)
bgImageView.snp.makeConstraints { make in
make.top.right.left.equalToSuperview()
make.height.equalTo(981*width)
}
view.addSubview(deleteBtn)
deleteBtn.snp.makeConstraints { make in
make.width.height.equalTo(24*width)
make.right.equalToSuperview().offset(-18*width)
make.top.equalTo(navView.snp.bottom).offset(24*width)
}
view.addSubview(historyLabel)
historyLabel.snp.makeConstraints { make in
make.centerY.equalTo(deleteBtn)
make.left.equalToSuperview().offset(18*width)
}
view.addSubview(historyCollectionView)
historyCollectionView.snp.makeConstraints { make in
make.left.right.equalToSuperview()
make.top.equalTo(deleteBtn.snp.bottom).offset(6*width)
make.height.equalTo(30*width)
}
view.addSubview(grideCollectionView)
grideCollectionView.snp.makeConstraints { make in
make.top.equalTo(historyCollectionView.snp.bottom)
make.left.right.bottom.equalToSuperview()
}
}
//
private func createSearchView() -> UIView{
let searchView:UIView = UIView()
searchView.backgroundColor = .init(hex: "#212121")
searchView.isUserInteractionEnabled = true
searchView.layer.masksToBounds = true
searchView.layer.cornerRadius = 16*width
//icon
let iconImageView = UIImageView(image: .init(named: "Search_ICON'logo"))
searchView.addSubview(iconImageView)
iconImageView.snp.makeConstraints { make in
make.height.width.equalTo(16*width)
make.left.equalToSuperview().offset(16*width)
make.centerY.equalToSuperview()
}
let label = createLabel("Search songs,artists,playlists", font: .systemFont(ofSize: 14*width, weight: .regular), textColor: .init(hex: "#666666"), textAlignment: .left)
searchView.addSubview(label)
label.snp.makeConstraints { make in
make.left.equalTo(iconImageView.snp.right).offset(8*width)
make.centerY.equalToSuperview()
}
searchView.isUserInteractionEnabled = true
searchView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(searchClick(_:))))
return searchView
}
//
@objc private func grideReloadAction(_ sender:Notification) {
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
grideCollectionView.reloadData()
}
}
//
@objc fileprivate func searchClick(_ sender:UITapGestureRecognizer) {
let resultVC = MPPositive_SearchResultShowViewController()
navigationController?.pushViewController(resultVC, animated: false)
}
//
@objc private func deleteClick(_ sender:UIButton) {
//
MPPositive_SearchTagModel.fetchAll { [weak self] items in
guard let self = self else {return}
items.forEach({(MPPositive_SearchTagModel.delete($0))})
guard let layout = historyCollectionView.collectionViewLayout as? MPPositive_TagFlowLayout else {return}
layout.originxArray.removeAll()
layout.originyArray.removeAll()
layout.layoutAttributeds.removeAll()
historyCollectionView.reloadData()
historyCollectionView.snp.updateConstraints { make in
make.height.equalTo(10)
}
}
}
}
//MARK: - collectionView
extension MPPositive_SearchViewController: UICollectionViewDataSource, UICollectionViewDelegate, MPPositive_TagLayoutDelegate {
func waterFlowLayout(_ layout: MPPositive_TagFlowLayout, indexPath: IndexPath) -> CGFloat {
let text = MPPositive_LoadCoreModel.shared.searchTags[indexPath.row].text ?? ""
let textWidth = text.textAutoWidth(height: 11.5 * width, font: .systemFont(ofSize: 12 * width, weight: .medium)) + (43 * width)
return textWidth
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == historyCollectionView {
return MPPositive_LoadCoreModel.shared.searchTags.count > 10 ? 10:MPPositive_LoadCoreModel.shared.searchTags.count
}else {
return MPPositive_GridLoadViewModel.shared.grideViewModels.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == historyCollectionView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_SearchTagCollectionViewCellID, for: indexPath) as! MPPositive_SearchTagCollectionViewCell
cell.setText(MPPositive_LoadCoreModel.shared.searchTags[indexPath.row].text)
return cell
}else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_SearchGrideCollectionViewCellID, for: indexPath) as! MPPositive_SearchGrideCollectionViewCell
cell.gride = MPPositive_GridLoadViewModel.shared.grideViewModels[indexPath.row]
return cell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == historyCollectionView {
let text = MPPositive_LoadCoreModel.shared.searchTags[indexPath.row].text ?? ""
let resultVC = MPPositive_SearchResultShowViewController(text)
navigationController?.pushViewController(resultVC, animated: false)
}else {
if indexPath.row <= (MPPositive_GridLoadViewModel.shared.grideViewModels.count - 1) {
let item = MPPositive_GridLoadViewModel.shared.grideViewModels[indexPath.row]
MP_AnalyticsManager.shared.grid_mood_clickAction(item.title ?? "")
let moodVC = MPPositive_GrideMoodViewController(item.grid.browseId, params: item.grid.params, title: item.title ?? "")
navigationController?.pushViewController(moodVC, animated: false)
}
}
}
}
//MARK: -
protocol MPPositive_TagLayoutDelegate {
func waterFlowLayout(_ layout:MPPositive_TagFlowLayout,indexPath:IndexPath) -> CGFloat
}
//MARK: -
///layout
class MPPositive_TagFlowLayout: UICollectionViewFlowLayout {
//
var rowHeight:CGFloat = 22 * width
//
var delegate:MPPositive_TagLayoutDelegate?
//x
var originxArray:[CGFloat]!
//y
var originyArray:[CGFloat]!
//
var layoutAttributeds:[UICollectionViewLayoutAttributes] = []
override init() {
super.init()
//
minimumInteritemSpacing = 8 * width
//
minimumLineSpacing = 8 * width
//
sectionInset = .init(top: 5 * width, left: 18 * width, bottom: 5 * width, right: 18 * width)
scrollDirection = .vertical
//
originxArray = [CGFloat]()
originyArray = [CGFloat]()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: -
//
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
override func prepare() {
super.prepare()
}
//itemlayoutAttributes
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let array = super.layoutAttributesForElements(in: rect)
var mutArray = [UICollectionViewLayoutAttributes]()
array?.forEach({ (attrs) in
let theAttrs = layoutAttributesForItem(at: attrs.indexPath)
mutArray.append(theAttrs!)
})
layoutAttributeds = mutArray
return mutArray
}
//itemlayoutAttributes
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
//x
var x = sectionInset.left
var y = sectionInset.top
//cellxy
let preRow = indexPath.row - 1
if preRow >= 0 {
//cell
if originyArray.count > preRow {
//xy
x = originxArray[preRow]
y = originyArray[preRow]
}
let preIndexPath = IndexPath(item: preRow, section: indexPath.section)
//
let preWidth = delegate?.waterFlowLayout(self, indexPath: preIndexPath)
x += preWidth! + minimumInteritemSpacing
}
var currentWidth = delegate?.waterFlowLayout(self, indexPath: indexPath)
//cell
currentWidth = min(currentWidth!, collectionView!.frame.size.width - sectionInset.left - sectionInset.right)
if x + currentWidth! > collectionView!.frame.size.width - sectionInset.right {
//
x = self.sectionInset.left
y += rowHeight + minimumLineSpacing
}
//
let attrs = UICollectionViewLayoutAttributes.init(forCellWith: indexPath)
attrs.frame = CGRect(x: x, y: y, width: currentWidth!, height: rowHeight)
originxArray.insert(x, at: indexPath.row)
originyArray.insert(y, at: indexPath.row)
return attrs
}
}