refactored to use Protocol extension methods
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
324d00b53b
commit
37372c5487
@ -80,12 +80,6 @@ public class CheckboxGroup: Control<DefaultCheckboxGroupModel>, SelectorGroupHan
|
||||
}
|
||||
|
||||
open override func updateView(viewModel: ModelType) {
|
||||
func findSelectorView(id: UUID) -> ModelHandlerType? {
|
||||
return selectorViews.first(where: { existingSelectorView in
|
||||
return existingSelectorView.model.id == id
|
||||
})
|
||||
}
|
||||
|
||||
for selectorModel in viewModel.selectors {
|
||||
//see if view is there for the model
|
||||
if let foundSelectorView = findSelectorView(id: selectorModel.id) {
|
||||
@ -93,16 +87,7 @@ public class CheckboxGroup: Control<DefaultCheckboxGroupModel>, SelectorGroupHan
|
||||
} else {
|
||||
|
||||
//create view
|
||||
let newSelectorView = ModelHandlerType(with: selectorModel)
|
||||
|
||||
//add model update to the subscribers
|
||||
newSelectorView.handlerPublisher()
|
||||
.sink { [weak self] model in
|
||||
if let cached = self?.getCachedSelector(viewModel: model), newSelectorView.shouldUpdateView(viewModel: cached) {
|
||||
self?.replace(viewModel: model)
|
||||
}
|
||||
}
|
||||
.store(in: &subscribers)
|
||||
let newSelectorView = createModelHandler(selector: selectorModel)
|
||||
|
||||
self.selectorViews.append(newSelectorView)
|
||||
mainStackView.addArrangedSubview(newSelectorView)
|
||||
|
||||
@ -285,10 +285,11 @@ open class RadioBoxBase<ModelType: RadioBoxModel>: Control<ModelType>, Changable
|
||||
$0.forFalse.disabled.lightColor = VDSFormControlsColor.backgroundOnlight
|
||||
$0.forFalse.disabled.darkColor = VDSFormControlsColor.backgroundOndark
|
||||
|
||||
$0.forTrue.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
||||
$0.forTrue.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
||||
$0.forTrue.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
||||
$0.forTrue.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
||||
$0.forTrue.enabled.lightColor = VDSFormControlsColor.backgroundOnlight
|
||||
$0.forTrue.enabled.darkColor = VDSFormControlsColor.backgroundOndark
|
||||
$0.forTrue.disabled.lightColor = VDSFormControlsColor.backgroundOnlight
|
||||
$0.forTrue.disabled.darkColor = VDSFormControlsColor.backgroundOndark
|
||||
|
||||
//error doesn't care enabled/disable
|
||||
$0.error.forTrue.lightColor = VDSColor.elementsPrimaryOnlight
|
||||
$0.error.forTrue.darkColor = VDSColor.elementsPrimaryOndark
|
||||
@ -314,16 +315,7 @@ open class RadioBoxBase<ModelType: RadioBoxModel>: Control<ModelType>, Changable
|
||||
$0.error.forFalse.darkColor = VDSColor.feedbackErrorOndark
|
||||
}
|
||||
}()
|
||||
|
||||
private var radioBoxStrikethroughColorConfiguration: BinarySurfaceColorConfiguration<ModelType> = {
|
||||
return BinarySurfaceColorConfiguration<ModelType>().with {
|
||||
$0.forTrue.lightColor = VDSColor.elementsPrimaryOnlight
|
||||
$0.forTrue.darkColor = VDSColor.elementsPrimaryOndark
|
||||
$0.forFalse.lightColor = VDSColor.elementsSecondaryOnlight
|
||||
$0.forFalse.darkColor = VDSColor.elementsSecondaryOndark
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - RadioBox View Updates
|
||||
//--------------------------------------------------
|
||||
@ -335,7 +327,6 @@ open class RadioBoxBase<ModelType: RadioBoxModel>: Control<ModelType>, Changable
|
||||
//get the colors
|
||||
let backgroundColor = radioBoxBackgroundColorConfiguration.getColor(viewModel)
|
||||
let borderColor = radioBoxBorderColorConfiguration.getColor(viewModel)
|
||||
let strikethroughColor = radioBoxStrikethroughColorConfiguration.getColor(viewModel)
|
||||
let borderWidth = viewModel.selected ? selectorBorderWidthSelected : selectorBorderWidth
|
||||
|
||||
if let shapeLayer = shapeLayer, let sublayers = layer.sublayers, sublayers.contains(shapeLayer) {
|
||||
@ -358,11 +349,11 @@ open class RadioBoxBase<ModelType: RadioBoxModel>: Control<ModelType>, Changable
|
||||
border.fillColor = nil
|
||||
border.opacity = 1.0
|
||||
border.lineWidth = strikeThroughLineThickness
|
||||
border.strokeColor = strikethroughColor.cgColor
|
||||
border.strokeColor = borderColor.cgColor
|
||||
|
||||
let linePath = UIBezierPath()
|
||||
|
||||
let offsetPercent: CGFloat = 0.0
|
||||
let offsetPercent: CGFloat = 0.005
|
||||
linePath.move(to: CGPoint(x: selectorCornerRadius, y: bounds.height * (1 - offsetPercent)))
|
||||
linePath.addLine(to: CGPoint(x: bounds.width - selectorCornerRadius, y: bounds.height * offsetPercent))
|
||||
border.path = linePath.cgPath
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
public class RadioBoxGroup: Control<DefaultRadioBoxGroupModel>, SelectorGroupHandlerable, Changable {
|
||||
public class RadioBoxGroup: Control<DefaultRadioBoxGroupModel>, SelectorGroupSelectedHandlerable, Changable {
|
||||
public typealias ModelHandlerType = RadioBox
|
||||
|
||||
//--------------------------------------------------
|
||||
@ -16,9 +16,22 @@ public class RadioBoxGroup: Control<DefaultRadioBoxGroupModel>, SelectorGroupHan
|
||||
//--------------------------------------------------
|
||||
public var selectorViews: [ModelHandlerType] = []
|
||||
|
||||
@Proxy(\.model.selectedModel)
|
||||
public var selectedModel: ModelHandlerType.ModelType? {
|
||||
didSet{
|
||||
if hasError, selectedModel != nil {
|
||||
hasError = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public var hasError: Bool {
|
||||
get { model.hasError }
|
||||
set {
|
||||
var newHasError = newValue
|
||||
if selectedModel != nil, newHasError {
|
||||
newHasError = false
|
||||
}
|
||||
let selectors = model.selectors.compactMap { existing in
|
||||
return existing.copyWith {
|
||||
$0.hasError = newValue
|
||||
@ -81,12 +94,6 @@ public class RadioBoxGroup: Control<DefaultRadioBoxGroupModel>, SelectorGroupHan
|
||||
}
|
||||
|
||||
open override func updateView(viewModel: ModelType) {
|
||||
func findSelectorView(id: UUID) -> ModelHandlerType? {
|
||||
return selectorViews.first(where: { existingSelectorView in
|
||||
return existingSelectorView.model.id == id
|
||||
})
|
||||
}
|
||||
|
||||
for selectorModel in viewModel.selectors {
|
||||
//see if view is there for the model
|
||||
if let foundSelectorView = findSelectorView(id: selectorModel.id) {
|
||||
@ -94,16 +101,7 @@ public class RadioBoxGroup: Control<DefaultRadioBoxGroupModel>, SelectorGroupHan
|
||||
} else {
|
||||
|
||||
//create view
|
||||
let newSelectorView = ModelHandlerType(with: selectorModel)
|
||||
|
||||
//add model update to the subscribers
|
||||
newSelectorView.handlerPublisher()
|
||||
.sink { [weak self] model in
|
||||
if let cached = self?.getCachedSelector(viewModel: model), newSelectorView.shouldUpdateView(viewModel: cached) {
|
||||
self?.replace(viewModel: model)
|
||||
}
|
||||
}
|
||||
.store(in: &subscribers)
|
||||
let newSelectorView = createModelHandler(selector: selectorModel)
|
||||
|
||||
self.selectorViews.append(newSelectorView)
|
||||
mainStackView.addArrangedSubview(newSelectorView)
|
||||
|
||||
@ -7,9 +7,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol RadioBoxGroupModel: SelectorGroupModelable where SelectorModelType: RadioBoxModel {
|
||||
|
||||
}
|
||||
public protocol RadioBoxGroupModel: SelectorGroupSelectedModelable where SelectorModelType: RadioBoxModel { }
|
||||
|
||||
public struct DefaultRadioBoxGroupModel: RadioBoxGroupModel {
|
||||
public typealias SelectorModelType = DefaultRadioBoxModel
|
||||
@ -19,6 +17,7 @@ public struct DefaultRadioBoxGroupModel: RadioBoxGroupModel {
|
||||
public var surface: Surface = .light
|
||||
public var disabled: Bool = false
|
||||
public var selectors: [SelectorModelType]
|
||||
public var selectedModel: DefaultRadioBoxModel?
|
||||
public var hasError: Bool = false
|
||||
public var errorText: String?
|
||||
public init() { selectors = [] }
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
public class RadioButtonGroup: Control<DefaultRadioButtonGroupModel>, SelectorGroupHandlerable, Changable {
|
||||
public class RadioButtonGroup: Control<DefaultRadioButtonGroupModel>, SelectorGroupSelectedHandlerable, Changable {
|
||||
public typealias ModelHandlerType = RadioButton
|
||||
|
||||
//--------------------------------------------------
|
||||
@ -95,12 +95,6 @@ public class RadioButtonGroup: Control<DefaultRadioButtonGroupModel>, SelectorGr
|
||||
}
|
||||
|
||||
open override func updateView(viewModel: ModelType) {
|
||||
func findSelectorView(id: UUID) -> ModelHandlerType? {
|
||||
return selectorViews.first(where: { existingSelectorView in
|
||||
return existingSelectorView.model.id == id
|
||||
})
|
||||
}
|
||||
|
||||
for selectorModel in viewModel.selectors {
|
||||
//see if view is there for the model
|
||||
if let foundSelectorView = findSelectorView(id: selectorModel.id) {
|
||||
@ -108,46 +102,11 @@ public class RadioButtonGroup: Control<DefaultRadioButtonGroupModel>, SelectorGr
|
||||
} else {
|
||||
|
||||
//create view
|
||||
let newSelectorView = ModelHandlerType(with: selectorModel)
|
||||
|
||||
//add the selectedPublisher for the change
|
||||
newSelectorView.publisher(for: .valueChanged)
|
||||
.sink(receiveValue: { [weak self] control in
|
||||
guard self?.model.selectors.count ?? 0 > 0 else { return }
|
||||
self?.didSelect(selector: control.model)
|
||||
})
|
||||
.store(in: &subscribers)
|
||||
|
||||
//add model update to the subscribers
|
||||
newSelectorView.handlerPublisher()
|
||||
.sink { [weak self] model in
|
||||
if let cached = self?.getCachedSelector(viewModel: model), newSelectorView.shouldUpdateView(viewModel: cached) {
|
||||
self?.replace(viewModel: model)
|
||||
}
|
||||
}
|
||||
.store(in: &subscribers)
|
||||
let newSelectorView = createModelHandler(selector: selectorModel)
|
||||
|
||||
self.selectorViews.append(newSelectorView)
|
||||
mainStackView.addArrangedSubview(newSelectorView)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open func didSelect(selector: ModelHandlerType.ModelType) {
|
||||
if var oldSelectedModel = selectedModel {
|
||||
oldSelectedModel.selected = false
|
||||
replace(viewModel: oldSelectedModel)
|
||||
}
|
||||
|
||||
//only select is selected
|
||||
if selector.selected {
|
||||
var newSelectedModel = selector
|
||||
newSelectedModel.selected = true
|
||||
replace(viewModel: newSelectedModel)
|
||||
selectedModel = newSelectedModel
|
||||
} else {
|
||||
//ensure current value is removed
|
||||
selectedModel = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,9 +7,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol RadioButtonGroupModel: SelectorGroupModelable where SelectorModelType: RadioButtonModel {
|
||||
var selectedModel: SelectorModelType? { get set }
|
||||
}
|
||||
public protocol RadioButtonGroupModel: SelectorGroupSelectedModelable where SelectorModelType: RadioButtonModel { }
|
||||
|
||||
extension RadioButtonGroupModel {
|
||||
public var errorText: String? { return nil }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user