// // 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 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" override func viewDidLoad() { super.viewDidLoad() setTitle("") configure() 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() 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(grideCollectionView) grideCollectionView.snp.makeConstraints { make in make.top.equalTo(navView.snp.bottom).offset(15*width) 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".localizableString(), 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.right.equalToSuperview().offset(-16*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) { MP_AnalyticsManager.shared.search_from_actionAction("search page") let resultVC = MPPositive_SearchResultShowViewController() navigationController?.pushViewController(resultVC, animated: false) } } //MARK: - collectionView extension MPPositive_SearchViewController: UICollectionViewDataSource, UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return MPPositive_GridLoadViewModel.shared.grideViewModels.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 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 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) MP_AdMobManager.shared.showSearchInterstitialAdIfAvailable(completion: nil) } } }