From 5cf6e6be1215293590a0481cafd1e6b491f5910b Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 6 Dec 2022 15:58:15 -0600 Subject: [PATCH] refactored buttons Signed-off-by: Matt Bruce --- VDSSample/Classes/Helper.swift | 54 +++++++++---------- .../ButtonGroupViewController.swift | 17 +++--- .../ButtonViewController.swift | 22 ++------ .../TextLinkCaretViewController.swift | 4 +- .../TextLinkViewController.swift | 4 +- 5 files changed, 44 insertions(+), 57 deletions(-) diff --git a/VDSSample/Classes/Helper.swift b/VDSSample/Classes/Helper.swift index 05ba8b0..8a140f9 100644 --- a/VDSSample/Classes/Helper.swift +++ b/VDSSample/Classes/Helper.swift @@ -24,49 +24,43 @@ extension UIView { } } +extension ButtonBase { + func labelPublisher(_ label: UILabel){ + publisher(for: .touchUpInside) + .sink { 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") + }.store(in: &subscribers) + } +} + extension BaseViewController { - func makeButton(_ text: String) -> Button { + func makeButton(_ text: String, label: UILabel) -> Button { return Button().with{ $0.text = text - $0.publisher(for: .touchUpInside) - .sink { [weak self] control in - let alertController:UIAlertController = UIAlertController(title: "Alert", - message: "\(control.text!) Clicked", - preferredStyle: UIAlertController.Style.alert) - alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil)) - self?.present(alertController, animated: true) - print("clicked me") - }.store(in: &subscribers) + $0.labelPublisher(label) } } - func makeTextLink(_ text: String) -> TextLink { + func makeTextLink(_ text: String, label: UILabel) -> TextLink { return TextLink().with{ $0.text = text - $0.publisher(for: .touchUpInside) - .sink { [weak self] control in - let alertController:UIAlertController = UIAlertController(title: "Alert", - message: "\(control.text!) Clicked", - preferredStyle: UIAlertController.Style.alert) - alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil)) - self?.present(alertController, animated: true) - print("clicked me") - }.store(in: &subscribers) + $0.labelPublisher(label) } } - func makeTextLinkCaret(_ text: String) -> TextLinkCaret { + func makeTextLinkCaret(_ text: String, label: UILabel) -> TextLinkCaret { return TextLinkCaret().with{ $0.text = text - $0.publisher(for: .touchUpInside) - .sink { [weak self] control in - let alertController:UIAlertController = UIAlertController(title: "Alert", - message: "\(control.text!) Clicked", - preferredStyle: UIAlertController.Style.alert) - alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil)) - self?.present(alertController, animated: true) - print("clicked me") - }.store(in: &subscribers) + $0.labelPublisher(label) } } } diff --git a/VDSSample/ViewControllers/ButtonGroupViewController.swift b/VDSSample/ViewControllers/ButtonGroupViewController.swift index 2b2aa45..aa0f780 100644 --- a/VDSSample/ViewControllers/ButtonGroupViewController.swift +++ b/VDSSample/ViewControllers/ButtonGroupViewController.swift @@ -59,7 +59,7 @@ class ButtonGroupViewController: BaseViewController { items: RowQuantity.allCases) }() - + var label = Label() var disabledSwitch = Toggle() var widthTextField = TextField() @@ -68,13 +68,13 @@ class ButtonGroupViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() buttonGroup.buttons = [ - makeButton("Secondary").with{ $0.use = .secondary }, - makeButton("Primary"), - makeTextLink("Large Text Link"), - makeButton("Widge Label Button"), - makeTextLinkCaret("Text Link Caret"), - makeTextLink("Small Text Link").with{ $0.size = .small }, - makeButton("Small Button").with{$0.size = .small; $0.use = .secondary } + makeButton("Secondary", label: label).with{ $0.use = .secondary }, + makeButton("Primary", label: label), + makeTextLink("Large Text Link", label: label), + makeButton("Widge Label Button", label: label), + makeTextLinkCaret("Text Link Caret", label: label), + makeTextLink("Small Text Link", label: label).with{ $0.size = .small }, + makeButton("Small Button", label: label).with{$0.size = .small; $0.use = .secondary } ] addContentTopView(view: buttonGroup) @@ -87,6 +87,7 @@ class ButtonGroupViewController: BaseViewController { override func allTextFields() -> [UITextField]? { [widthTextField] } func setupForm(){ + addFormRow(label: "Button Action", view: label) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch)) addFormRow(label: "Position", view: buttonPositionSelectorView) diff --git a/VDSSample/ViewControllers/ButtonViewController.swift b/VDSSample/ViewControllers/ButtonViewController.swift index b1a92a1..8bb498b 100644 --- a/VDSSample/ViewControllers/ButtonViewController.swift +++ b/VDSSample/ViewControllers/ButtonViewController.swift @@ -24,15 +24,16 @@ class ButtonViewController: BaseViewController { items: ButtonSize.allCases) }() + var label = Label() var disabledSwitch = Toggle() var textField = TextField() var widthTextField = TextField() - let button = Button() + var button: Button! override func viewDidLoad() { super.viewDidLoad() - + button = makeButton("Button", label: label) addContentTopView(view: .makeWrapper(for: button)) setupForm() @@ -43,6 +44,7 @@ class ButtonViewController: BaseViewController { override func allTextFields() -> [UITextField]? { [textField, widthTextField] } func setupForm(){ + addFormRow(label: "Button Action", view: label) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Use", view: usePickerSelectorView) addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch)) @@ -73,21 +75,7 @@ class ButtonViewController: BaseViewController { } - func setupModel() { - button.text = "Button" - - button - .publisher(for: .touchUpInside) - .sink { [weak self] control in - let alertController:UIAlertController = UIAlertController(title: "Alert", - message: "\(control.text!) Clicked", - preferredStyle: UIAlertController.Style.alert) - alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:nil)) - self?.present(alertController, animated: true) - print("clicked me") - }.store(in: &subscribers) - - + func setupModel() { //setup UI surfacePickerSelectorView.text = button.surface.rawValue disabledSwitch.isOn = button.disabled diff --git a/VDSSample/ViewControllers/TextLinkCaretViewController.swift b/VDSSample/ViewControllers/TextLinkCaretViewController.swift index 2067d72..1ba4314 100644 --- a/VDSSample/ViewControllers/TextLinkCaretViewController.swift +++ b/VDSSample/ViewControllers/TextLinkCaretViewController.swift @@ -18,10 +18,11 @@ class TextLinkCaretViewController: BaseViewController { items: TextLinkCaretPosition.allCases) }() + var label = Label() var disabledSwitch = Toggle() var textField = TextField() lazy var textLinkCaret: TextLinkCaret = { - makeTextLinkCaret("Text Link Caret") + makeTextLinkCaret("Text Link Caret", label: label) }() override func viewDidLoad() { @@ -36,6 +37,7 @@ class TextLinkCaretViewController: BaseViewController { override func allTextFields() -> [UITextField]? { [textField] } func setupForm(){ + addFormRow(label: "Button Action", view: label) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch)) addFormRow(label: "Label", view: textField) diff --git a/VDSSample/ViewControllers/TextLinkViewController.swift b/VDSSample/ViewControllers/TextLinkViewController.swift index 855f028..9b14103 100644 --- a/VDSSample/ViewControllers/TextLinkViewController.swift +++ b/VDSSample/ViewControllers/TextLinkViewController.swift @@ -18,10 +18,11 @@ class TextLinkViewController: BaseViewController { items: ButtonSize.allCases) }() + var label = Label() var disabledSwitch = Toggle() var textField = TextField() lazy var textLink: TextLink = { - makeTextLink("Text Link") + makeTextLink("Text Link", label: label) }() override func viewDidLoad() { @@ -37,6 +38,7 @@ class TextLinkViewController: BaseViewController { override func allTextFields() -> [UITextField]? { [textField] } func setupForm(){ + addFormRow(label: "Button Action", view: label) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Disabled", view: .makeWrapper(for: disabledSwitch)) addFormRow(label: "Label", view: textField)