updated to selector base

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-10 10:11:03 -05:00
parent 63d8ad2d25
commit a55bea38de
4 changed files with 60 additions and 48 deletions

View File

@ -38,6 +38,7 @@
EA3362452892F9130071C351 /* Labelable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3362442892F9130071C351 /* Labelable.swift */; }; EA3362452892F9130071C351 /* Labelable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3362442892F9130071C351 /* Labelable.swift */; };
EA33624728931B050071C351 /* Initable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA33624628931B050071C351 /* Initable.swift */; }; EA33624728931B050071C351 /* Initable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA33624628931B050071C351 /* Initable.swift */; };
EA3C3B4C2894823E000CA526 /* AnyProxy-PropertyWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3C3B4B2894823E000CA526 /* AnyProxy-PropertyWrapper.swift */; }; EA3C3B4C2894823E000CA526 /* AnyProxy-PropertyWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3C3B4B2894823E000CA526 /* AnyProxy-PropertyWrapper.swift */; };
EAB1D29428A3ECF700DAE764 /* SelectorGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F13028A17FAB00B287F5 /* SelectorGroup.swift */; };
EAF7F0952899861000B287F5 /* Checkbox.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0932899861000B287F5 /* Checkbox.swift */; }; EAF7F0952899861000B287F5 /* Checkbox.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0932899861000B287F5 /* Checkbox.swift */; };
EAF7F0962899861000B287F5 /* CheckboxModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0942899861000B287F5 /* CheckboxModel.swift */; }; EAF7F0962899861000B287F5 /* CheckboxModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0942899861000B287F5 /* CheckboxModel.swift */; };
EAF7F09A2899B17200B287F5 /* CATransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0992899B17200B287F5 /* CATransaction.swift */; }; EAF7F09A2899B17200B287F5 /* CATransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0992899B17200B287F5 /* CATransaction.swift */; };
@ -140,8 +141,8 @@
EAF7F11628A1475A00B287F5 /* RadioButtonModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RadioButtonModel.swift; sourceTree = "<group>"; }; EAF7F11628A1475A00B287F5 /* RadioButtonModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RadioButtonModel.swift; sourceTree = "<group>"; };
EAF7F12B28A1617600B287F5 /* SelectorBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectorBase.swift; sourceTree = "<group>"; }; EAF7F12B28A1617600B287F5 /* SelectorBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectorBase.swift; sourceTree = "<group>"; };
EAF7F12E28A1619600B287F5 /* SelectorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectorModel.swift; sourceTree = "<group>"; }; EAF7F12E28A1619600B287F5 /* SelectorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectorModel.swift; sourceTree = "<group>"; };
EAF7F13028A17FAB00B287F5 /* SelectorGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectorGroup.swift; sourceTree = "<group>"; };
EAF7F13228A2A16500B287F5 /* LabelAttributeAttachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelAttributeAttachment.swift; sourceTree = "<group>"; }; EAF7F13228A2A16500B287F5 /* LabelAttributeAttachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelAttributeAttachment.swift; sourceTree = "<group>"; };
EAF7F13028A17FAB00B287F5 /* RadioButtonGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadioButtonGroup.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -364,7 +365,6 @@
EAF7F11428A1470D00B287F5 /* RadioButton */ = { EAF7F11428A1470D00B287F5 /* RadioButton */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
EAF7F13028A17FAB00B287F5 /* RadioButtonGroup.swift */,
EAF7F11528A1475A00B287F5 /* RadioButton.swift */, EAF7F11528A1475A00B287F5 /* RadioButton.swift */,
EAF7F11628A1475A00B287F5 /* RadioButtonModel.swift */, EAF7F11628A1475A00B287F5 /* RadioButtonModel.swift */,
); );
@ -375,6 +375,7 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
EAF7F12B28A1617600B287F5 /* SelectorBase.swift */, EAF7F12B28A1617600B287F5 /* SelectorBase.swift */,
EAF7F13028A17FAB00B287F5 /* SelectorGroup.swift */,
EAF7F12E28A1619600B287F5 /* SelectorModel.swift */, EAF7F12E28A1619600B287F5 /* SelectorModel.swift */,
); );
path = SelectorBase; path = SelectorBase;
@ -496,13 +497,13 @@
EA3362322891F2ED0071C351 /* FontStyles.swift in Sources */, EA3362322891F2ED0071C351 /* FontStyles.swift in Sources */,
EAF7F0B5289C126F00B287F5 /* UILabel.swift in Sources */, EAF7F0B5289C126F00B287F5 /* UILabel.swift in Sources */,
EAF7F0A6289B0CE000B287F5 /* Resetable.swift in Sources */, EAF7F0A6289B0CE000B287F5 /* Resetable.swift in Sources */,
EAB1D29428A3ECF700DAE764 /* SelectorGroup.swift in Sources */,
EA3361C328902D960071C351 /* Toggle.swift in Sources */, EA3361C328902D960071C351 /* Toggle.swift in Sources */,
EAF7F12C28A1617600B287F5 /* SelectorBase.swift in Sources */, EAF7F12C28A1617600B287F5 /* SelectorBase.swift in Sources */,
EAF7F0A0289AB7EC00B287F5 /* View.swift in Sources */, EAF7F0A0289AB7EC00B287F5 /* View.swift in Sources */,
EAF7F11828A1475A00B287F5 /* RadioButtonModel.swift in Sources */, EAF7F11828A1475A00B287F5 /* RadioButtonModel.swift in Sources */,
EAF7F12F28A1619600B287F5 /* SelectorModel.swift in Sources */, EAF7F12F28A1619600B287F5 /* SelectorModel.swift in Sources */,
EA3362402892EF6C0071C351 /* Label.swift in Sources */, EA3362402892EF6C0071C351 /* Label.swift in Sources */,
EAF7F13128A17FAB00B287F5 /* RadioButtonGroup.swift in Sources */,
EAF7F0B3289B1ADC00B287F5 /* LabelAttributeAction.swift in Sources */, EAF7F0B3289B1ADC00B287F5 /* LabelAttributeAction.swift in Sources */,
EA33622E2891EA3C0071C351 /* DispatchQueue+Once.swift in Sources */, EA33622E2891EA3C0071C351 /* DispatchQueue+Once.swift in Sources */,
EAF7F0AF289B144C00B287F5 /* LabelAttributeUnderline.swift in Sources */, EAF7F0AF289B144C00B287F5 /* LabelAttributeUnderline.swift in Sources */,

