Merge branch 'bugfix/MVAPCT-271' into 'develop'
Digital PCT265 defect MVAPCT-271 - Ensure the app doesn't crash on invalid color hex. ### Summary Ensure the app doesn't crash on invalid color hex. ### JIRA Ticket https://onejira.verizon.com/browse/MVAPCT-271 Co-authored-by: Scott Pfeil <Scott.Pfeil3@verizonwireless.com> See merge request https://gitlab.verizon.com/BPHV_MIPS/mvm_core_ui/-/merge_requests/1192
This commit is contained in:
commit
464e36606e
@ -44,6 +44,7 @@ public final class Color: Codable {
|
||||
|
||||
enum ColorError: Error {
|
||||
case badName(reason: String)
|
||||
case invalidHex(reason: String)
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
@ -53,21 +54,21 @@ public final class Color: Codable {
|
||||
public init(uiColor: UIColor) {
|
||||
self.uiColor = uiColor
|
||||
hex = UIColor.hexString(for: uiColor) ?? ""
|
||||
determineRGBA()
|
||||
try? determineRGBA()
|
||||
}
|
||||
|
||||
public init?(uiColor: UIColor?) {
|
||||
guard let uiColor = uiColor else { return nil }
|
||||
self.uiColor = uiColor
|
||||
hex = UIColor.hexString(for: uiColor) ?? ""
|
||||
determineRGBA()
|
||||
try? determineRGBA()
|
||||
}
|
||||
|
||||
init?(name: String) {
|
||||
guard let colorTuple = UIColor.names[name] else { return nil }
|
||||
self.uiColor = colorTuple.uiColor
|
||||
self.hex = colorTuple.hex
|
||||
determineRGBA()
|
||||
try? determineRGBA()
|
||||
}
|
||||
|
||||
public init?(colorString: String) throws {
|
||||
@ -75,7 +76,7 @@ public final class Color: Codable {
|
||||
self.uiColor = components.color
|
||||
hex = components.hex
|
||||
name = components.name ?? ""
|
||||
determineRGBA()
|
||||
try determineRGBA()
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
@ -99,7 +100,7 @@ public final class Color: Codable {
|
||||
name = components.name ?? ""
|
||||
}
|
||||
|
||||
determineRGBA()
|
||||
try determineRGBA()
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
@ -135,9 +136,12 @@ public final class Color: Codable {
|
||||
return CGFloat(Int(hex[start..<end], radix: 16) ?? 0)
|
||||
}
|
||||
|
||||
private func determineRGBA() {
|
||||
private func determineRGBA() throws {
|
||||
|
||||
guard !hex.isEmpty else { return }
|
||||
guard hex.count >= 6 else {
|
||||
throw ColorError.invalidHex(reason: "Invalid hex: \(hex)")
|
||||
}
|
||||
|
||||
let redStart = hex.startIndex
|
||||
let redEnd = hex.index(redStart, offsetBy: 2)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user