refactored toggle
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
4390c29c08
commit
9ac7cef6c3
@ -50,7 +50,6 @@
|
||||
EA89204C28B66CE2006B9984 /* ScrollWrapperView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA89204528B66CE2006B9984 /* ScrollWrapperView.swift */; };
|
||||
EA89204E28B67332006B9984 /* CheckBoxGroupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA89204D28B67332006B9984 /* CheckBoxGroupViewController.swift */; };
|
||||
EA89205128B68307006B9984 /* TextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA89205028B68307006B9984 /* TextField.swift */; };
|
||||
EAB1D2C528A6B11D00DAE764 /* TestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB1D2C428A6B11D00DAE764 /* TestViewController.swift */; };
|
||||
EAB1D2C928AAAA1D00DAE764 /* ModelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB1D2C828AAAA1D00DAE764 /* ModelViewController.swift */; };
|
||||
EAB1D2CB28AAB9E200DAE764 /* TemplateViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB1D2CA28AAB9E200DAE764 /* TemplateViewController.swift */; };
|
||||
EAB1D2D428AC409F00DAE764 /* LabelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB1D2D328AC409F00DAE764 /* LabelViewController.swift */; };
|
||||
@ -113,7 +112,6 @@
|
||||
EA89204528B66CE2006B9984 /* ScrollWrapperView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScrollWrapperView.swift; sourceTree = "<group>"; };
|
||||
EA89204D28B67332006B9984 /* CheckBoxGroupViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckBoxGroupViewController.swift; sourceTree = "<group>"; };
|
||||
EA89205028B68307006B9984 /* TextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextField.swift; sourceTree = "<group>"; };
|
||||
EAB1D2C428A6B11D00DAE764 /* TestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestViewController.swift; sourceTree = "<group>"; };
|
||||
EAB1D2C828AAAA1D00DAE764 /* ModelViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelViewController.swift; sourceTree = "<group>"; };
|
||||
EAB1D2CA28AAB9E200DAE764 /* TemplateViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TemplateViewController.swift; sourceTree = "<group>"; };
|
||||
EAB1D2D328AC409F00DAE764 /* LabelViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelViewController.swift; sourceTree = "<group>"; };
|
||||
@ -255,7 +253,6 @@
|
||||
EA89201828B56DF5006B9984 /* RadioBoxGroupViewController.swift */,
|
||||
EAF7F11928A14A0E00B287F5 /* RadioButtonViewController.swift */,
|
||||
EAB1D2CA28AAB9E200DAE764 /* TemplateViewController.swift */,
|
||||
EAB1D2C428A6B11D00DAE764 /* TestViewController.swift */,
|
||||
EA3C3BB328996775000CA526 /* ToggleViewController.swift */,
|
||||
);
|
||||
path = ViewControllers;
|
||||
@ -414,7 +411,6 @@
|
||||
EA3C3B9F289966EF000CA526 /* SceneDelegate.swift in Sources */,
|
||||
EA89204A28B66CE2006B9984 /* KeyboardFrameChange.swift in Sources */,
|
||||
EA3C3BB428996775000CA526 /* PickerBase.swift in Sources */,
|
||||
EAB1D2C528A6B11D00DAE764 /* TestViewController.swift in Sources */,
|
||||
EAB1D2C928AAAA1D00DAE764 /* ModelViewController.swift in Sources */,
|
||||
EA89204728B66CE2006B9984 /* KeyboardFrameChangeListener.swift in Sources */,
|
||||
EA89204828B66CE2006B9984 /* ScrollViewKeyboardAvoiding.swift in Sources */,
|
||||
|
||||
@ -19,14 +19,12 @@ protocol Initable {
|
||||
|
||||
class MenuViewController: UITableViewController {
|
||||
let items: [MenuComponent] = [
|
||||
MenuComponent(title: "Label", viewController: LabelViewController.self),
|
||||
MenuComponent(title: "Toggle", viewController: ToggleViewController.self),
|
||||
MenuComponent(title: "Checkbox", viewController: CheckboxViewController.self),
|
||||
MenuComponent(title: "CheckboxGroup", viewController: CheckboxGroupViewController.self),
|
||||
MenuComponent(title: "Label", viewController: LabelViewController.self),
|
||||
MenuComponent(title: "RadioButton", viewController: RadioButtonViewController.self),
|
||||
MenuComponent(title: "RadioBoxGroup", viewController: RadioBoxGroupViewController.self),
|
||||
MenuComponent(title: "Test Form", viewController: TestViewController.self),
|
||||
MenuComponent(title: "Template", viewController: TemplateViewController.self)
|
||||
MenuComponent(title: "Toggle", viewController: ToggleViewController.self)
|
||||
]
|
||||
|
||||
override func numberOfSections(in tableView: UITableView) -> Int {
|
||||
|
||||
@ -217,7 +217,7 @@ public class ModelScrollViewController<ModelType: Modelable>: UIViewController,
|
||||
view.bottomAnchor.constraint(equalTo: contentTopView.bottomAnchor, constant: -20).isActive = true
|
||||
}
|
||||
|
||||
open func addFormRow(label: String, view: UIView) {
|
||||
open func addFormRow(label: String, view: UIView, stackView: UIStackView? = nil) {
|
||||
let formRow = UIStackView().with {
|
||||
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||
$0.alignment = .fill
|
||||
@ -233,7 +233,11 @@ public class ModelScrollViewController<ModelType: Modelable>: UIViewController,
|
||||
formRow.addArrangedSubview(label)
|
||||
formRow.addArrangedSubview(view)
|
||||
|
||||
formStackView.addArrangedSubview(formRow)
|
||||
if let stackView {
|
||||
stackView.addArrangedSubview(formRow)
|
||||
} else {
|
||||
formStackView.addArrangedSubview(formRow)
|
||||
}
|
||||
}
|
||||
|
||||
open func setup() {}
|
||||
|
||||
@ -1,130 +0,0 @@
|
||||
//
|
||||
// TestViewController.swift
|
||||
// VDSSample
|
||||
//
|
||||
// Created by Matt Bruce on 8/12/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import Combine
|
||||
import VDS
|
||||
|
||||
struct User {
|
||||
var firstName: String
|
||||
var lastName: String
|
||||
}
|
||||
|
||||
class UserNameView: UIControl {
|
||||
|
||||
private let firstNameTextField = TextField()
|
||||
private let lastNameTextField = TextField()
|
||||
private var subscriptions = Set<AnyCancellable>()
|
||||
|
||||
var user: User {
|
||||
User(firstName: firstNameTextField.text ?? "", lastName: lastNameTextField.text ?? "" )
|
||||
}
|
||||
|
||||
init(user: User) {
|
||||
super.init(frame: .zero)
|
||||
firstNameTextField.text = user.firstName
|
||||
lastNameTextField.text = user.lastName
|
||||
|
||||
translatesAutoresizingMaskIntoConstraints = false
|
||||
firstNameTextField.translatesAutoresizingMaskIntoConstraints = false
|
||||
lastNameTextField.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
addSubview(firstNameTextField)
|
||||
addSubview(lastNameTextField)
|
||||
|
||||
firstNameTextField.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
||||
firstNameTextField.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
||||
firstNameTextField.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
||||
firstNameTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
|
||||
|
||||
lastNameTextField.topAnchor.constraint(equalTo: firstNameTextField.bottomAnchor, constant: 20).isActive = true
|
||||
lastNameTextField.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
||||
lastNameTextField.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
||||
lastNameTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
|
||||
lastNameTextField.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
||||
|
||||
firstNameTextField
|
||||
.textPublisher
|
||||
.sink { [weak self] _ in
|
||||
self?.sendActions(for: .valueChanged)
|
||||
}.store(in: &subscriptions)
|
||||
|
||||
lastNameTextField
|
||||
.textPublisher
|
||||
.sink { [weak self] _ in
|
||||
self?.sendActions(for: .valueChanged)
|
||||
}.store(in: &subscriptions)
|
||||
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
}
|
||||
|
||||
@objcMembers class TestViewController: UIViewController, Initable {
|
||||
deinit {
|
||||
print("\(Self.self) deinit")
|
||||
}
|
||||
|
||||
var user = User(firstName: "Joe", lastName: "User") {
|
||||
didSet {
|
||||
print("User oldValue: \(oldValue)")
|
||||
print("User newValue: \(user)")
|
||||
}
|
||||
}
|
||||
|
||||
private var subscriptions = Set<AnyCancellable>()
|
||||
|
||||
required init() {
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
view.backgroundColor = .white
|
||||
|
||||
let nameTextField = UserNameView(user: user)
|
||||
let button = UIButton()
|
||||
|
||||
button.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
view.addSubview(nameTextField)
|
||||
view.addSubview(button)
|
||||
nameTextField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
|
||||
nameTextField.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
|
||||
nameTextField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
|
||||
button.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
|
||||
button.topAnchor.constraint(equalTo: nameTextField.bottomAnchor, constant: 20).isActive = true
|
||||
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
|
||||
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
|
||||
button.backgroundColor = .blue
|
||||
button.setTitle("Print", for: .normal)
|
||||
|
||||
//publishers
|
||||
nameTextField
|
||||
.publisher(for: .valueChanged)
|
||||
.sink(receiveValue: { [weak self] control in
|
||||
guard let self else { return }
|
||||
self.user = control.user
|
||||
})
|
||||
.store(in: &subscriptions)
|
||||
|
||||
button
|
||||
.tapPublisher
|
||||
.sink { [weak self] _ in
|
||||
if let user = self?.user {
|
||||
print("\(user.firstName) \(user.lastName)")
|
||||
}
|
||||
}.store(in: &subscriptions)
|
||||
}
|
||||
}
|
||||
@ -10,41 +10,111 @@ import UIKit
|
||||
import VDS
|
||||
import VDSColorTokens
|
||||
|
||||
class ToggleViewController: ModelViewController<DefaultToggleModel>, StoryboardInitable {
|
||||
deinit {
|
||||
print("\(Self.self) deinit")
|
||||
}
|
||||
|
||||
class ToggleViewController: ModelScrollViewController<DefaultToggleModel> {
|
||||
|
||||
enum PickerType {
|
||||
case surface, textSize, textPosition
|
||||
}
|
||||
static var storyboardId: String = "toggle"
|
||||
static var storyboardName: String = "Components"
|
||||
|
||||
@IBOutlet weak var toggleContainerView: UIView!
|
||||
@IBOutlet weak var picker: UIPickerView!
|
||||
@IBOutlet weak var surfaceLabel: UILabel!
|
||||
@IBOutlet weak var textSizeLabel: UILabel!
|
||||
@IBOutlet weak var textPositionLabel: UILabel!
|
||||
@IBOutlet weak var showTextStack: UIStackView!
|
||||
|
||||
var surfacePickerSelectorView = PickerSelectorView(title: "light")
|
||||
var disabledSwitch = UISwitch()
|
||||
var showTextSwitch = UISwitch()
|
||||
|
||||
var textFormStackView: UIStackView = {
|
||||
return UIStackView().with {
|
||||
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||
$0.alignment = .fill
|
||||
$0.distribution = .fillProportionally
|
||||
$0.axis = .vertical
|
||||
$0.spacing = 10
|
||||
}
|
||||
}()
|
||||
|
||||
var boldSwitch = UISwitch()
|
||||
var textSizePickerSelectorView = PickerSelectorView(title: "Large")
|
||||
var textPositionPickerSelectorView = PickerSelectorView(title: "left")
|
||||
var onTextField = TextField()
|
||||
var offTextField = TextField()
|
||||
|
||||
var toggle = Toggle()
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
addContentTopView(view: toggle)
|
||||
|
||||
setupForm()
|
||||
setupPicker()
|
||||
}
|
||||
|
||||
func setupForm() {
|
||||
|
||||
toggleContainerView.addSubview(toggle)
|
||||
toggle.leadingAnchor.constraint(equalTo: toggleContainerView.leadingAnchor, constant: 20).isActive = true
|
||||
toggle.bottomAnchor.constraint(equalTo: toggleContainerView.bottomAnchor, constant: -20).isActive = true
|
||||
toggle.topAnchor.constraint(equalTo: toggleContainerView.topAnchor, constant: 20).isActive = true
|
||||
addFormRow(label: "Show Text", view: showTextSwitch)
|
||||
addFormRow(label: "Surface", view: surfacePickerSelectorView)
|
||||
addFormRow(label: "Disabled", view: disabledSwitch)
|
||||
|
||||
//add the hidden elements to textFormStackView
|
||||
addFormRow(label: "Bold", view: boldSwitch, stackView: textFormStackView)
|
||||
addFormRow(label: "Text Size", view: textSizePickerSelectorView, stackView: textFormStackView)
|
||||
addFormRow(label: "Text Position", view: textPositionPickerSelectorView, stackView: textFormStackView)
|
||||
addFormRow(label: "On Text", view: onTextField, stackView: textFormStackView)
|
||||
addFormRow(label: "Off Text", view: offTextField, stackView: textFormStackView)
|
||||
//add textFormStackView to main form
|
||||
formStackView.addArrangedSubview(textFormStackView)
|
||||
|
||||
toggle.publisher(for: .valueChanged).sink { toggle in
|
||||
print("toggle changed: \(toggle.isOn)")
|
||||
}.store(in: &subscribers)
|
||||
|
||||
showTextSwitch
|
||||
.publisher(for: .valueChanged)
|
||||
.sink { [weak self] sender in
|
||||
self?.toggle.showText = sender.isOn
|
||||
self?.textFormStackView.isHidden = !sender.isOn
|
||||
}.store(in: &subscribers)
|
||||
|
||||
surfacePickerSelectorView.button
|
||||
.publisher(for: .touchUpInside)
|
||||
.sink { [weak self] _ in
|
||||
self?.pickerType = .surface
|
||||
}.store(in: &subscribers)
|
||||
|
||||
textSizePickerSelectorView.button
|
||||
.publisher(for: .touchUpInside)
|
||||
.sink { [weak self] _ in
|
||||
self?.pickerType = .textSize
|
||||
}.store(in: &subscribers)
|
||||
|
||||
textPositionPickerSelectorView.button
|
||||
.publisher(for: .touchUpInside)
|
||||
.sink { [weak self] _ in
|
||||
self?.pickerType = .textPosition
|
||||
}.store(in: &subscribers)
|
||||
|
||||
disabledSwitch
|
||||
.publisher(for: .valueChanged)
|
||||
.sink { [weak self] sender in
|
||||
self?.toggle.disabled = sender.isOn
|
||||
}.store(in: &subscribers)
|
||||
|
||||
showTextStack.isHidden = true
|
||||
view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
|
||||
setupPicker()
|
||||
boldSwitch
|
||||
.publisher(for: .valueChanged)
|
||||
.sink { [weak self] sender in
|
||||
self?.toggle.isBold = sender.isOn
|
||||
}.store(in: &subscribers)
|
||||
|
||||
onTextField
|
||||
.textPublisher
|
||||
.sink { [weak self] text in
|
||||
self?.toggle.onText = text
|
||||
}.store(in: &subscribers)
|
||||
|
||||
offTextField
|
||||
.textPublisher
|
||||
.sink { [weak self] text in
|
||||
self?.toggle.offText = text
|
||||
}.store(in: &subscribers)
|
||||
|
||||
textFormStackView.isHidden = true
|
||||
}
|
||||
|
||||
override func updateView(viewModel: DefaultToggleModel) {
|
||||
@ -52,41 +122,6 @@ class ToggleViewController: ModelViewController<DefaultToggleModel>, StoryboardI
|
||||
toggle.set(with: viewModel)
|
||||
}
|
||||
|
||||
@IBAction func showTextChanged(_ sender: UISwitch) {
|
||||
toggle.showText = sender.isOn
|
||||
showTextStack.isHidden = !sender.isOn
|
||||
}
|
||||
|
||||
@IBAction func disabledChanged(_ sender: UISwitch) {
|
||||
toggle.disabled = sender.isOn
|
||||
}
|
||||
|
||||
@IBAction func boldChanged(_ sender: UISwitch) {
|
||||
toggle.isBold = sender.isOn
|
||||
}
|
||||
|
||||
@IBAction func offTextDidEnd(_ sender: UITextField) {
|
||||
toggle.offText = sender.text ?? "Off"
|
||||
sender.resignFirstResponder()
|
||||
}
|
||||
|
||||
@IBAction func onTextDidEnd(_ sender: UITextField) {
|
||||
toggle.onText = sender.text ?? "On"
|
||||
sender.resignFirstResponder()
|
||||
}
|
||||
|
||||
@IBAction func surfaceClick(_ sender: Any) {
|
||||
pickerType = .surface
|
||||
}
|
||||
|
||||
@IBAction func textSizeClick(_ sender: Any) {
|
||||
pickerType = .textSize
|
||||
}
|
||||
|
||||
@IBAction func textPositionClick(_ sender: Any) {
|
||||
pickerType = .textPosition
|
||||
}
|
||||
|
||||
//Picker
|
||||
var surfacePicker = SurfacePicker()
|
||||
var textSizePicker = TextSizePicker()
|
||||
@ -114,21 +149,21 @@ class ToggleViewController: ModelViewController<DefaultToggleModel>, StoryboardI
|
||||
}
|
||||
|
||||
func setupPicker(){
|
||||
picker.isHidden = true
|
||||
|
||||
surfacePicker.onPickerDidSelect = { [weak self] item in
|
||||
self?.toggle.surface = item
|
||||
self?.toggleContainerView.backgroundColor = item.color
|
||||
self?.surfaceLabel.text = item.rawValue
|
||||
self?.contentTopView.backgroundColor = item.color
|
||||
self?.surfacePickerSelectorView.text = item.rawValue
|
||||
}
|
||||
|
||||
textSizePicker.onPickerDidSelect = { [weak self] item in
|
||||
self?.toggle.fontSize = item
|
||||
self?.textSizeLabel.text = item.rawValue
|
||||
self?.textSizePickerSelectorView.text = item.rawValue
|
||||
}
|
||||
|
||||
textPositionPicker.onPickerDidSelect = { [weak self] item in
|
||||
self?.toggle.textPosition = item
|
||||
self?.textPositionLabel.text = item.rawValue
|
||||
self?.textPositionPickerSelectorView.text = item.rawValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user