View File

@ -9,21 +9,21 @@ import Foundation
import UIKit import UIKit
import VDSColorTokens import VDSColorTokens
import VDSFormControlsTokens import VDSFormControlsTokens
import Combine
public class RadioButton: RadioButtonBase<DefaultRadioButtonModel>{ public class RadioButton: RadioButtonBase<DefaultRadioButtonModel>{
public var selectorController: RadioButtonGroup?
//this is where the code would go for the controller //this is where the code would go for the controller
public override func toggleAndAction() { public override func toggleAndAction() {
if let selectorController = selectorController { if let didSelect = self.didSelect {
selectorController.didSelect(selector: self) didSelect(model.inputId)
} else { } else {
super.toggleAndAction() super.toggleAndAction()
} }
} }
} }
public class RadioButtonGroupModel: SelectorGroupModelBase<DefaultRadioButtonModel> {}
public class RadioButtonGroup: SelectorGroupBase<DefaultRadioButtonModel, RadioButton> {}
open class RadioButtonBase<ModelType: RadioButtonModel>: SelectorBase<ModelType> { open class RadioButtonBase<ModelType: RadioButtonModel>: SelectorBase<ModelType> {
//-------------------------------------------------- //--------------------------------------------------

View File

@ -65,7 +65,7 @@ open class SelectorBase<ModelType: SelectorModel>: Control<ModelType>, Changable
view.translatesAutoresizingMaskIntoConstraints = false view.translatesAutoresizingMaskIntoConstraints = false
return view return view
}() }()
public var didSelect: ((String?) -> ())?
public var onChange: Blocks.ActionBlock? public var onChange: Blocks.ActionBlock?
@Proxy(\.model.id) @Proxy(\.model.id)

View File

