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