2021-02-23 15:14:10 +01:00
|
|
|
//
|
|
|
|
// PickServerCategoriesCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by BradGao on 2021/2/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
protocol PickServerCategoriesDataSource: class {
|
|
|
|
func numberOfCategories() -> Int
|
2021-02-26 11:27:47 +01:00
|
|
|
func category(at index: Int) -> MastodonPickServerViewModel.Category
|
2021-02-23 15:14:10 +01:00
|
|
|
func selectedIndex() -> Int
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol PickServerCategoriesDelegate: class {
|
|
|
|
func pickServerCategoriesCell(didSelect index: Int)
|
|
|
|
}
|
|
|
|
|
|
|
|
final class PickServerCategoriesCell: UITableViewCell {
|
|
|
|
|
|
|
|
weak var dataSource: PickServerCategoriesDataSource!
|
|
|
|
weak var delegate: PickServerCategoriesDelegate!
|
|
|
|
|
2021-02-26 09:43:59 +01:00
|
|
|
let metricView = UIView()
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
let collectionView: UICollectionView = {
|
|
|
|
let flowLayout = UICollectionViewFlowLayout()
|
|
|
|
flowLayout.scrollDirection = .horizontal
|
|
|
|
let view = ControlContainableCollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
2021-02-26 09:43:59 +01:00
|
|
|
view.register(PickServerCategoryCollectionViewCell.self, forCellWithReuseIdentifier: String(describing: PickServerCategoryCollectionViewCell.self))
|
2021-02-23 15:14:10 +01:00
|
|
|
view.backgroundColor = .clear
|
|
|
|
view.showsHorizontalScrollIndicator = false
|
|
|
|
view.showsVerticalScrollIndicator = false
|
2021-02-26 09:43:59 +01:00
|
|
|
view.layer.masksToBounds = false
|
2021-02-23 15:14:10 +01:00
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PickServerCategoriesCell {
|
|
|
|
|
|
|
|
private func _init() {
|
|
|
|
self.selectionStyle = .none
|
|
|
|
backgroundColor = .clear
|
2021-02-26 09:43:59 +01:00
|
|
|
|
|
|
|
metricView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(metricView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
metricView.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor),
|
|
|
|
metricView.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor),
|
|
|
|
metricView.topAnchor.constraint(equalTo: contentView.topAnchor),
|
|
|
|
metricView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
|
|
|
|
metricView.heightAnchor.constraint(equalToConstant: 80).priority(.defaultHigh),
|
|
|
|
])
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
contentView.addSubview(collectionView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
collectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
|
|
collectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
|
|
|
|
collectionView.topAnchor.constraint(equalTo: contentView.topAnchor),
|
|
|
|
collectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
|
2021-02-26 09:43:59 +01:00
|
|
|
collectionView.heightAnchor.constraint(equalToConstant: 80).priority(.defaultHigh),
|
2021-02-23 15:14:10 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
collectionView.delegate = self
|
|
|
|
collectionView.dataSource = self
|
|
|
|
}
|
2021-02-26 09:43:59 +01:00
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
|
|
collectionView.collectionViewLayout.invalidateLayout()
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:14:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension PickServerCategoriesCell: UICollectionViewDelegateFlowLayout {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
|
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
|
|
|
|
delegate.pickServerCategoriesCell(didSelect: indexPath.row)
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
2021-02-26 09:43:59 +01:00
|
|
|
layoutIfNeeded()
|
|
|
|
return UIEdgeInsets(top: 0, left: metricView.frame.minX - collectionView.frame.minX, bottom: 0, right: collectionView.frame.maxX - metricView.frame.maxX)
|
2021-02-23 15:14:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
|
|
|
return 16
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
|
|
|
return CGSize(width: 60, height: 80)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PickServerCategoriesCell: UICollectionViewDataSource {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
|
return dataSource.numberOfCategories()
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
|
|
let category = dataSource.category(at: indexPath.row)
|
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PickServerCategoryCollectionViewCell.self), for: indexPath) as! PickServerCategoryCollectionViewCell
|
|
|
|
cell.category = category
|
|
|
|
|
|
|
|
// Select the default category by default
|
|
|
|
if indexPath.row == dataSource.selectedIndex() {
|
|
|
|
// Use `[]` as the scrollPosition to avoid contentOffset change
|
|
|
|
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: [])
|
|
|
|
cell.isSelected = true
|
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|