MoviaBox/Thimra/Class/Home/Controller/SPHomeViewController.swift
2025-04-22 15:21:12 +08:00

211 lines
7.4 KiB
Swift

//
// SPHomeViewController.swift
// Thimra
//
// Created by on 2025/4/8.
//
import UIKit
class SPHomeViewController: SPHomeChildController {
private lazy var viewModel: SPHomeViewModel = SPHomeViewModel()
private lazy var page = 1
private lazy var dataArr: [SPShortModel] = []
//MARK: UI
private lazy var logoImageView: UIImageView = {
let imageView = UIImageView(image: UIImage(named: "logo_icon_01"))
imageView.setContentHuggingPriority(.required, for: .horizontal)
imageView.setContentCompressionResistancePriority(.required, for: .horizontal)
return imageView
}()
private lazy var searchButton: SPHomeSearchButton = {
let button = SPHomeSearchButton()
button.addTarget(self, action: #selector(handleSearchButton), for: .touchUpInside)
return button
}()
private lazy var layout: UICollectionViewFlowLayout = {
let width = floor((kSPScreenWidth - 32 - 13) / 2)
let height = 221 / 165 * width + 44
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: width, height: height)
layout.headerReferenceSize = CGSize(width: kSPScreenWidth, height: SPHomeHeaderView.contentHeight(viewModel: self.viewModel))
layout.sectionInset = .init(top: 0, left: 16, bottom: 0, right: 16)
layout.minimumInteritemSpacing = 13
layout.minimumLineSpacing = 13
return layout
}()
private lazy var collectionView: SPCollectionView = {
let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.sp_addRefreshHeader { [weak self] in
self?.handleHeaderRefresh(nil)
}
collectionView.sp_addRefreshBackFooter(insetBottom: 0) { [weak self] in
self?.handleFooterRefresh(nil)
}
collectionView.register(SPHomeHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView")
SPHomeShortCell.registerCell(collectionView: collectionView)
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityDidChangeNotification), name: SPNetworkReachabilityManager.reachabilityDidChangeNotification, object: nil)
requestModuleData()
requestPlayHistory()
requestListDataArr(page: 1, completer: nil)
_setupUI()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func handleHeaderRefresh(_ completer: (() -> Void)?) {
requestModuleData()
requestPlayHistory()
requestListDataArr(page: 1) { [weak self] in
self?.collectionView.sp_endHeaderRefreshing()
}
}
override func handleFooterRefresh(_ completer: (() -> Void)?) {
requestListDataArr(page: self.page + 1) { [weak self] in
self?.collectionView.sp_endFooterRefreshing()
}
}
}
extension SPHomeViewController {
private func _setupUI() {
view.addSubview(logoImageView)
view.addSubview(searchButton)
view.addSubview(self.collectionView)
logoImageView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(16)
make.centerY.equalTo(searchButton)
}
searchButton.snp.makeConstraints { make in
make.left.equalTo(logoImageView.snp.right).offset(6)
make.right.equalToSuperview().offset(-16)
make.top.equalToSuperview().offset(kSPStatusbarHeight + 10)
}
self.collectionView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
// make.top.equalToSuperview().offset(kSPStatusbarHeight + 66)
make.top.equalTo(searchButton.snp.bottom).offset(34)
}
}
}
extension SPHomeViewController {
@objc private func handleSearchButton() {
let vc = SPSearchViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
@objc private func reachabilityDidChangeNotification() {
}
}
//MARK: -------------- UICollectionViewDelegate & UICollectionViewDataSource --------------
extension SPHomeViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = SPHomeShortCell.dequeueReusableCell(collectionView: collectionView, indexPath: indexPath)
cell.model = dataArr[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArr.count
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath) as? SPHomeHeaderView {
headerView.viewModel = self.viewModel
return headerView
}
return UICollectionReusableView()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let model = self.dataArr[indexPath.row]
let vc = SPPlayerDetailViewController()
vc.shortPlayId = model.short_play_id
self.navigationController?.pushViewController(vc, animated: true)
}
}
extension SPHomeViewController {
private func requestModuleData() {
SPHomeAPI.requestHomeModuleData { [weak self] model in
guard let self = self else { return }
if let model = model {
self.viewModel.moduleModel = model
self.layout.headerReferenceSize = CGSize(width: kSPScreenWidth, height: SPHomeHeaderView.contentHeight(viewModel: self.viewModel))
self.collectionView.reloadData()
}
}
}
///
private func requestPlayHistory() {
SPVideoAPI.requestPlayHistoryList(page: 1) { [weak self] listModel in
guard let self = self else { return }
if let list = listModel?.list {
self.viewModel.playHistoryArr = list
self.layout.headerReferenceSize = CGSize(width: kSPScreenWidth, height: SPHomeHeaderView.contentHeight(viewModel: self.viewModel))
self.collectionView.reloadData()
}
}
}
///
private func requestListDataArr(page: Int, completer: (() -> Void)?) {
SPHomeAPI.requestHomeList(page: page) { [weak self] listModel in
guard let self = self else { return }
if let list = listModel?.list {
if page == 1 {
self.dataArr.removeAll()
}
self.dataArr += list
self.collectionView.reloadData()
self.page = page
}
completer?()
}
}
}