@ -8,27 +8,27 @@
import Foundation import Foundation
import UIKit import UIKit
public protocol SelectorGroup: Modelable {
public protocol SelectorGroupModel: Modelable, FormFieldable {
associatedtype SelectorType: SelectorModel associatedtype SelectorType: SelectorModel
var models: [SelectorType] { get set } var selectors: [SelectorType] { get set }
} }
public struct RadioButtonGroupModel: SelectorGroup, FormFieldable{ open class SelectorGroupModelBase<SelectorType: SelectorModel>: SelectorGroupModel{
public var inputId: String? public var inputId: String?
public var value: AnyHashable? public var value: AnyHashable?
public var surface: Surface = .light public var surface: Surface = .light
public var disabled: Bool = false public var disabled: Bool = false
public var models: [DefaultRadioButtonModel] public var selectors: [SelectorType]
public init() { models = [] } required public init() { selectors = [] }
public init(models: [DefaultRadioButtonModel]){ public init(selectors: [SelectorType]){
self.models = models self.selectors = selectors
} }
} }
public class RadioButtonGroup: View<RadioButtonGroupModel>, Changable { open class SelectorGroupBase<SelectorType: SelectorModel, SelectorHandlerType: SelectorBase<SelectorType>>: View<SelectorGroupModelBase<SelectorType>>, Changable {
public var selectorViews: [SelectorHandlerType] = []
public var radioButtons: [RadioButton] = [] public var selectedView: SelectorHandlerType?
public var selectedView: RadioButton?
public var onChange: Blocks.ActionBlock? public var onChange: Blocks.ActionBlock?
//-------------------------------------------------- //--------------------------------------------------
@ -48,23 +48,23 @@ public class RadioButtonGroup: View<RadioButtonGroupModel>, Changable {
//-------------------------------------------------- //--------------------------------------------------
override public var disabled: Bool { override public var disabled: Bool {
didSet { didSet {
let models = model.models.compactMap { existing in let selectors = model.selectors.compactMap { existing in
return updated(existing){ return updated(existing){
$0.disabled = disabled $0.disabled = disabled
} }
} }
model.models = models model.selectors = selectors
} }
} }
override public var surface: Surface { override public var surface: Surface {
didSet { didSet {
let models = model.models.compactMap { existing in let selectors = model.selectors.compactMap { existing in
return updated(existing){ return updated(existing){
$0.surface = surface $0.surface = surface
} }
} }
model.models = models model.selectors = selectors
} }
} }
@ -81,23 +81,34 @@ public class RadioButtonGroup: View<RadioButtonGroupModel>, Changable {
mainStackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true mainStackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
} }
public override func onStateChange(viewModel: RadioButtonGroupModel) { open override func shouldUpdateView(viewModel: ModelType) -> Bool {
super.onStateChange(viewModel: viewModel) return true
}
for radioButtonModel in viewModel.models { open override func updateView(viewModel: ModelType) {
let radioButton = radioButtons.first(where: { existingRadioButton in func findSelectorView(inputId: String) -> SelectorHandlerType? {
return existingRadioButton.model.inputId == radioButtonModel.inputId && radioButtonModel.inputId != nil return selectorViews.first(where: { existingSelectorView in
return existingSelectorView.model.inputId == inputId
}) })
}
//found view for selectorModel in viewModel.selectors {
if let radioButton = radioButton { //see if view is there for the model
radioButton.set(with: radioButtonModel) if let inputId = selectorModel.inputId, let foundSelectorView = findSelectorView(inputId: inputId) {
foundSelectorView.set(with: selectorModel)
} else { } else {
//create view //create view
let newRadioButton = RadioButton(with: radioButtonModel) let newSelectorView = SelectorHandlerType(with: selectorModel)
newRadioButton.selectorController = self newSelectorView.didSelect = {[weak self] (inputId) in
self.radioButtons.append(newRadioButton) guard let self else { return }
mainStackView.addArrangedSubview(newRadioButton) self.selectedView?.isSelected = false
if let inputId = selectorModel.inputId, let selector = findSelectorView(inputId: inputId) {
selector.isSelected = true
self.selectedView = selector
}
}
self.selectorViews.append(newSelectorView)
mainStackView.addArrangedSubview(newSelectorView)
} }
} }
} }
@ -120,7 +131,7 @@ public class RadioButtonGroup: View<RadioButtonGroupModel>, Changable {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Delegate // MARK: - Delegate
//-------------------------------------------------- //--------------------------------------------------
public func didSelect(selector: RadioButton) { public func didSelect(selector: SelectorHandlerType) {
selectedView?.isSelected = false selectedView?.isSelected = false
selector.isSelected = true selector.isSelected = true
selectedView = selector selectedView = selector