Digital ACT-191 ONEAPP-9311 story: refactored code and resolved constraint issues
This commit is contained in:
parent
06b0c80a58
commit
46b11c2585
@ -37,21 +37,33 @@ open class InputStepper: EntryFieldBase {
|
|||||||
case large, small
|
case large, small
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enum used to describe the width of a fixed value or percentage of the input stepper control.
|
||||||
|
public enum ControlWidth {
|
||||||
|
case percentage(CGFloat)
|
||||||
|
case value(CGFloat)
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Public Properties
|
// MARK: - Public Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
/// Accepts a string or number value to control the width of input stepper.
|
/// If there is a width that is larger than this size's minimumWidth, the input stepper will resize to this width.
|
||||||
/// auto(default) - The control's width is determined by the combined width of the value, trailing text and padding
|
open var controlWidth: ControlWidth? {
|
||||||
/// Value - The control's width can be set to a fixed pixel.
|
get { _controlWidth }
|
||||||
open var controlWidth: String? = "auto" { didSet { setNeedsUpdate() } }
|
set {
|
||||||
|
if let newValue {
|
||||||
/// Accepts percentage value to controlWidth of input stepper.
|
switch newValue {
|
||||||
open var controlWidthPercentage: CGFloat? {
|
case .percentage(let percentage):
|
||||||
didSet {
|
if percentage <= 100.0 {
|
||||||
if let percentage = controlWidthPercentage, percentage > 100 {
|
_controlWidth = newValue
|
||||||
controlWidthPercentage = 100
|
}
|
||||||
|
case .value(let value):
|
||||||
|
if value > 0 && value > containerSize.width {
|
||||||
|
_controlWidth = newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_controlWidth = nil
|
||||||
}
|
}
|
||||||
updateControlWidthPercentage()
|
|
||||||
setNeedsUpdate()
|
setNeedsUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,7 +74,6 @@ open class InputStepper: EntryFieldBase {
|
|||||||
if let percentage = widthPercentage, percentage > 100 {
|
if let percentage = widthPercentage, percentage > 100 {
|
||||||
widthPercentage = 100
|
widthPercentage = 100
|
||||||
}
|
}
|
||||||
updatePercentageWidth()
|
|
||||||
setNeedsUpdate()
|
setNeedsUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,6 +120,8 @@ open class InputStepper: EntryFieldBase {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Properties
|
// MARK: - Private Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
private var _controlWidth: ControlWidth? = nil
|
||||||
|
|
||||||
/// Default Int value of the input stepper, defaults to '0'.
|
/// Default Int value of the input stepper, defaults to '0'.
|
||||||
internal var defaultIntValue: Int {
|
internal var defaultIntValue: Int {
|
||||||
guard let intValue = defaultValue as? Int else { return 0 }
|
guard let intValue = defaultValue as? Int else { return 0 }
|
||||||
@ -236,8 +249,8 @@ open class InputStepper: EntryFieldBase {
|
|||||||
updateButtonStates()
|
updateButtonStates()
|
||||||
|
|
||||||
// Update stepper container border and corner radius.
|
// Update stepper container border and corner radius.
|
||||||
setControlWidth(controlWidth)
|
updateContainerWidthWithPercentage()
|
||||||
updateControlWidthPercentage()
|
updateInputStepperWidth()
|
||||||
updateStepperView()
|
updateStepperView()
|
||||||
setNeedsLayout()
|
setNeedsLayout()
|
||||||
}
|
}
|
||||||
@ -271,8 +284,7 @@ open class InputStepper: EntryFieldBase {
|
|||||||
super.reset()
|
super.reset()
|
||||||
textLabel.reset()
|
textLabel.reset()
|
||||||
textLabel.textStyle = .boldBodyLarge
|
textLabel.textStyle = .boldBodyLarge
|
||||||
controlWidth = "auto"
|
controlWidth = nil
|
||||||
controlWidthPercentage = nil
|
|
||||||
widthPercentage = nil
|
widthPercentage = nil
|
||||||
id = nil
|
id = nil
|
||||||
minValue = 0
|
minValue = 0
|
||||||
@ -286,6 +298,7 @@ open class InputStepper: EntryFieldBase {
|
|||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
internal override func updateContainerWidth() {
|
internal override func updateContainerWidth() {
|
||||||
|
stepperWidthConstraint?.deactivate()
|
||||||
widthConstraint?.deactivate()
|
widthConstraint?.deactivate()
|
||||||
trailingLessThanEqualsConstraint?.deactivate()
|
trailingLessThanEqualsConstraint?.deactivate()
|
||||||
trailingEqualsConstraint?.deactivate()
|
trailingEqualsConstraint?.deactivate()
|
||||||
@ -361,17 +374,25 @@ open class InputStepper: EntryFieldBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set control width to input stepper.
|
// Set control width to input stepper.
|
||||||
private func setControlWidth(_ text: String?) {
|
private func updateInputStepperWidth() {
|
||||||
if let text, text == "auto" {
|
guard let controlWidth else {
|
||||||
stepperWidthConstraint?.deactivate()
|
return
|
||||||
} else if let controlWidth = Int(text ?? "") {
|
}
|
||||||
// Set controlWidth provided which is either pixel or percentage
|
|
||||||
let width = width ?? CGFloat(containerView.frame.size.width)
|
switch controlWidth {
|
||||||
updateStepperContainerWidth(controlWidth: CGFloat(controlWidth), width: width)
|
case .percentage(let percentage):
|
||||||
|
// Set the inputStepper's controlWidth based on percentage received relative to its parentView's frame.
|
||||||
|
let superWidth = width ?? CGFloat(containerView.frame.size.width)
|
||||||
|
let value = max(superWidth * ((percentage) / 100), minWidth)
|
||||||
|
updateStepperContainerWidth(controlWidth: value, width: superWidth)
|
||||||
|
|
||||||
|
case .value(let value):
|
||||||
|
let superWidth = width ?? CGFloat(containerView.frame.size.width)
|
||||||
|
updateStepperContainerWidth(controlWidth: value, width: superWidth)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handling the controlwidth without going beyond the width of the parent container.
|
// Handling the controlwidth without exceeding the width of the parent container.
|
||||||
private func updateStepperContainerWidth(controlWidth: CGFloat, width: CGFloat) {
|
private func updateStepperContainerWidth(controlWidth: CGFloat, width: CGFloat) {
|
||||||
if controlWidth >= containerSize.width && controlWidth <= width {
|
if controlWidth >= containerSize.width && controlWidth <= width {
|
||||||
stepperWidthConstraint?.deactivate()
|
stepperWidthConstraint?.deactivate()
|
||||||
@ -384,30 +405,21 @@ open class InputStepper: EntryFieldBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateControlWidthPercentage() {
|
// Update the container view width based on the percentage received.
|
||||||
let superWidth = width ?? CGFloat(containerView.frame.size.width)
|
private func updateContainerWidthWithPercentage() {
|
||||||
|
|
||||||
// Set the inputStepper's controlWidth based on the controlWidth percentage received relative to its parentView's frame.
|
|
||||||
if let controlWidthPercentage {
|
|
||||||
controlWidth = String( Int( max(superWidth * ((controlWidthPercentage) / 100), minWidth)))
|
|
||||||
setControlWidth(controlWidth)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func updatePercentageWidth() {
|
|
||||||
guard let superWidth = superview?.frame.width else {
|
guard let superWidth = superview?.frame.width else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set width of Parent container based on width perecentage received relative to its superview frame.
|
// Set width of Parent container based on width perecentage received relative to its superview frame.
|
||||||
if let widthPercentage {
|
if let widthPercentage {
|
||||||
width = max(superWidth * ((widthPercentage) / 100), minWidth)
|
// test value vs minimum width and take the greater value
|
||||||
if controlWidthPercentage != nil {
|
width = max(superWidth * (widthPercentage / 100), minWidth)
|
||||||
updateControlWidthPercentage()
|
updateContainerWidth()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add border and update constratints to stepper view.
|
||||||
private func updateStepperView() {
|
private func updateStepperView() {
|
||||||
fieldStackView.removeConstraints()
|
fieldStackView.removeConstraints()
|
||||||
fieldStackView.pinTop().pinLeading().pinBottom().pinTrailingLessThanOrEqualTo()
|
fieldStackView.pinTop().pinLeading().pinBottom().pinTrailingLessThanOrEqualTo()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user