diff --git a/VDSSample/Classes/Helper.swift b/VDSSample/Classes/Helper.swift index dcae0bd..058ebd0 100644 --- a/VDSSample/Classes/Helper.swift +++ b/VDSSample/Classes/Helper.swift @@ -37,35 +37,43 @@ extension UIView { } } +func labelPublisherCompletionHandler() -> (String, UILabel) -> () { + return { (text, label) in + let newText = "\(text) clicked - " + if let labelText = label.text { + let components = labelText.components(separatedBy: " - ") + let last: String = (components.last ?? "0").trimmingCharacters(in: .whitespaces) + let count = Int(last)! + label.text = "\(newText)\(count+1)" + } else { + label.text = "\(newText)1" + } + } +} + extension Changeable { - func labelPublisher(_ text: String, label: UILabel) { + func onChangeActionPublisher(_ text: String, label: UILabel) { onChange = { _ in - let newText = "\(text) clicked - " - if let labelText = label.text { - let components = labelText.components(separatedBy: " - ") - let last: String = (components.last ?? "0").trimmingCharacters(in: .whitespaces) - let count = Int(last)! - label.text = "\(newText)\(count+1)" - } else { - label.text = "\(newText)1" - } - } + let handler = labelPublisherCompletionHandler() + handler(text, label) + } + } +} + +extension Clickable { + func onClickActionPublisher(_ text: String, label: UILabel) { + onClick = { _ in + let handler = labelPublisherCompletionHandler() + handler(text, label) + } } } extension ButtonBase { func labelPublisher(_ label: UILabel){ onClick = { control in - let newText = "\(control.text!) clicked - " - if let labelText = label.text { - let components = labelText.components(separatedBy: " - ") - let last: String = (components.last ?? "0").trimmingCharacters(in: .whitespaces) - let count = Int(last)! - label.text = "\(newText)\(count+1)" - } else { - label.text = "\(newText)1" - } - print("clicked me") + let handler = labelPublisherCompletionHandler() + handler(control.text!, label) } } } diff --git a/VDSSample/ViewControllers/BaseViewController.swift b/VDSSample/ViewControllers/BaseViewController.swift index f592b0b..8114c1b 100644 --- a/VDSSample/ViewControllers/BaseViewController.swift +++ b/VDSSample/ViewControllers/BaseViewController.swift @@ -251,6 +251,13 @@ public class BaseViewController: UIViewController, Initable { addFormRow(label: "Show Bounds", view: debugViewSwitch) } + let actionLabel = Label() + + @discardableResult + public func addActionRow() -> UIView { + addFormRow(label: "Action", view: actionLabel) + } + public func scrollToBottom() { let bottomOffset = CGPoint(x: 0, y: bottomScrollView.contentSize.height - bottomScrollView.bounds.height + bottomScrollView.contentInset.bottom) bottomScrollView.setContentOffset(bottomOffset, animated: true) diff --git a/VDSSample/ViewControllers/ButtonGroupViewController.swift b/VDSSample/ViewControllers/ButtonGroupViewController.swift index 8e6a104..7a63b3f 100644 --- a/VDSSample/ViewControllers/ButtonGroupViewController.swift +++ b/VDSSample/ViewControllers/ButtonGroupViewController.swift @@ -59,7 +59,6 @@ class ButtonGroupViewController: BaseViewController { items: RowQuantity.allCases) }() - var label = Label() var disabledSwitch = Toggle() var widthTextField = NumericField() var percentageTextField = NumericField() @@ -76,6 +75,8 @@ class ButtonGroupViewController: BaseViewController { stackView.axis = .vertical stackView.spacing = 30 + let label = actionLabel + component.buttons = [ makeButton("Secondary", label: label).with{ $0.use = .secondary }, makeButton("Primary", label: label), @@ -107,8 +108,8 @@ class ButtonGroupViewController: BaseViewController { } override func setupForm(){ - super.setupForm() - addFormRow(label: "Action", view: label) + super.setupForm() + addActionRow() addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Position", view: buttonPositionSelectorView) diff --git a/VDSSample/ViewControllers/ButtonIconViewController.swift b/VDSSample/ViewControllers/ButtonIconViewController.swift index eb8777f..6f31c6b 100644 --- a/VDSSample/ViewControllers/ButtonIconViewController.swift +++ b/VDSSample/ViewControllers/ButtonIconViewController.swift @@ -60,6 +60,7 @@ class ButtonIconViewController: BaseViewController { override func setupForm(){ super.setupForm() + addActionRow() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Surface Type", view: surfaceTypePickerSelectorView) @@ -77,9 +78,7 @@ class ButtonIconViewController: BaseViewController { self?.component.disabled = sender.isOn } - component.onClick = { sender in - print("Button Icon was clicked") - } + component.onClickActionPublisher("ButtonIcon", label: actionLabel) floating.onChange = { [weak self] sender in self?.component.floating = sender.isOn diff --git a/VDSSample/ViewControllers/CheckBoxGroupViewController.swift b/VDSSample/ViewControllers/CheckBoxGroupViewController.swift index 1a649b3..10245e5 100644 --- a/VDSSample/ViewControllers/CheckBoxGroupViewController.swift +++ b/VDSSample/ViewControllers/CheckBoxGroupViewController.swift @@ -28,10 +28,9 @@ class CheckboxGroupViewController: BaseViewController { setupModel() } - var actionLabel = Label() override func setupForm(){ super.setupForm() - addFormRow(label: "Action", view: actionLabel) + addActionRow() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Label Text", view: labelTextField) diff --git a/VDSSample/ViewControllers/CheckboxItemViewController.swift b/VDSSample/ViewControllers/CheckboxItemViewController.swift index 6acb10a..58b45f9 100644 --- a/VDSSample/ViewControllers/CheckboxItemViewController.swift +++ b/VDSSample/ViewControllers/CheckboxItemViewController.swift @@ -27,10 +27,9 @@ class CheckboxItemViewController: BaseViewController { setupModel() } - var actionLabel = Label() override func setupForm(){ super.setupForm() - addFormRow(label: "Action", view: actionLabel) + addActionRow() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Label Text", view: labelTextField) @@ -68,7 +67,7 @@ class CheckboxItemViewController: BaseViewController { self?.component.errorText = text }.store(in: &subscribers) - component.labelPublisher("Checkbox", label: actionLabel) + component.onChangeActionPublisher("Checkbox", label: actionLabel) } func setupModel() { diff --git a/VDSSample/ViewControllers/CheckboxViewController.swift b/VDSSample/ViewControllers/CheckboxViewController.swift index 21b90ec..f61bcf1 100644 --- a/VDSSample/ViewControllers/CheckboxViewController.swift +++ b/VDSSample/ViewControllers/CheckboxViewController.swift @@ -23,10 +23,9 @@ class CheckboxViewController: BaseViewController { setupModel() } - var actionLabel = Label() override func setupForm(){ super.setupForm() - addFormRow(label: "Action", view: actionLabel) + addActionRow() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Error", view: showErrorSwitch) @@ -43,7 +42,7 @@ class CheckboxViewController: BaseViewController { self?.component.disabled = sender.isOn } - component.labelPublisher("Checkbox", label: actionLabel) + component.onChangeActionPublisher("Checkbox", label: actionLabel) } func setupModel() { diff --git a/VDSSample/ViewControllers/RadioBoxGroupViewController.swift b/VDSSample/ViewControllers/RadioBoxGroupViewController.swift index d72d09b..0307fbc 100644 --- a/VDSSample/ViewControllers/RadioBoxGroupViewController.swift +++ b/VDSSample/ViewControllers/RadioBoxGroupViewController.swift @@ -28,10 +28,9 @@ class RadioBoxGroupViewController: BaseViewController{ setupModel() } - var actionLabel = Label() override func setupForm(){ super.setupForm() - addFormRow(label: "Action", view: actionLabel) + addActionRow() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Strikethrough", view: strikeThroughSwitch) diff --git a/VDSSample/ViewControllers/RadioButtonGroupViewController.swift b/VDSSample/ViewControllers/RadioButtonGroupViewController.swift index 3eeaa2f..52c7c17 100644 --- a/VDSSample/ViewControllers/RadioButtonGroupViewController.swift +++ b/VDSSample/ViewControllers/RadioButtonGroupViewController.swift @@ -26,10 +26,9 @@ class RadioButtonGroupViewController: BaseViewController { setupModel() } - var actionLabel = Label() override func setupForm(){ super.setupForm() - addFormRow(label: "Action", view: actionLabel) + addActionRow() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Label Text", view: labelTextField) diff --git a/VDSSample/ViewControllers/RadioButtonItemViewController.swift b/VDSSample/ViewControllers/RadioButtonItemViewController.swift index 895c69d..35c603d 100644 --- a/VDSSample/ViewControllers/RadioButtonItemViewController.swift +++ b/VDSSample/ViewControllers/RadioButtonItemViewController.swift @@ -27,10 +27,9 @@ class RadioButtonItemViewController: BaseViewController { setupModel() } - var actionLabel = Label() override func setupForm(){ super.setupForm() - addFormRow(label: "Action", view: actionLabel) + addActionRow() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Label Text", view: labelTextField) diff --git a/VDSSample/ViewControllers/RadioButtonViewController.swift b/VDSSample/ViewControllers/RadioButtonViewController.swift index 071a447..9b74a3c 100644 --- a/VDSSample/ViewControllers/RadioButtonViewController.swift +++ b/VDSSample/ViewControllers/RadioButtonViewController.swift @@ -22,10 +22,9 @@ class RadioButtonViewController: BaseViewController { setupPicker() setupModel() } - var actionLabel = Label() override func setupForm(){ super.setupForm() - addFormRow(label: "Action", view: actionLabel) + addActionRow() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Error", view: showErrorSwitch) diff --git a/VDSSample/ViewControllers/ToggleViewController.swift b/VDSSample/ViewControllers/ToggleViewController.swift index 5737143..7831770 100644 --- a/VDSSample/ViewControllers/ToggleViewController.swift +++ b/VDSSample/ViewControllers/ToggleViewController.swift @@ -43,7 +43,7 @@ class ToggleViewController: BaseViewController { override func setupForm() { super.setupForm() - + addActionRow() addFormRow(label: "Show Text", view: showTextSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Disabled", view: disabledSwitch) @@ -59,12 +59,7 @@ class ToggleViewController: BaseViewController { append(section: textFormStackView) component.onChange = { [weak self] toggle in - let alertController:UIAlertController = UIAlertController(title: "Alert", - message: "Toggle Value: \(toggle.isOn)", - preferredStyle: UIAlertController.Style.alert) - alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil)) - self?.present(alertController, animated: true) - print("toggle changed: \(toggle.isOn)") + self?.actionLabel.text = "Toggle Value: \(toggle.isOn)" } showTextSwitch.onChange = { [weak self] sender in