LeadKit/TIUIElements/Sources/Views/DefaultHomogeneousItemsColl.../DefaultHomogeneousItemsColl...

112 lines
3.6 KiB
Swift

//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
import TIUIKitCore
open class DefaultHomogeneousItemsCollectionView<CellContentView: UIView & ConfigurableView & AppearanceConfigurable>:
BaseInitializeableCollectionView,
UICollectionViewDataSource,
ConfigurableView,
AppearanceConfigurable
where CellContentView.Appearance: WrappedViewAppearance {
typealias CellType = CellContentView.InCollectionCell
public private(set) var viewModel: DefaultCollectionViewModel<CellContentView.ViewModelType>?
private var appearance: Appearance = .defaultAppearance
// MARK: - BaseInitializeableCollectionView
override open func bindViews() {
super.bindViews()
register(CellType.self, forCellWithReuseIdentifier: String(describing: CellType.self))
dataSource = self
}
override open func configureLayout() {
super.configureLayout()
collectionViewLayout = createLayout()
}
override open func configureAppearance() {
super.configureAppearance()
isScrollEnabled = false
}
// MARK: - UICollectionViewDataSource
open func numberOfSections(in collectionView: UICollectionView) -> Int {
viewModel?.numberOfSections() ?? .zero
}
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
viewModel?.numberOfItems(inSection: section) ?? .zero
}
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: CellType.self), for: indexPath)
guard let viewModel else {
return cell
}
switch cell {
case let configureableCell as CellType:
configureableCell.configure(with: viewModel.itemViewModel(at: indexPath))
configureableCell.configure(appearance: appearance)
default:
break
}
return cell
}
// MARK: - ConfigurableView
open func configure(with viewModel: DefaultCollectionViewModel<CellContentView.ViewModelType>) {
self.viewModel = viewModel
reloadData()
}
// MARK: - AppearanceConfigurable
open func configure(appearance: DefaultWrappedViewHolderAppearance<CellContentView.Appearance, DefaultWrappedLayout>) {
configureUIView(appearance: appearance)
self.appearance = appearance
}
// MARK: - Subclass override
open func createLayout() -> UICollectionViewLayout {
collectionViewLayout
}
}