refactored checkbox
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
fb9b871345
commit
1b4d40f67e
@ -9,6 +9,7 @@ import Foundation
|
|||||||
import UIKit
|
import UIKit
|
||||||
import VDS
|
import VDS
|
||||||
import VDSColorTokens
|
import VDSColorTokens
|
||||||
|
import Combine
|
||||||
|
|
||||||
class CheckboxViewController: UIViewController, StoryboardInitable {
|
class CheckboxViewController: UIViewController, StoryboardInitable {
|
||||||
enum PickerType {
|
enum PickerType {
|
||||||
@ -27,11 +28,24 @@ class CheckboxViewController: UIViewController, StoryboardInitable {
|
|||||||
@IBOutlet weak var showErrorSwitch: UISwitch!
|
@IBOutlet weak var showErrorSwitch: UISwitch!
|
||||||
@IBOutlet weak var errorTextField: UITextField!
|
@IBOutlet weak var errorTextField: UITextField!
|
||||||
|
|
||||||
var checkbox: Checkbox!
|
var model = DefaultCheckboxModel()
|
||||||
|
var checkbox = Checkbox()
|
||||||
|
var subscribers = Set<AnyCancellable>()
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
var model = DefaultCheckboxModel()
|
checkboxContainerView.addSubview(checkbox)
|
||||||
|
checkbox.leadingAnchor.constraint(equalTo: checkboxContainerView.leadingAnchor, constant: 10).isActive = true
|
||||||
|
checkbox.bottomAnchor.constraint(equalTo: checkboxContainerView.bottomAnchor, constant: -20).isActive = true
|
||||||
|
checkbox.topAnchor.constraint(equalTo: checkboxContainerView.topAnchor, constant: 20).isActive = true
|
||||||
|
checkbox.trailingAnchor.constraint(equalTo: checkboxContainerView.trailingAnchor, constant: 10).isActive = true
|
||||||
|
view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
|
||||||
|
setupPicker()
|
||||||
|
createModel()
|
||||||
|
setupBinding()
|
||||||
|
}
|
||||||
|
|
||||||
|
func createModel() {
|
||||||
model.labelText = "Terms and conditions"
|
model.labelText = "Terms and conditions"
|
||||||
model.childText = "I agree to Verizon's terms and conditions click here"
|
model.childText = "I agree to Verizon's terms and conditions click here"
|
||||||
model.childTextAttributes = [
|
model.childTextAttributes = [
|
||||||
@ -42,25 +56,40 @@ class CheckboxViewController: UIViewController, StoryboardInitable {
|
|||||||
LabelAttributeFont(location: 2, length: 5, style: .BoldTitleLarge, color: UIColor.red.hexString!)
|
LabelAttributeFont(location: 2, length: 5, style: .BoldTitleLarge, color: UIColor.red.hexString!)
|
||||||
]
|
]
|
||||||
model.errorText = "Error Text"
|
model.errorText = "Error Text"
|
||||||
|
|
||||||
|
checkbox.set(with: model)
|
||||||
|
|
||||||
|
//setup UI
|
||||||
surfaceLabel.text = model.surface.rawValue
|
surfaceLabel.text = model.surface.rawValue
|
||||||
disabledSwitch.isOn = model.selected
|
disabledSwitch.isOn = model.selected
|
||||||
labelTextField.text = model.labelText
|
labelTextField.text = model.labelText
|
||||||
childTextField.text = model.childText
|
childTextField.text = model.childText
|
||||||
showErrorSwitch.isOn = model.hasError
|
showErrorSwitch.isOn = model.hasError
|
||||||
errorTextField.text = model.errorText
|
errorTextField.text = model.errorText
|
||||||
|
|
||||||
checkbox = Checkbox(with: model)
|
|
||||||
checkbox.translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
checkboxContainerView.addSubview(checkbox)
|
|
||||||
checkbox.leadingAnchor.constraint(equalTo: checkboxContainerView.leadingAnchor, constant: 10).isActive = true
|
|
||||||
checkbox.bottomAnchor.constraint(equalTo: checkboxContainerView.bottomAnchor, constant: -20).isActive = true
|
|
||||||
checkbox.topAnchor.constraint(equalTo: checkboxContainerView.topAnchor, constant: 20).isActive = true
|
|
||||||
checkbox.trailingAnchor.constraint(equalTo: checkboxContainerView.trailingAnchor, constant: 10).isActive = true
|
|
||||||
view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
|
|
||||||
setupPicker()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupBinding() {
|
||||||
|
//create the subject
|
||||||
|
let modelSubject = CurrentValueSubject<DefaultCheckboxModel, Never>(model)
|
||||||
|
|
||||||
|
//assign - this will auto overwrite any changes
|
||||||
|
modelSubject.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main).assign(to: \.model, on: self).store(in: &subscribers)
|
||||||
|
|
||||||
|
//bind
|
||||||
|
checkbox.createBinding(with: modelSubject, storeIn: &subscribers)
|
||||||
|
|
||||||
|
//print out on subject changes
|
||||||
|
modelSubject.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] model in
|
||||||
|
|
||||||
|
self?.showErrorSwitch.isOn = model.hasError
|
||||||
|
print("CheckboxViewController hasError: \(model.hasError)")
|
||||||
|
print("CheckboxViewController selected: \(model.selected)")
|
||||||
|
|
||||||
|
}.store(in: &subscribers)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@IBAction func disabledChanged(_ sender: UISwitch) {
|
@IBAction func disabledChanged(_ sender: UISwitch) {
|
||||||
checkbox.disabled = sender.isOn
|
checkbox.disabled = sender.isOn
|
||||||
}
|
}
|
||||||
|
|||||||
@ -87,7 +87,7 @@ class RadioButtonViewController: UIViewController, StoryboardInitable {
|
|||||||
radioButtonGroup.createBinding(with: modelSubject, storeIn: &subscribers)
|
radioButtonGroup.createBinding(with: modelSubject, storeIn: &subscribers)
|
||||||
|
|
||||||
//print out on subject changes
|
//print out on subject changes
|
||||||
modelSubject.sink { [weak self] model in
|
modelSubject.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] model in
|
||||||
|
|
||||||
self?.showErrorSwitch.isOn = model.hasError
|
self?.showErrorSwitch.isOn = model.hasError
|
||||||
print("RadioButtonViewController hasError: \(model.hasError)")
|
print("RadioButtonViewController hasError: \(model.hasError)")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user