updated swatch

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-10-20 15:07:58 -05:00
parent 60dbfa9d2b
commit 0b237b4847
5 changed files with 54 additions and 60 deletions

View File

@ -68,7 +68,6 @@ open class CollectionView: UICollectionView, ModelHandlerable, ViewProtocol, Res
if !initialSetupPerformed { if !initialSetupPerformed {
initialSetupPerformed = true initialSetupPerformed = true
setup() setup()
updateView()
} }
} }

View File

@ -14,14 +14,26 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICo
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
private var initialSetupPerformed = false private var initialSetupPerformed = false
public var modelHandler: ModelHandlerType = ModelHandlerType()
@Proxy(\.modelHandler.surface)
open var surface: Surface
@Proxy(\.modelHandler.disabled) public var modelHandler: ModelHandlerType? {
open var disabled: Bool 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 // MARK: - Initializers
@ -49,7 +61,6 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICo
if !initialSetupPerformed { if !initialSetupPerformed {
initialSetupPerformed = true initialSetupPerformed = true
setup() setup()
modelHandler.updateView()
} }
} }
@ -59,12 +70,6 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: UICo
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = 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() { open override func prepareForReuse() {

View File

@ -66,7 +66,6 @@ open class Control: UIControl, ModelHandlerable, ViewProtocol, Resettable {
if !initialSetupPerformed { if !initialSetupPerformed {
initialSetupPerformed = true initialSetupPerformed = true
setup() setup()
updateView()
} }
} }

View File

@ -104,9 +104,7 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable
addSubview(selectorView) addSubview(selectorView)
selectorView.addSubview(fillView) selectorView.addSubview(fillView)
updateSelector()
selectorView.topAnchor.constraint(equalTo: topAnchor).isActive = true selectorView.topAnchor.constraint(equalTo: topAnchor).isActive = true
selectorView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true selectorView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
selectorView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true selectorView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
@ -126,7 +124,7 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable
public override func reset() { public override func reset() {
super.reset() super.reset()
updateSelector() setNeedsDisplay()
setAccessibilityLabel() setAccessibilityLabel()
} }
@ -139,10 +137,10 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable
// MARK: - State // MARK: - State
//-------------------------------------------------- //--------------------------------------------------
open override func updateView() { open override func updateView() {
updateSelector()
setAccessibilityHint() setAccessibilityHint()
setAccessibilityValue(isSelected) setAccessibilityValue(isSelected)
setAccessibilityLabel(isSelected) setAccessibilityLabel(isSelected)
setNeedsDisplay()
} }
//-------------------------------------------------- //--------------------------------------------------
@ -195,8 +193,14 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable
return swatchSize return swatchSize
} }
open func updateSelector() { open override func layoutSubviews() {
//get the colors super.layoutSubviews()
// Accounts for any size changes
layer.setNeedsDisplay()
}
open override func draw(_ layer: CALayer, in ctx: CGContext) {
let backgroundColor = radioSwatchBackgroundColorConfiguration.getColor(self) let backgroundColor = radioSwatchBackgroundColorConfiguration.getColor(self)
let borderColor = isSelected ? radioSwatchBorderColorConfiguration.getColor(self) : .clear let borderColor = isSelected ? radioSwatchBorderColorConfiguration.getColor(self) : .clear
let fillBorderColor = radioSwatchFillBorderColorConfiguration.getColor(self) 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.cornerRadius = fillView.bounds.width * 0.5
fillView.layer.borderWidth = selectorBorderWidth fillView.layer.borderWidth = selectorBorderWidth
fillView.layer.masksToBounds = true 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?.removeFromSuperlayer()
shapeLayer = nil shapeLayer = nil
if strikethrough { if strikethrough {
let strikeThroughBorderColor = radioSwatchBorderColorConfiguration.getColor(self)
let bounds = selectorView.bounds let bounds = selectorView.bounds
let length = max(bounds.size.height, bounds.size.width) let length = max(bounds.size.height, bounds.size.width)
guard length > 0.0, shapeLayer == nil else { return } guard length > 0.0, shapeLayer == nil else { return }
@ -262,7 +255,7 @@ open class RadioSwatchBase: Control, Accessable, DataTrackable, BinaryColorable
strikeThrough.fillColor = nil strikeThrough.fillColor = nil
strikeThrough.opacity = 1.0 strikeThrough.opacity = 1.0
strikeThrough.lineWidth = strikeThroughLineThickness strikeThrough.lineWidth = strikeThroughLineThickness
strikeThrough.strokeColor = borderColor.cgColor strikeThrough.strokeColor = strikeThroughBorderColor.cgColor
let linePath = UIBezierPath() let linePath = UIBezierPath()
linePath.move(to: CGPoint(x: 0, y: bounds.height)) linePath.move(to: CGPoint(x: 0, y: bounds.height))

View File

@ -12,10 +12,7 @@ import Combine
public class RadioSwatchGroup: RadioSwatchGroupBase<RadioSwatch> { public class RadioSwatchGroup: RadioSwatchGroupBase<RadioSwatch> {
public override func didSelect(selector: RadioSwatch) { public override func didSelect(selector: RadioSwatch) {
if let index = selectorViews.firstIndex(where: {$0.isSelected == true }), selectedHandler?.toggle()
let cell = collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? CollectionViewCell<RadioSwatch> {
cell.modelHandler.toggle()
}
selector.toggle() selector.toggle()
label.text = selector.text label.text = selector.text
valueChanged() valueChanged()
@ -57,7 +54,7 @@ public class RadioSwatchGroupBase<ModelHandlerType: RadioSwatchBase>: SelectorGr
$0.showsVerticalScrollIndicator = false $0.showsVerticalScrollIndicator = false
$0.isScrollEnabled = false $0.isScrollEnabled = false
$0.translatesAutoresizingMaskIntoConstraints = false $0.translatesAutoresizingMaskIntoConstraints = false
$0.register(CollectionViewCell<ModelHandlerType>.self, forCellWithReuseIdentifier: "collectionViewCell") $0.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell")
} }
}() }()
@ -66,13 +63,19 @@ public class RadioSwatchGroupBase<ModelHandlerType: RadioSwatchBase>: SelectorGr
//-------------------------------------------------- //--------------------------------------------------
override public var disabled: Bool { override public var disabled: Bool {
didSet { didSet {
updateSelectors() for selector in selectorViews {
selector.disabled = disabled
}
collectionView.reloadData()
} }
} }
override public var surface: Surface { override public var surface: Surface {
didSet { didSet {
updateSelectors() for selector in selectorViews {
selector.surface = surface
}
collectionView.reloadData()
} }
} }
@ -139,13 +142,6 @@ public class RadioSwatchGroupBase<ModelHandlerType: RadioSwatchBase>: SelectorGr
collectionView.reloadData() collectionView.reloadData()
} }
private func updateSelectors() {
for selector in selectorViews {
selector.surface = surface
selector.disabled = disabled
}
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - UICollectionViewDelegateFlowLayout // MARK: - UICollectionViewDelegateFlowLayout
//-------------------------------------------------- //--------------------------------------------------
@ -161,8 +157,7 @@ public class RadioSwatchGroupBase<ModelHandlerType: RadioSwatchBase>: SelectorGr
} }
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return } didSelect(selector: selectorViews[indexPath.row])
didSelect(selector: cell.modelHandler)
} }
//-------------------------------------------------- //--------------------------------------------------
@ -176,15 +171,18 @@ public class RadioSwatchGroupBase<ModelHandlerType: RadioSwatchBase>: SelectorGr
return selectorViews.count return selectorViews.count
} }
var cellsubs: [Int: AnyCancellable] = [:]
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell<ModelHandlerType> else { return UICollectionViewCell() } let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
let handler = selectorViews[indexPath.row]
let model = selectorViews[indexPath.row] handler.isUserInteractionEnabled = false
cell.modelHandler = selectorViews[indexPath.row] cell.addSubview(handler)
cell.modelHandler.isUserInteractionEnabled = false 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 return cell
} }
open func didSelect(selector: ModelHandlerType) { open func didSelect(selector: ModelHandlerType) {