refactored naming conventions and merged percentage and width into an enum

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-28 15:14:35 -05:00
parent 5756b299e0
commit d84ccbb5c2

View File

@ -18,57 +18,56 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
//--------------------------------------------------
// MARK: - Enums
//--------------------------------------------------
public enum ButtonPosition: String, CaseIterable {
public enum Alignment: String, CaseIterable {
case left, center, right
}
public enum ChildWidth {
case percentage(CGFloat)
case value(CGFloat)
}
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
//An object containing number of Button components per row, in each viewport
//An object containing number of Button components per row for iPhones
open var rowQuantityPhone: Int = 0 { didSet { setNeedsUpdate() } }
//An object containing number of Button components per row for iPads
open var rowQuantityTablet: Int = 0 { didSet { setNeedsUpdate() } }
// An object containing number of Button components per row
open var rowQuantity: Int { UIDevice.isIPad ? rowQuantityTablet : rowQuantityPhone }
//If provided, aligns TextLink/TextLinkCaret alignment when rowQuantity is set one.
open var buttonPosition: ButtonPosition = .center { didSet { setNeedsUpdate() }}
open var alignment: Alignment = .center { didSet { setNeedsUpdate() }}
/// Array of Buttonable Views that are shown in the group.
open var buttons: [Buttonable] = [] { didSet { setNeedsUpdate() }}
//If provided, width of Button components will be rendered based on this value. If omitted, default button widths are rendered.
open var buttonWidth: CGFloat? {
didSet {
if let buttonWidth, let buttonPercentage, buttonWidth > 0, buttonPercentage > 0{
self.buttonPercentage = nil
}
buttons.forEach { button in
if let button = button as? Button {
button.width = buttonWidth
/// If provided, width of Button components will be rendered based on this value. If omitted, default button widths are rendered.
private var _childWidth: ChildWidth?
open var childWidth: ChildWidth? {
get { _childWidth }
set {
if let newValue {
switch newValue {
case .percentage(let percentage):
if percentage <= 100.0, rowQuantity > 0 {
_childWidth = newValue
}
case .value(let value):
if value > 0 {
_childWidth = newValue
}
}
} else {
_childWidth = nil
}
setNeedsUpdate()
}
}
var _buttonPercentage: CGFloat?
open var buttonPercentage: CGFloat? {
get { _buttonPercentage }
set {
if let newValue, newValue <= 100.0, rowQuantity > 0 {
_buttonPercentage = newValue
} else {
_buttonPercentage = nil
}
if let buttonWidth, let buttonPercentage, buttonWidth > 0, buttonPercentage > 0 {
self.buttonWidth = nil
}
positionLayout.buttonPercentage = buttonPercentage
setNeedsUpdate()
}
}
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
@ -141,8 +140,20 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
positionLayout.position = buttonPosition
positionLayout.position = alignment
positionLayout.rowQuantity = rowQuantity
var percentage: CGFloat?
var width: CGFloat?
if let childWidth {
switch childWidth {
case .value(let value): width = value
case .percentage(let value): percentage = value
}
}
buttons.forEach { ($0 as? Button)?.width = width }
positionLayout.buttonPercentage = percentage
collectionView.reloadData()
}