diff --git a/Calcium/CalciumApp.swift b/Calcium/CalciumApp.swift index 3ead7c6..e69d4ea 100644 --- a/Calcium/CalciumApp.swift +++ b/Calcium/CalciumApp.swift @@ -7,10 +7,16 @@ // import ComposableArchitecture +import Resolver import SwiftUI @main struct CalciumApp: App { + init() { + Resolver.register { NativeCalculator() } + .implements(Calculator.self) + } + var body: some Scene { WindowGroup { MainScreen( diff --git a/Calcium/Enums.swift b/Calcium/Enums.swift index 66bb4b2..8f0c6b2 100644 --- a/Calcium/Enums.swift +++ b/Calcium/Enums.swift @@ -9,6 +9,21 @@ import BigNumber import CalciumCommon +protocol Calculator { + func calculateValue(lhs: String, rhs: String, operation: CalciumCommon.Operation) -> String +} + +struct NativeCalculator: Calculator { + func calculateValue(lhs: String, rhs: String, operation: CalciumCommon.Operation) -> String { + .init( + operation.calculateValue( + lhs: .init(lhs) ?? .init(), + rhs: .init(rhs) ?? .init() + ) + ) + } +} + extension CalciumCommon.Digit: CalciumCommon.CalculatorButtonRepresentable { public var displayingValue: String { String(value) diff --git a/Calcium/MainReducer.swift b/Calcium/MainReducer.swift index a4f5428..b4a76b8 100644 --- a/Calcium/MainReducer.swift +++ b/Calcium/MainReducer.swift @@ -9,6 +9,7 @@ import BigNumber import CalciumCommon import ComposableArchitecture +import Resolver enum MainReducerError: Error { case invalidValues @@ -16,6 +17,8 @@ enum MainReducerError: Error { @Reducer struct MainReducer { + @Injected private var calculator: Calculator + @ObservableState struct State: Equatable { var enabled1 = true @@ -139,10 +142,8 @@ struct MainReducer { throw MainReducerError.invalidValues } let value = await Task { - operation.calculateValue( - lhs: lhs, - rhs: rhs - ) + let result = calculator.calculateValue(lhs: lhs.asString(radix: 10), rhs: rhs.asString(radix: 10), operation: operation) + return BInt(result) ?? .init() }.value await send(.calculated(value), animation: .calciumDefault) } diff --git a/project.yml b/project.yml index 534ffd8..0728725 100644 --- a/project.yml +++ b/project.yml @@ -11,6 +11,9 @@ packages: BigNumber: url: https://github.com/mkrd/Swift-BigInt version: 2.3.0 + Resolver: + url: https://github.com/hmlongco/Resolver + version: 1.5.1 targets: CalciumApp: type: application @@ -20,6 +23,7 @@ targets: - framework: CalciumCommon/build/XCFrameworks/release/CalciumCommon.xcframework - package: ComposableArchitecture - package: BigNumber + - package: Resolver info: path: Calcium/Info.plist postCompileScripts: