XSeri/XSeri/Class/Home/Controller/XSHomePopularViewController.swift
2026-03-03 13:53:32 +08:00

166 lines
5.5 KiB
Swift

//
// XSHomePopularViewController.swift
// XSeri
//
// Created by 鸿 on 2025/12/30.
//
import UIKit
import SnapKit
class XSHomePopularViewController: XSHomeChildViewController {
private lazy var viewModel = XSHomeViewModel()
private lazy var layout: XSWaterfallFlowLayout = {
let layout = XSWaterfallFlowLayout()
layout.delegate = self
return layout
}()
private lazy var collectionView: XSCollectionView = {
let collectionView = XSCollectionView(frame: .zero, collectionViewLayout: self.layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.contentInset = .init(top: 0, left: 0, bottom: XSScreen.customTabBarHeight + 10, right: 0)
collectionView.xs_addRefreshHeader { [weak self] in
self?.handleHeaderRefresh(nil)
}
collectionView.register(XSHomePopularCell.self, forCellWithReuseIdentifier: "XSHomePopularCell")
collectionView.register(XSHomePopularBigCell.self, forCellWithReuseIdentifier: "XSHomePopularBigCell")
return collectionView
}()
deinit {
NotificationCenter.default.removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(networkStatusDidChangeNotification), name: XSNetworkMonitorManager.networkStatusDidChangeNotification, object: nil)
xs_setupUI()
Task {
await self.requestDataArr()
}
}
override func handleHeaderRefresh(_ completer: (() -> Void)?) {
Task {
await self.requestDataArr()
self.collectionView.xs_endHeaderRefreshing()
}
}
@objc private func networkStatusDidChangeNotification() {
guard XSNetworkMonitorManager.manager.isReachable == true else { return }
guard self.viewModel.dataArr.isEmpty else { return }
Task {
await self.requestDataArr()
}
}
}
extension XSHomePopularViewController {
private func xs_setupUI() {
view.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.top.equalToSuperview().offset(self.topHeight + 15)
}
}
}
//MARK: UICollectionViewDelegate UICollectionViewDataSource
extension XSHomePopularViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let itemModel = self.viewModel.dataArr[indexPath.section]
let model = itemModel.list?[indexPath.row]
if itemModel.module_key == .week_highest_recommend {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XSHomePopularCell", for: indexPath) as! XSHomePopularCell
cell.model = model
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XSHomePopularBigCell", for: indexPath) as! XSHomePopularBigCell
cell.model = model
return cell
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.viewModel.dataArr[section].list?.count ?? 0
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.viewModel.dataArr.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let itemModel = self.viewModel.dataArr[indexPath.section]
let model = itemModel.list?[indexPath.row]
let vc = XSShortDetailViewController()
vc.shortId = model?.short_play_id
self.navigationController?.pushViewController(vc, animated: true)
}
}
//MARK: XSWaterfallMutiSectionDelegate
extension XSHomePopularViewController: XSWaterfallMutiSectionDelegate {
func heightForRowAtIndexPath(collectionView collection: UICollectionView, layout: XSWaterfallFlowLayout, indexPath: IndexPath, itemWidth: CGFloat) -> CGFloat {
if indexPath.section == 0 {
return 146 + 57
} else {
return 219 + 92
}
}
func columnNumber(collectionView collection: UICollectionView, layout: XSWaterfallFlowLayout, section: Int) -> Int {
if section == 0 {
return 3
} else {
return 2
}
}
func insetForSection(collectionView collection: UICollectionView, layout: XSWaterfallFlowLayout, section: Int) -> UIEdgeInsets {
return .init(top: 0, left: 16, bottom: 0, right: 16)
}
func interitemSpacing(collectionView collection: UICollectionView, layout: XSWaterfallFlowLayout, section: Int) -> CGFloat {
return 13
}
func lineSpacing(collectionView collection: UICollectionView, layout: XSWaterfallFlowLayout, section: Int) -> CGFloat {
return 16
}
func spacingWithLastSection(collectionView collection: UICollectionView, layout: XSWaterfallFlowLayout, section: Int) -> CGFloat {
if section > 0 {
return 26
} else {
return 0
}
}
}
extension XSHomePopularViewController {
private func requestDataArr() async {
await self.viewModel.requestHomeData()
self.collectionView.reloadData()
}
}