Skip to content

Commit

Permalink
Remove force-unwrapping when updating colors
Browse files Browse the repository at this point in the history
Closes #152, where tapping to another segmented-control option from the hex-color text field would crash the app:
#152
  • Loading branch information
bryanjclark committed Jan 14, 2021
1 parent 3802ad1 commit d7c337c
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions SwiftTweaks/TweakColorEditViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,44 @@ extension TweakColorEditViewController: TweakColorCellDelegate {
viewData = .hex(hex: oldHex, alpha: newNumber)
}
case let .rgBa(r: oldR, g: oldG, b: oldB, a: oldA):
switch changedValue.numericType! {
// NOTE : See discussion on https://github.com/Khan/SwiftTweaks/issues/152 for why this guard-statement is necessary
guard
let numericType = changedValue.numericType,
let numericValue = changedValue.numericValue
else {
return
}

switch numericType {
case .red:
viewData = .rgBa(r: changedValue.numericValue!, g: oldG, b: oldB, a: oldA)
viewData = .rgBa(r: numericValue, g: oldG, b: oldB, a: oldA)
case .green:
viewData = .rgBa(r: oldR, g: changedValue.numericValue!, b: oldB, a: oldA)
viewData = .rgBa(r: oldR, g: numericValue, b: oldB, a: oldA)
case .blue:
viewData = .rgBa(r: oldR, g: oldG, b: changedValue.numericValue!, a: oldA)
viewData = .rgBa(r: oldR, g: oldG, b: numericValue, a: oldA)
case .alpha:
viewData = .rgBa(r: oldR, g: oldG, b: oldB, a: changedValue.numericValue!)
viewData = .rgBa(r: oldR, g: oldG, b: oldB, a: numericValue)
case .hue, .saturation, .brightness:
break
}
case let .hsBa(h: oldH, s: oldS, b: oldB, a: oldA):
switch changedValue.numericType! {
// NOTE : See discussion on https://github.com/Khan/SwiftTweaks/issues/152 for why this guard-statement is necessary
guard
let numericType = changedValue.numericType,
let numericValue = changedValue.numericValue
else {
return
}

switch numericType {
case .hue:
viewData = .hsBa(h: changedValue.numericValue!, s: oldS, b: oldB, a: oldA)
viewData = .hsBa(h: numericValue, s: oldS, b: oldB, a: oldA)
case .saturation:
viewData = .hsBa(h: oldH, s: changedValue.numericValue!, b: oldB, a: oldA)
viewData = .hsBa(h: oldH, s: numericValue, b: oldB, a: oldA)
case .brightness:
viewData = .hsBa(h: oldH, s: oldS, b: changedValue.numericValue!, a: oldA)
viewData = .hsBa(h: oldH, s: oldS, b: numericValue, a: oldA)
case .alpha:
viewData = .hsBa(h: oldH, s: oldS, b: oldB, a: changedValue.numericValue!)
viewData = .hsBa(h: oldH, s: oldS, b: oldB, a: numericValue)
case .red, .green, .blue:
break
}
Expand Down

0 comments on commit d7c337c

Please sign in to comment.