implement with CurrentValueSubject

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-15 16:47:57 -05:00
parent 07bd0a538f
commit 46f857e4a8

View File

@ -11,10 +11,9 @@ import VDS
import VDSColorTokens import VDSColorTokens
import Combine import Combine
class CheckboxViewController: UIViewController, StoryboardInitable {
deinit {
print("\(Self.self) deinit") class CheckboxViewController: ModelViewController<DefaultCheckboxModel>, StoryboardInitable {
}
enum PickerType { enum PickerType {
case surface case surface
@ -32,9 +31,7 @@ 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 model = DefaultCheckboxModel()
var checkbox = Checkbox() var checkbox = Checkbox()
var subscribers = Set<AnyCancellable>()
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -50,6 +47,7 @@ class CheckboxViewController: UIViewController, StoryboardInitable {
} }
func createModel() { func createModel() {
var model = DefaultCheckboxModel()
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 = [
@ -74,25 +72,22 @@ class CheckboxViewController: UIViewController, StoryboardInitable {
} }
func setupBinding() { func setupBinding() {
//create the subject
let modelSubject = CurrentValueSubject<DefaultCheckboxModel, Never>(model)
//bind checkbox.createBinding(with: model, storeIn: &subscribers)
checkbox.createBinding(with: modelSubject, storeIn: &subscribers)
//update the model
//print out on subject changes //print out on subject changes
modelSubject model
.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main) .debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main)
.sink { [weak self] updatedModel in .sink { [weak self] updatedModel in
self?.model = updatedModel
self?.showErrorSwitch.isOn = updatedModel.hasError self?.showErrorSwitch.isOn = updatedModel.hasError
print("CheckboxViewController local hasError: \(self?.model.value.hasError)")
print("CheckboxViewController hasError: \(updatedModel.hasError)") print("CheckboxViewController hasError: \(updatedModel.hasError)")
print("CheckboxViewController local selected: \(self?.model.value.selected)")
print("CheckboxViewController selected: \(updatedModel.selected)") print("CheckboxViewController selected: \(updatedModel.selected)")
}.store(in: &subscribers) }
.store(in: &subscribers)
} }
@IBAction func disabledChanged(_ sender: UISwitch) { @IBAction func disabledChanged(_ sender: UISwitch) {
checkbox.disabled = sender.isOn checkbox.disabled = sender.isOn
} }