cleaned up code

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-08-15 15:43:01 -05:00
parent a95499b186
commit 69cfb38149

View File

@ -32,7 +32,7 @@ open class PriceLockup: View {
// MARK: - Enums
//--------------------------------------------------
/// Enum used to describe the term of PriceLockup.
public enum Term: String, CaseIterable {
public enum Term: String, DefaultValuing, CaseIterable {
case month, year, biennial, none
/// The default term is 'month'.
@ -54,38 +54,35 @@ open class PriceLockup: View {
}
/// Enum that represents the size availble for PriceLockup.
public enum Size: String, CaseIterable {
case xxxsmall
case xxsmall
case xsmall
public enum Size: String, DefaultValuing, CaseIterable {
case xxxsmall = "3XSmall"
case xxsmall = "2XSmall"
case xsmall = "XSmall"
case small
case medium
case large
case xlarge
case xxlarge
case xlarge = "XLarge"
case xxlarge = "2XLarge"
public static var defaultValue: Self { .medium }
}
/// Enum used to describe the kind of PriceLockup.
public enum Kind: String, CaseIterable {
public enum Kind: String, DefaultValuing, CaseIterable {
case primary, secondary, savings
/// The default kind is 'primary'.
public static var defaultValue : Self { .primary }
/// Color configuation relative to kind.
public var colorConfiguration: ViewColorConfiguration {
public var colorConfiguration: SurfaceColorConfiguration {
switch self {
case .primary:
return ViewColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false)}
return SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
case .secondary:
return ViewColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark, forDisabled: false)}
return SurfaceColorConfiguration(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark)
case .savings:
return ViewColorConfiguration().with {
$0.setSurfaceColors(VDSColor.paletteGreen26, VDSColor.paletteGreen36, forDisabled: false)}
return SurfaceColorConfiguration(VDSColor.paletteGreen26, VDSColor.paletteGreen36)
}
}
}
@ -107,11 +104,11 @@ open class PriceLockup: View {
open var price: Float? { didSet { setNeedsUpdate() } }
/// Color to the component. The default kind is primary.
open var kind: Kind = Kind.defaultValue { didSet { setNeedsUpdate() } }
open var kind: Kind = .defaultValue { didSet { setNeedsUpdate() } }
/// Size of the component. It varies by size and viewport(mobile/Tablet).
/// The default size is medium with viewport mobile.
open var size: Size = Size.defaultValue { didSet { setNeedsUpdate() } }
open var size: Size = .defaultValue { didSet { setNeedsUpdate() } }
/// If true, the component with a strikethrough. It applies only when uniformSize is true.
/// Does not apply a strikethrough format to leading and trailing text.
@ -119,7 +116,7 @@ open class PriceLockup: View {
/// Term text for the component. The default term is 'month'.
/// Superscript placement can vary when term and delimeter are "none".
open var term: Term = Term.defaultValue { didSet { setNeedsUpdate() } }
open var term: Term = .defaultValue { didSet { setNeedsUpdate() } }
/// Trailing text for the component.
open var trailingText: String? { didSet { setNeedsUpdate() } }
@ -210,20 +207,32 @@ open class PriceLockup: View {
open override func updateView() {
super.updateView()
priceLockupLabel.text = fetchText()
priceLockupLabel.text = formatText()
priceLockupLabel.surface = surface
// Set the attributed text
updateLabelAttributes()
}
open override func setDefaults() {
super.setDefaults()
bold = false
hideCurrency = false
leadingText = nil
price = nil
kind = .defaultValue
size = .defaultValue
strikethrough = false
term = .defaultValue
trailingText = nil
superscript = nil
uniformSize = false
}
/// Resets to default settings.
open override func reset() {
super.reset()
shouldUpdateView = false
priceLockupLabel.reset()
shouldUpdateView = true
setNeedsUpdate()
super.reset()
}
//--------------------------------------------------
@ -274,32 +283,44 @@ open class PriceLockup: View {
}
// Get text for PriceLockup.
open func fetchText() -> String {
private func formatText() -> String {
var text : String = ""
let space = " "
let delimiter = "/"
delimiterIndex = 0
strikethroughLength = 0
let currency: String = hideCurrency ? "" : "$"
if let leadingStr = leadingText {
text = text + leadingStr + space
delimiterIndex = delimiterIndex + leadingStr.count + space.count
if let leadingText {
text.append(leadingText)
text.append(space)
delimiterIndex = delimiterIndex + leadingText.count + space.count
}
strikethroughLocation = delimiterIndex
if let value = price {
let valueStr = "\(value.clean)"
text = text + currency + valueStr
delimiterIndex = delimiterIndex + valueStr.count + currency.count
strikethroughLength = valueStr.count + currency.count
if let price = price?.clean {
text.append(currency)
text.append(price)
delimiterIndex = delimiterIndex + price.count + currency.count
strikethroughLength = price.count + currency.count
}
if term != .none {
text = text + delimiter + term.type
text.append(delimiter)
text.append(term.type)
strikethroughLength = strikethroughLength + delimiter.count + term.type.count
}
if let trailingStr = trailingText {
text = text + space + trailingStr
if let trailingText {
text.append(space)
text.append(trailingText)
}
text = text + (superscript ?? "")
if let superscript {
text.append(superscript)
}
return text
}
}
@ -307,6 +328,6 @@ open class PriceLockup: View {
extension Float {
// remove a decimal from a float if the decimal is equal to 0
var clean: String {
return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(self)
return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(describing: self)
}
}