fix: code review notes

This commit is contained in:
Nikita Semenov 2022-08-11 02:25:58 +03:00
parent 95651b2b58
commit 0abda665bf
8 changed files with 66 additions and 33 deletions

View File

@ -70,6 +70,7 @@ open class DefaultFilterCollectionCell: ContainerCollectionViewCell<UILabel>,
backgroundColor = cellAppearance.normalBgColor
layer.borderColor = cellAppearance.normalBorderColor.cgColor
layer.borderWidth = cellAppearance.normalBorderWidth
case .selected:
wrappedView.textColor = cellAppearance.selectedFontColor
backgroundColor = cellAppearance.selectedBgColor

View File

@ -0,0 +1,30 @@
//
// Copyright (c) 2022 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
public extension Array where Element: FilterPropertyValueRepresenter {
func contains(_ property: Element) -> Bool {
contains(where: { $0.id == property.id })
}
}

View File

@ -26,31 +26,31 @@ import TIUIKitCore
@available(iOS 13, *)
public extension UICollectionViewLayout {
static func gridLayout(_ conf: FiltersLayoutConfiguration) -> UICollectionViewLayout {
let item = NSCollectionLayoutItem(layoutSize: conf.itemSize)
static func gridLayout(_ configuration: FiltersLayoutConfiguration) -> UICollectionViewLayout {
let item = NSCollectionLayoutItem(layoutSize: configuration.itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),
heightDimension: conf.itemSize.heightDimension)
heightDimension: configuration.itemSize.heightDimension)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.interItemSpacing = .fixed(conf.horizontalItemSpacing)
group.interItemSpacing = .fixed(configuration.horizontalItemSpacing)
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(insets: conf.contentInsets)
section.interGroupSpacing = conf.verticalItemSpacing
section.contentInsets = NSDirectionalEdgeInsets(insets: configuration.contentInsets)
section.interGroupSpacing = configuration.verticalItemSpacing
return UICollectionViewCompositionalLayout(section: section)
}
static func horizontalScrollLayout(_ conf: FiltersLayoutConfiguration) -> UICollectionViewLayout {
let item = NSCollectionLayoutItem(layoutSize: conf.itemSize)
static func horizontalScrollLayout(_ configuration: FiltersLayoutConfiguration) -> UICollectionViewLayout {
let item = NSCollectionLayoutItem(layoutSize: configuration.itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: conf.itemSize.widthDimension,
let groupSize = NSCollectionLayoutSize(widthDimension: configuration.itemSize.widthDimension,
heightDimension: .fractionalHeight(1))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(insets: conf.contentInsets)
section.interGroupSpacing = conf.horizontalItemSpacing
section.contentInsets = NSDirectionalEdgeInsets(insets: configuration.contentInsets)
section.interGroupSpacing = configuration.horizontalItemSpacing
section.orthogonalScrollingBehavior = .continuous
return UICollectionViewCompositionalLayout(section: section)

View File

@ -26,17 +26,15 @@ public struct DefaultFilterPropertyValue: FilterPropertyValueRepresenter {
public let id: String
public let title: String
public let excludingProperties: [String]
public let excludingPropertiesIds: [String]
public var isSelected: Bool
}
public extension DefaultFilterPropertyValue {
init(id: String, title: String, excludingProperties: [String] = []) {
public init(id: String, title: String, excludingPropertiesIds: [String] = [], isSelected: Bool = false) {
self.id = id
self.title = title
self.excludingProperties = excludingProperties
self.isSelected = false
self.excludingPropertiesIds = excludingPropertiesIds
self.isSelected = isSelected
}
}

View File

@ -22,6 +22,6 @@
public protocol FilterPropertyValueRepresenter {
var id: String { get }
var excludingProperties: [String] { get }
var excludingPropertiesIds: [String] { get }
var isSelected: Bool { get set }
}

View File

@ -25,9 +25,9 @@ import Foundation
public protocol FilterViewModelProtocol: AnyObject {
associatedtype Property: FilterPropertyValueRepresenter & Hashable
associatedtype CellViewModel: FilterCellViewModelProtocol & Hashable
associatedtype CellViewModelType: FilterCellViewModelProtocol & Hashable
typealias Change = (indexPath: IndexPath, viewModel: CellViewModel)
typealias Change = (indexPath: IndexPath, viewModel: CellViewModelType)
var properties: [Property] { get set }
var selectedProperties: [Property] { get set }
@ -70,15 +70,15 @@ public extension FilterViewModelProtocol {
}
private func excludeProperties(_ filter: Property) -> [Property] {
let propertiesToExclude = filter.excludingProperties
let propertiesIdsToExclude = filter.excludingPropertiesIds
guard !propertiesToExclude.isEmpty else {
guard !propertiesIdsToExclude.isEmpty else {
return []
}
var excludedProperties = [Property]()
for propertiesIdToExclude in propertiesToExclude {
for propertiesIdToExclude in propertiesIdsToExclude {
let propertyToExclude = selectedProperties.first { property in
property.id == propertiesIdToExclude
}

View File

@ -29,7 +29,7 @@ open class BaseFilterViewModel<CellViewModelType: FilterCellViewModelProtocol &
// MARK: - FilterViewModelProtocol
public typealias Property = PropertyValue
public typealias CellViewModel = CellViewModelType
public typealias CellViewModelType = CellViewModelType
public var properties: [PropertyValue] = [] {
didSet {
@ -54,18 +54,18 @@ open class BaseFilterViewModel<CellViewModelType: FilterCellViewModelProtocol &
open func filterDidSelected(atIndexPath indexPath: IndexPath) -> [Change] {
let (selected, deselected) = toggleProperty(atIndexPath: indexPath)
let changedFilters = properties
let changedProperties = properties
.enumerated()
.filter { isPropertyInArray($0.element, properties: selected) || isPropertyInArray($0.element, properties: deselected) }
.filter { selected.contains($0.element) || deselected.contains($0.element) }
for (offset, element) in changedFilters {
let isSelected = isPropertyInArray(element, properties: selectedProperties)
changedProperties.forEach { index, element in
let isSelected = selectedProperties.contains(element)
setSelectedCell(atIndex: offset, isSelected: isSelected)
properties[offset].isSelected = isSelected
setSelectedCell(atIndex: index, isSelected: isSelected)
setSelectedProperty(atIndex: index, isSelected: isSelected)
}
let changedItems = changedFilters
let changedItems = changedProperties
.map {
Change(indexPath: IndexPath(item: $0.offset, section: .zero),
viewModel: cellsViewModels[$0.offset])
@ -78,6 +78,10 @@ open class BaseFilterViewModel<CellViewModelType: FilterCellViewModelProtocol &
cellsViewModels[index].isSelected = isSelected
}
open func setSelectedProperty(atIndex index: Int, isSelected: Bool) {
properties[index].isSelected = isSelected
}
open func isPropertyInArray(_ property: PropertyValue, properties: [PropertyValue]) -> Bool {
properties.contains(where: { $0.id == property.id })
}

View File

@ -156,8 +156,8 @@ open class BaseFiltersCollectionView<CellType: UICollectionViewCell & Configurab
cell.configure(with: change.viewModel)
change.viewModel.isSelected
? selectItem(at: change.indexPath, animated: false, scrollPosition: [])
: deselectItem(at: change.indexPath, animated: false)
? selectItem(at: change.indexPath, animated: false, scrollPosition: [])
: deselectItem(at: change.indexPath, animated: false)
}
}
}