136 lines
4.4 KiB
Swift
136 lines
4.4 KiB
Swift
//
|
|
// SRFavoritesViewController.swift
|
|
// SynthReel
|
|
//
|
|
// Created by CSGY on 2025/11/21.
|
|
// Copyright © 2025 SR. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import JXPagingView
|
|
import JXSegmentedView
|
|
|
|
class SRFavoritesViewController: SRViewController {
|
|
|
|
var dataArr: [SRShortModel] = []
|
|
|
|
var page = 1
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
self.backgroundImageView.isHidden = true
|
|
self.view.backgroundColor = .clear
|
|
// Do any additional setup after loading the view.
|
|
|
|
view.addSubview(collectionView);
|
|
collectionView.snp.makeConstraints { make in
|
|
make.edges.equalToSuperview()
|
|
}
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
Task {
|
|
await requestDataArr(page: self.page)
|
|
}
|
|
}
|
|
|
|
lazy var collectionViewLayout: UICollectionViewFlowLayout = {
|
|
let itemWidth = floor((UIScreen.width - 30 - 12) / 3.0)
|
|
let itemHeight = 148.0 / 111 * itemWidth
|
|
|
|
let layout = UICollectionViewFlowLayout()
|
|
layout.minimumInteritemSpacing = 6
|
|
layout.minimumLineSpacing = 10
|
|
layout.itemSize = .init(width: itemWidth, height: itemHeight)
|
|
layout.sectionInset = .init(top: 10, left: 15, bottom: 10, right: 15)
|
|
return layout
|
|
}()
|
|
|
|
lazy var collectionView: SRCollectionView = {
|
|
let collectionView = SRCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
|
|
collectionView.delegate = self
|
|
collectionView.dataSource = self
|
|
collectionView.showsVerticalScrollIndicator = false
|
|
collectionView.showsHorizontalScrollIndicator = false
|
|
// collectionView.register(UINib(nibName: "SRHistoryCell", bundle: nil), forCellWithReuseIdentifier: "cell")
|
|
collectionView.register(SRFavoritesCell.self, forCellWithReuseIdentifier: "cell")
|
|
collectionView.sr_addRefreshFooter { [weak self] in
|
|
self?.handleFooterRefresh(nil)
|
|
}
|
|
return collectionView
|
|
}()
|
|
|
|
override func listScrollView() -> UIScrollView {
|
|
return collectionView
|
|
}
|
|
|
|
override func handleHeaderRefresh(_ completer: (() -> Void)?) {
|
|
Task {
|
|
await self.requestDataArr(page: 1)
|
|
completer?()
|
|
}
|
|
}
|
|
|
|
override func handleFooterRefresh(_ completer: (() -> Void)?) {
|
|
Task {
|
|
await self.requestDataArr(page: self.page + 1)
|
|
self.collectionView.sr_endFooterRefreshing()
|
|
completer?()
|
|
}
|
|
}
|
|
}
|
|
|
|
//MARK: UICollectionViewDelegate UICollectionViewDataSource
|
|
extension SRFavoritesViewController: UICollectionViewDelegate, UICollectionViewDataSource {
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SRFavoritesCell
|
|
cell.model = self.dataArr[indexPath.row]
|
|
cell.onCollectChanged = { [weak self] clickedCell in
|
|
guard let self = self else { return }
|
|
|
|
guard let indexPath = self.collectionView.indexPath(for: clickedCell) else {
|
|
return
|
|
}
|
|
self.dataArr.remove(at: indexPath.item)
|
|
self.collectionView.performBatchUpdates {
|
|
self.collectionView.deleteItems(at: [indexPath])
|
|
}
|
|
}
|
|
return cell
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
return self.dataArr.count
|
|
}
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
self.didScrollCallback?(scrollView)
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
let model = dataArr[indexPath.row]
|
|
|
|
let vc = SRDetailPlayerViewController()
|
|
vc.shortId = model.short_play_id
|
|
self.navigationController?.pushViewController(vc, animated: true)
|
|
}
|
|
}
|
|
|
|
extension SRFavoritesViewController {
|
|
|
|
private func requestDataArr(page: Int) async {
|
|
|
|
if let dataArr = await SRHomeApi.requestFavoritesData(page: page) {
|
|
if page == 1 {
|
|
self.dataArr.removeAll()
|
|
}
|
|
self.dataArr += dataArr
|
|
self.page = page
|
|
self.collectionView.reloadData()
|
|
}
|
|
}
|
|
|
|
}
|