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 {
initialSetupPerformed = true
setup()
updateView()
}
}

View File

@ -14,14 +14,26 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: 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<ModelHandlerType: ModelHandlerable & UIView>: UICo
if !initialSetupPerformed {
initialSetupPerformed = true
setup()
modelHandler.updateView()
}
}
@ -59,12 +70,6 @@ open class CollectionViewCell<ModelHandlerType: ModelHandlerable & UIView>: 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() {

View File

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

View File

@ -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))

View File

@ -12,10 +12,7 @@ import Combine
public class RadioSwatchGroup: RadioSwatchGroupBase<RadioSwatch> {
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<RadioSwatch> {
cell.modelHandler.toggle()
}
selectedHandler?.toggle()
selector.toggle()
label.text = selector.text
valueChanged()
@ -57,7 +54,7 @@ public class RadioSwatchGroupBase<ModelHandlerType: RadioSwatchBase>: SelectorGr
$0.showsVerticalScrollIndicator = false
$0.isScrollEnabled = 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 {
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<ModelHandlerType: RadioSwatchBase>: 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<ModelHandlerType: RadioSwatchBase>: SelectorGr
}
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
didSelect(selector: cell.modelHandler)
didSelect(selector: selectorViews[indexPath.row])
}
//--------------------------------------------------
@ -176,15 +171,18 @@ public class RadioSwatchGroupBase<ModelHandlerType: RadioSwatchBase>: 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<ModelHandlerType> 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) {