fix: view model configuration

This commit is contained in:
Nikita Semenov 2022-08-04 17:01:19 +03:00
parent ef1eb2d8ac
commit 7f6883c7b7
4 changed files with 24 additions and 47 deletions

View File

@ -23,8 +23,5 @@
import UIKit
public protocol FiltersCollectionHolder: AnyObject {
func applyChange(_ changes: [DefaultFiltersViewModel.Change])
func updateView()
func configure(filterCell: UICollectionViewCell, cellViewModel: FilterCellViewModelProtocol)
func registerCells()
}

View File

@ -24,8 +24,7 @@ import UIKit
open class DefaultFiltersViewModel: NSObject,
FiltersViewModelProtocol,
UICollectionViewDelegate,
UICollectionViewDataSource {
UICollectionViewDelegate {
public typealias Change = (indexPath: IndexPath, viewModel: FilterCellViewModelProtocol)
@ -40,21 +39,6 @@ open class DefaultFiltersViewModel: NSObject,
self.cellsViewModels = filters.compactMap { $0.convertToViewModel() as? FilterCellViewModelProtocol }
}
// MARK: - UICollectionViewDataSource
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
filtersCollectionHolder?.registerCells()
return cellsViewModels.count
}
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let viewModel = cellsViewModels[indexPath.item]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: viewModel.id, for: indexPath)
filtersCollectionHolder?.configure(filterCell: cell, cellViewModel: viewModel)
return cell
}
// MARK: - UICollectionViewDelegate
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
@ -70,13 +54,15 @@ open class DefaultFiltersViewModel: NSObject,
filters[offset].isSelected = selectedFilters.contains(element)
}
let changedItems = changedFilters
.map {
Change(indexPath: IndexPath(item: $0.offset, section: .zero),
viewModel: cellsViewModels[$0.offset])
}
filtersCollectionHolder?.updateView()
filtersCollectionHolder?.applyChange(changedItems)
// let changedItems = changedFilters
// .map {
// Change(indexPath: IndexPath(item: $0.offset, section: .zero),
// viewModel: cellsViewModels[$0.offset])
// }
// filtersCollectionHolder?.applyChange(changedItems)
}
// MARK: - Private methods

View File

@ -54,7 +54,6 @@ open class BaseFiltersCollectionView<CellType: UICollectionViewCell & Configurab
super.init(frame: .zero, collectionViewLayout: layout)
initializeView()
viewDidLoad()
}
@ -69,8 +68,7 @@ open class BaseFiltersCollectionView<CellType: UICollectionViewCell & Configurab
}
open func bindViews() {
// delegate = viewModel
// dataSource = viewModel
delegate = viewModel
}
open func configureLayout() {
@ -91,33 +89,29 @@ open class BaseFiltersCollectionView<CellType: UICollectionViewCell & Configurab
// MARK: - FiltersCollectionHolder
open func applyChange(_ changes: [DefaultFiltersViewModel.Change]) {
for change in changes {
guard let cell = cellForItem(at: change.indexPath) else {
continue
}
// open func applyChange(_ changes: [DefaultFiltersViewModel.Change]) {
// for change in changes {
// guard let cell = cellForItem(at: change.indexPath) else {
// continue
// }
configure(filterCell: cell, cellViewModel: change.viewModel)
}
// configure(filterCell: cell, cellViewModel: change.viewModel)
// }
applySnapshot()
}
// applySnapshot()
// }
open func updateView() {
applySnapshot()
}
open func configure(filterCell: UICollectionViewCell, cellViewModel: FilterCellViewModelProtocol) {
guard let cellViewModel = cellViewModel as? CellType.ViewModelType else { return }
// open func configure(filterCell: UICollectionViewCell, cellViewModel: FilterCellViewModelProtocol) {
// guard let cellViewModel = cellViewModel as? CellType.ViewModelType else { return }
guard let configurableCell = filterCell as? CellType else { return }
// guard let configurableCell = filterCell as? CellType else { return }
configurableCell.configure(with: cellViewModel)
}
open func registerCells() {
viewModel?.filters.forEach { self.register(CellType.self, forCellWithReuseIdentifier: $0.id) }
}
// configurableCell.configure(with: cellViewModel)
// }
open func applySnapshot() {
guard let viewModel = viewModel else {