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

Commit

Permalink
@DependencyClient (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov authored Aug 12, 2024
1 parent 0d2c445 commit b92c9c3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Calcium/CalciumApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SwiftUI
@main
struct CalciumApp: App {
init() {
Resolver.register { KMMCalculator() }
Resolver.register { NativeCalculator() }
.implements(Calculator.self)
}

Expand Down
45 changes: 45 additions & 0 deletions Calcium/CalculatorClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// CalculatorClient.swift
// Calcium
//
// Created by Roman Podymov on 11/08/2024.
// Copyright © 2024 Calcium. All rights reserved.
//

import CalciumCommon
import ComposableArchitecture
import Resolver

@DependencyClient
struct CalculatorClient {
var calculateValue: (String, String, CalciumCommon.Operation) -> String = { _, _, _ in "" }
}

extension DependencyValues {
var calculator: CalculatorClient {
get { self[CalculatorClient.self] }
set { self[CalculatorClient.self] = newValue }
}
}

extension CalculatorClient: DependencyKey {
static let liveValue = CalculatorClient(
calculateValue: {
@Injected var calculator: Calculator

return calculator.calculateValue(lhs: $0, rhs: $1, operation: $2)
}
)
}

extension CalculatorClient: TestDependencyKey {
static let previewValue = {
@Injected var calculator: Calculator

return Self(calculateValue: {
NativeCalculator().calculateValue(lhs: $0, rhs: $1, operation: $2)
})
}()

static let testValue = previewValue
}
9 changes: 6 additions & 3 deletions Calcium/MainReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import BigNumber
import CalciumCommon
import ComposableArchitecture
import Resolver

enum MainReducerError: Error {
case invalidValues
}

@Reducer
struct MainReducer {
@Injected private var calculator: Calculator
@Dependency(\.calculator) var calculator

@ObservableState
struct State: Equatable {
Expand Down Expand Up @@ -142,7 +141,11 @@ struct MainReducer {
throw MainReducerError.invalidValues
}
let value = await Task {
let result = calculator.calculateValue(lhs: lhs.asString(radix: 10), rhs: rhs.asString(radix: 10), operation: operation)
let result = calculator.calculateValue(
lhs.asString(radix: 10),
rhs.asString(radix: 10),
operation
)
return BInt(result) ?? .init()
}.value
await send(.calculated(value), animation: .calciumDefault)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
plugins {
kotlin("multiplatform").version("2.0.20-Beta2").apply(false)
kotlin("multiplatform").version("2.0.20-RC").apply(false)
}

0 comments on commit b92c9c3

Please sign in to comment.