From 0b237b48474028094ff52658a1dc3fb897a9f45e Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 20 Oct 2022 15:07:58 -0500 Subject: [PATCH] updated swatch Signed-off-by: Matt Bruce --- VDS/Classes/CollectionView.swift | 1 - VDS/Classes/CollectionViewCell.swift | 33 ++++++++------ VDS/Classes/Control.swift | 1 - VDS/Components/RadioSwatch/RadioSwatch.swift | 35 ++++++--------- .../RadioSwatch/RadioSwatchGroup.swift | 44 +++++++++---------- 5 files changed, 54 insertions(+), 60 deletions(-) diff --git a/VDS/Classes/CollectionView.swift b/VDS/Classes/CollectionView.swift index e279c41c..d74d88d6 100644 --- a/VDS/Classes/CollectionView.swift +++ b/VDS/Classes/CollectionView.swift @@ -68,7 +68,6 @@ open class CollectionView: UICollectionView, ModelHandlerable, ViewProtocol, Res if !initialSetupPerformed { initialSetupPerformed = true setup() - updateView() } } diff --git a/VDS/Classes/CollectionViewCell.swift b/VDS/Classes/CollectionViewCell.swift index 773ec6e6..753389fd 100644 --- a/VDS/Classes/CollectionViewCell.swift +++ b/VDS/Classes/CollectionViewCell.swift @@ -14,14 +14,26 @@ open class CollectionViewCell: UICo // MARK: - Properties //-------------------------------------------------- private var initialSetupPerformed = false - - public var modelHandler: ModelHandlerType = ModelHandlerType() - - @Proxy(\.modelHandler.surface) - open var surface: Surface - @Proxy(\.modelHandler.disabled) - open var disabled: Bool + public var modelHandler: ModelHandlerType? { + didSet { + if let oldValue { + oldValue.removeFromSuperview() + } + if let modelHandler { + addSubview(modelHandler) + modelHandler.didChange() + modelHandler.topAnchor.constraint(equalTo: topAnchor).isActive = true + modelHandler.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true + modelHandler.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true + modelHandler.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true + } + } + } + + open var surface: Surface = .light { didSet { modelHandler?.surface = surface } } + + open var disabled: Bool = false { didSet{ modelHandler?.disabled = disabled } } //-------------------------------------------------- // MARK: - Initializers @@ -49,7 +61,6 @@ open class CollectionViewCell: UICo if !initialSetupPerformed { initialSetupPerformed = true setup() - modelHandler.updateView() } } @@ -59,12 +70,6 @@ open class CollectionViewCell: UICo translatesAutoresizingMaskIntoConstraints = false insetsLayoutMarginsFromSafeArea = false - addSubview(modelHandler) - - modelHandler.topAnchor.constraint(equalTo: topAnchor).isActive = true - modelHandler.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true - modelHandler.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true - modelHandler.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true } open override func prepareForReuse() { diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index 246b78a6..ee069bf1 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -66,7 +66,6 @@ open class Control: UIControl, ModelHandlerable, ViewProtocol, Resettable { if !initialSetupPerformed { initialSetupPerformed = true setup() - updateView() } } diff --git a/VDS/Components/RadioSwatch/RadioSwatch.swift b/VDS/Components/RadioSwatch/RadioSwatch.swift index 5514cdaa..003feceb 100644 --- a/VDS/Components/RadioSwatch/RadioSwatch.swift +++ b/VDS/Components/RadioSwatch/RadioSwatch.swift @@ -104,9 +104,7 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable addSubview(selectorView) selectorView.addSubview(fillView) - - updateSelector() - + selectorView.topAnchor.constraint(equalTo: topAnchor).isActive = true selectorView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true selectorView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true @@ -126,7 +124,7 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable public override func reset() { super.reset() - updateSelector() + setNeedsDisplay() setAccessibilityLabel() } @@ -139,10 +137,10 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable // MARK: - State //-------------------------------------------------- open override func updateView() { - updateSelector() setAccessibilityHint() setAccessibilityValue(isSelected) setAccessibilityLabel(isSelected) + setNeedsDisplay() } //-------------------------------------------------- @@ -195,8 +193,14 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable return swatchSize } - open func updateSelector() { - //get the colors + open override func layoutSubviews() { + super.layoutSubviews() + // Accounts for any size changes + layer.setNeedsDisplay() + } + + open override func draw(_ layer: CALayer, in ctx: CGContext) { + let backgroundColor = radioSwatchBackgroundColorConfiguration.getColor(self) let borderColor = isSelected ? radioSwatchBorderColorConfiguration.getColor(self) : .clear let fillBorderColor = radioSwatchFillBorderColorConfiguration.getColor(self) @@ -236,23 +240,12 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable fillView.layer.cornerRadius = fillView.bounds.width * 0.5 fillView.layer.borderWidth = selectorBorderWidth fillView.layer.masksToBounds = true - setNeedsDisplay() - } - - open override func layoutSubviews() { - super.layoutSubviews() - // Accounts for any size changes - layer.setNeedsDisplay() - } - - open override func draw(_ layer: CALayer, in ctx: CGContext) { - - let borderColor = radioSwatchBorderColorConfiguration.getColor(self) - + shapeLayer?.removeFromSuperlayer() shapeLayer = nil if strikethrough { + let strikeThroughBorderColor = radioSwatchBorderColorConfiguration.getColor(self) let bounds = selectorView.bounds let length = max(bounds.size.height, bounds.size.width) guard length > 0.0, shapeLayer == nil else { return } @@ -262,7 +255,7 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable strikeThrough.fillColor = nil strikeThrough.opacity = 1.0 strikeThrough.lineWidth = strikeThroughLineThickness - strikeThrough.strokeColor = borderColor.cgColor + strikeThrough.strokeColor = strikeThroughBorderColor.cgColor let linePath = UIBezierPath() linePath.move(to: CGPoint(x: 0, y: bounds.height)) diff --git a/VDS/Components/RadioSwatch/RadioSwatchGroup.swift b/VDS/Components/RadioSwatch/RadioSwatchGroup.swift index 93c7dae0..bba253e9 100644 --- a/VDS/Components/RadioSwatch/RadioSwatchGroup.swift +++ b/VDS/Components/RadioSwatch/RadioSwatchGroup.swift @@ -12,10 +12,7 @@ import Combine public class RadioSwatchGroup: RadioSwatchGroupBase { public override func didSelect(selector: RadioSwatch) { - if let index = selectorViews.firstIndex(where: {$0.isSelected == true }), - let cell = collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? CollectionViewCell { - cell.modelHandler.toggle() - } + selectedHandler?.toggle() selector.toggle() label.text = selector.text valueChanged() @@ -57,7 +54,7 @@ public class RadioSwatchGroupBase: SelectorGr $0.showsVerticalScrollIndicator = false $0.isScrollEnabled = false $0.translatesAutoresizingMaskIntoConstraints = false - $0.register(CollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell") + $0.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell") } }() @@ -66,13 +63,19 @@ public class RadioSwatchGroupBase: SelectorGr //-------------------------------------------------- override public var disabled: Bool { didSet { - updateSelectors() + for selector in selectorViews { + selector.disabled = disabled + } + collectionView.reloadData() } } override public var surface: Surface { didSet { - updateSelectors() + for selector in selectorViews { + selector.surface = surface + } + collectionView.reloadData() } } @@ -139,13 +142,6 @@ public class RadioSwatchGroupBase: SelectorGr collectionView.reloadData() } - private func updateSelectors() { - for selector in selectorViews { - selector.surface = surface - selector.disabled = disabled - } - } - //-------------------------------------------------- // MARK: - UICollectionViewDelegateFlowLayout //-------------------------------------------------- @@ -161,8 +157,7 @@ public class RadioSwatchGroupBase: SelectorGr } open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell else { return } - didSelect(selector: cell.modelHandler) + didSelect(selector: selectorViews[indexPath.row]) } //-------------------------------------------------- @@ -176,15 +171,18 @@ public class RadioSwatchGroupBase: SelectorGr return selectorViews.count } - + var cellsubs: [Int: AnyCancellable] = [:] + public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell else { return UICollectionViewCell() } - - let model = selectorViews[indexPath.row] - cell.modelHandler = selectorViews[indexPath.row] - cell.modelHandler.isUserInteractionEnabled = false + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) + let handler = selectorViews[indexPath.row] + handler.isUserInteractionEnabled = false + cell.addSubview(handler) + handler.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true + handler.leadingAnchor.constraint(equalTo: cell.leadingAnchor).isActive = true + handler.trailingAnchor.constraint(equalTo: cell.trailingAnchor).isActive = true + handler.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true return cell - } open func didSelect(selector: ModelHandlerType) {