Created
July 27, 2021 14:58
-
-
Save victor-gesit/419bc1ad26510de672689d7b1506ac47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Protocols | |
protocol Toggleable { | |
var selected: Bool { get } | |
} | |
protocol Toggler: AnyObject { | |
var selectedProperty: ToggledProperty? { get set} | |
func toggleTo(toggleProperty: ToggledProperty?) | |
} | |
protocol ToggledProperty { | |
var id: Int { get } | |
} | |
protocol ViewToToggleDelegate: AnyObject { | |
func didSelectOption(option: Option?) | |
} | |
// Usage | |
// Options class | |
enum Option: Int, ToggledProperty { | |
case optionA | |
case optionB | |
case optionC | |
var id: Int { | |
get { | |
return self.rawValue | |
} | |
} | |
} | |
// View to toggle | |
class ViewToToggle: UIView, Toggleable { | |
weak var toggler: Toggler? | |
weak var delegate: ViewToToggleDelegate? | |
var option: Option = .optionA { | |
didSet { | |
self.updateUI() | |
} | |
} | |
var selected = false { | |
didSet { | |
updateUI() | |
} | |
} | |
func commonInit() { | |
// Bundle.main.loadNibNamed(Self.reuseIdentifier, owner: self, options: nil) | |
// contentView.fitInView(self) | |
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(optionSelected))) | |
} | |
@objc | |
private func optionSelected() { | |
self.selected = !selected | |
toggler?.toggleTo(toggleProperty: selected ? option : nil) | |
delegate?.didSelectOption(option: selected ? option : nil) | |
} | |
private func updateUI() { | |
// Update UI based on selected state and option | |
} | |
} | |
// Toggler Class | |
class ViewToggler: Toggler { | |
var viewsToToggle: [Toggleable & ViewToToggle] = [] | |
var selectedProperty: ToggledProperty? | |
func toggleTo(toggleProperty: ToggledProperty?) { | |
if let toggleProperty = toggleProperty { | |
for view in viewsToToggle { | |
if view.option.id == toggleProperty.id { | |
view.selected = true | |
} else { | |
view.selected = false | |
} | |
} | |
} else { | |
for view in viewsToToggle { | |
view.selected = false | |
} | |
} | |
self.selectedProperty = toggleProperty | |
} | |
func configureWith(views: [Toggleable & ViewToToggle]) { | |
self.viewsToToggle = views | |
} | |
} | |
// View Controller | |
class ContainingVC: UIViewController { | |
@IBOutlet weak var optionACardView: ViewToToggle! | |
@IBOutlet weak var optionBCardView: ViewToToggle! | |
@IBOutlet weak var optionCCardView: ViewToToggle! | |
var optionsToggler: OptionToggler = ViewToggler() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupOptionButtons() | |
} | |
func setupOptionButtons() { | |
optionACardView.configureCardView(with: .optionA, delegate: self, toggler: optionsToggler) | |
optionBCardView.configureCardView(with: .optionB, delegate: self, toggler: optionsToggler) | |
optionCCardView.configureCardView(with: .optionC, delegate: self, toggler: optionsToggler) | |
optionsToggler.configureWith(views: [optionACardView, | |
optionBCardView, | |
optionCCardView]) | |
} | |
} | |
extension ContainingVC: ViewToToggleDelegate { | |
func didSelectOption(option: Option?) { | |
print("Option selected: ", option) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment