ReaderHive/ReaderHive/Class/Explore/V/NRExploreNovelMenuView.swift
2025-12-17 18:04:38 +08:00

102 lines
3.3 KiB
Swift

//
// NRExploreNovelMenuView.swift
// ReaderHive
//
// Created by on 2025/11/25.
//
import UIKit
import SnapKit
class NRExploreNovelMenuView: UIView {
override var intrinsicContentSize: CGSize {
return .init(width: 60, height: UIScreen.height)
}
var didSelected: ((Int) -> Void)?
weak var viewModel: NRExploreNovelViewModel?
private(set) var selectedIndex = 0
private lazy var collectionViewLayout: NRWaterfallFlowLayout = {
let layout = NRWaterfallFlowLayout()
layout.delegate = self
return layout
}()
private lazy var collectionView: NRCollectionView = {
let collectionView = NRCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.showsVerticalScrollIndicator = false
collectionView.register(NRExploreNovelMenuCell.self, forCellWithReuseIdentifier: "cell")
return collectionView
}()
override init(frame: CGRect) {
super.init(frame: frame)
nr_setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension NRExploreNovelMenuView {
private func nr_setupUI() {
addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
}
//AMRK: UICollectionViewDelegate UICollectionViewDataSource
extension NRExploreNovelMenuView: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NRExploreNovelMenuCell
cell.item = self.viewModel?.menuDataArr[indexPath.row]
cell.nr_isSelected = indexPath.row == self.selectedIndex
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.viewModel?.menuDataArr.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard self.selectedIndex != indexPath.row else { return }
self.selectedIndex = indexPath.row
self.collectionView.reloadData()
self.didSelected?(self.selectedIndex)
}
}
//MARK: NRWaterfallMutiSectionDelegate
extension NRExploreNovelMenuView: NRWaterfallMutiSectionDelegate {
func heightForRowAtIndexPath(collectionView collection: UICollectionView, layout: NRWaterfallFlowLayout, indexPath: IndexPath, itemWidth: CGFloat) -> CGFloat {
let item = self.viewModel?.menuDataArr[indexPath.row]
let textHeight = item?.title?.size(.font(ofSize: 10, weight: .regular), .init(width: 60 - 4, height: 100)).height ?? 0
return textHeight + 44
}
func columnNumber(collectionView collection: UICollectionView, layout: NRWaterfallFlowLayout, section: Int) -> Int {
return 1
}
func lineSpacing(collectionView collection: UICollectionView, layout: NRWaterfallFlowLayout, section: Int) -> CGFloat {
return 16
}
}