Skip to content

Commit

Permalink
Handle errors on UI and calcState
Browse files Browse the repository at this point in the history
  • Loading branch information
JALVITO committed May 13, 2020
1 parent a5f02d8 commit 3ac3085
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 54 deletions.
74 changes: 55 additions & 19 deletions BaseCalc/EnvironmentalObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ class CalculatorState: ObservableObject {
let digitToAddIsDot = digitToAdd == "."

if willPerformOperation {
do {
prevNumber = try Number(number: currentText, base: currentBase)
} catch {
print("Error")
return
}

if digitToAddIsDot {
currentText = "0."
} else {
Expand Down Expand Up @@ -94,6 +87,10 @@ class CalculatorState: ObservableObject {
prevOperation = nil
prevNumber = nil
}

func enterErrorState() {
currentText = "Error"
}

func isInvalidForBitOperations() -> Bool {
isNegative || hasDecimalDot
Expand All @@ -110,6 +107,11 @@ class CalculatorState: ObservableObject {
solve()
} else {
willPerformOperation = true
do {
prevNumber = try Number(number: currentText, base: currentBase)
} catch {
enterErrorState()
}
}
}

Expand All @@ -135,44 +137,78 @@ class CalculatorState: ObservableObject {
hasDecimalDot = currNumber.hasFract
currentBase = newBase
} catch {
print("Error")
enterErrorState()
}
}

func getFloatingPoint() -> String {
do {
let number = try Number(number: currentText, base: currentBase)
return String(describing: number.getFloatingPoint())
} catch {
return "Error"
}
}

func getRadixComplement(digits: Int) {
do {
let number = try Number(number: currentText, base: currentBase)
let complement = try number.radixComplement(digits: digits)
currentText = complement.toString()
} catch {
enterErrorState()
}
}

func getRadixComplementDiminished(digits: Int) {
do {
let number = try Number(number: currentText, base: currentBase)
let complement = try number.radixComplementDiminished(digits: digits)
currentText = complement.toString()
} catch {
enterErrorState()
}
}

func solve() {
do {
let currentNumber = try Number(number: currentText, base: currentBase)

if prevNumber == nil {
enterErrorState()
return
}

switch prevOperation {
case .add:
changePrevNumber(answer: try (prevNumber ?? currentNumber) + currentNumber)
changePrevNumber(answer: try prevNumber! + currentNumber)
case .subtract:
changePrevNumber(answer: try (prevNumber ?? currentNumber) - currentNumber)
changePrevNumber(answer: try prevNumber! - currentNumber)
case .multiply:
changePrevNumber(answer: try (prevNumber ?? currentNumber) * currentNumber)
changePrevNumber(answer: try prevNumber! * currentNumber)
case .divide:
changePrevNumber(answer: try (prevNumber ?? currentNumber) / currentNumber)
changePrevNumber(answer: try prevNumber! / currentNumber)
case .and:
changePrevNumber(answer: try (prevNumber ?? currentNumber) & currentNumber)
changePrevNumber(answer: try prevNumber! & currentNumber)
case .or:
changePrevNumber(answer: try (prevNumber ?? currentNumber) | currentNumber)
changePrevNumber(answer: try prevNumber! | currentNumber)
case .xor:
changePrevNumber(answer: try (prevNumber ?? currentNumber) ^ currentNumber)
changePrevNumber(answer: try prevNumber! ^ currentNumber)
case .nor:
changePrevNumber(answer: try (prevNumber ?? currentNumber) ~| currentNumber)
changePrevNumber(answer: try prevNumber! ~| currentNumber)
case .leftShiftN:
changePrevNumber(answer: try (prevNumber ?? currentNumber) << currentNumber)
changePrevNumber(answer: try prevNumber! << currentNumber)
case .leftShift1:
changePrevNumber(answer: try currentNumber << Number(number: "1", base: currentBase))
case .rightShiftN:
changePrevNumber(answer: try (prevNumber ?? currentNumber) >> currentNumber)
changePrevNumber(answer: try prevNumber! >> currentNumber)
case .rightShift1:
changePrevNumber(answer: try currentNumber >> Number(number: "1", base: currentBase))
default:
print(prevOperation == nil)
}
} catch {
print("Error")
enterErrorState()
}
}

Expand Down
20 changes: 4 additions & 16 deletions BaseCalc/Views/ComplementAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,17 @@ struct AlertButtons: View {
Spacer()

Button(action: {
do {
let number = try Number(number: self.calculatorState.currentText, base: self.calculatorState.currentBase)
let complement = try number.radixComplement(digits: Int(self.digits))
self.calculatorState.currentText = complement.toString()
self.manager.isShowing.toggle()
} catch {
print("Error")
}
self.calculatorState.getRadixComplement(digits: Int(self.digits)!)
self.manager.isShowing.toggle()
}){
Text("ß compl.")
}.disabled(!isDigitCountValid)

Spacer()

Button(action: {
do {
let number = try Number(number: self.calculatorState.currentText, base: self.calculatorState.currentBase)
let complement = try number.radixComplementDiminished(digits: Int(self.digits))
self.calculatorState.currentText = complement.toString()
self.manager.isShowing.toggle()
} catch {
print("Error")
}
self.calculatorState.getRadixComplementDiminished(digits: Int(self.digits)!)
self.manager.isShowing.toggle()
}){
Text("ß⁻¹ compl.")
}.disabled(!isDigitCountValid)
Expand Down
23 changes: 4 additions & 19 deletions BaseCalc/Views/FloatingPointAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,15 @@ struct FloatingPointAlert: View {

var body: some View {
GeneralAlert(isShowing: manager.isShowing) {
FloatingPointAlertContent(
val: self.calculatorState.currentText,
base: self.calculatorState.currentBase
)
FloatingPointAlertContent()
}
}
}

struct FloatingPointAlertContent: View {
@EnvironmentObject var manager: FloatingPointAlertManager
@EnvironmentObject var calculatorState: CalculatorState
@EnvironmentObject var layout: LayoutState
let single: String

init(val: String, base: Base) {
do {
let number = try Number(number: val, base: base)

self.single = String(describing: number.getFloatingPoint())
// self.double = String(describing: number.getFloatingPoint(exponentDigits: 11, mantissaDigits: 52))
} catch {
print("Error")
self.single = "Error"
}
}

var body: some View {
VStack(alignment: .center) {
Expand All @@ -55,7 +40,7 @@ struct FloatingPointAlertContent: View {
.font(.subheadline)
.foregroundColor(.white)

Text(single)
Text(calculatorState.getFloatingPoint())
.font(.subheadline)
.foregroundColor(.gray)

Expand All @@ -68,7 +53,7 @@ struct FloatingPointAlertContent: View {
.font(.subheadline)
.foregroundColor(.white)

Text(single)
Text(calculatorState.getFloatingPoint())
.font(.subheadline)
.foregroundColor(.gray)
}
Expand Down

0 comments on commit 3ac3085

Please sign in to comment.