Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
Using BigNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov committed Jun 24, 2024
1 parent fc6fe27 commit ec10d9a
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions Calcium/MainReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct MainReducer {
displayingText = ""
}

mutating func onButtonPressed(calculatorButton: CalculatorButton) {
mutating func onButtonPressed(calculatorButton: CalculatorButton) -> Operation? {
switch calculatorButton {
case let .digit(value):
onDigitButtonPressed(value: value)
Expand All @@ -53,32 +53,48 @@ struct MainReducer {
case .equals:
switch latestOperationButton {
case let .operation(operation):
displayingText = String(
operation.calculateValue(
lhs: leftValue!,
rhs: BInt(displayingText)!
)
)
return operation
default:
break
}
latestOperationButton = .operation(.equals)
}
}
return nil
}
}

enum Action: Sendable {
case pressButton(CalculatorButton)
case calculated(BInt)
}

var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case let .pressButton(button):
state.onButtonPressed(calculatorButton: button)
let lhs = state.leftValue
let rhs = BInt(state.displayingText)
if let operation = state.onButtonPressed(calculatorButton: button) {
return .run { send in
await send(
.calculated(
operation.calculateValue(
lhs: lhs!,
rhs: rhs!
)
)
)
}
} else {
return .none
}
case let .calculated(value):
state.displayingText = String(value)
state.latestOperationButton = .operation(.equals)
return .none
}
}
}
}

extension BInt: @unchecked Sendable {}

0 comments on commit ec10d9a

Please sign in to comment.