// // RadioButtonContainer.swift // PrivadoVPN // // Created by Murad Shabanov on 29.07.2021. // Copyright © 2021 Privado LLC. All rights reserved. // import Foundation /// Hold all Radio Buttons class RadioButtonContainer: RadioBaseContainer { /// Radio button delegate will be assigned to all button added in container public weak var delegate: RadioButtonDelegate? { didSet { self.allButtons.forEach { $0.delegate = delegate } } } /// Select any radio button available in container. It will automatically deselect all other buttons public var selectedButton: Kind? { get { return selectedButtons.first } set { guard let button = newValue else { self.deselectAll() return } self.selectedButtons = [button] } } /// Making sure new button must have delegate assigned @discardableResult public override func addButton(_ button: Kind) -> Bool { button.delegate = self.delegate return super.addButton(button) } /// Deselecting previous selected button override func selectionChangeObserver(_ button: RadioButton, _ change: NSKeyValueObservedChange) { super.selectionChangeObserver(button, change) if button.isOn { // Deselect on selected button excepting current selected button self.allButtons.forEach { if $0.isOn, button != $0 { $0.isOn = false } } } } }