-
Notifications
You must be signed in to change notification settings - Fork 9
/
ImportOlympiaNameLedger.swift
72 lines (59 loc) · 1.82 KB
/
ImportOlympiaNameLedger.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import ComposableArchitecture
// MARK: - ImportOlympiaNameLedger
struct ImportOlympiaNameLedger: Sendable, FeatureReducer {
struct State: Sendable, Hashable {
var nameLedger: NameLedgerFactorSource.State
init(deviceInfo: LedgerDeviceInfo) {
self.nameLedger = .init(deviceInfo: deviceInfo)
}
}
enum ViewAction: Sendable, Equatable {
case closeButtonTapped
}
@CasePathable
enum ChildAction: Sendable, Equatable {
case nameLedger(NameLedgerFactorSource.Action)
}
enum DelegateAction: Sendable, Equatable {
case savedNewLedger(LedgerHardwareWalletFactorSource)
case failedToSaveNewLedger
}
@Dependency(\.dismiss) var dismiss
@Dependency(\.errorQueue) var errorQueue
@Dependency(\.factorSourcesClient) var factorSourcesClient
init() {}
var body: some ReducerOf<Self> {
Scope(state: \.nameLedger, action: /Action.child .. ChildAction.nameLedger) {
NameLedgerFactorSource()
}
Reduce(core)
}
func reduce(into state: inout State, viewAction: ViewAction) -> Effect<Action> {
switch viewAction {
case .closeButtonTapped:
.run { _ in
await dismiss()
}
}
}
func reduce(into state: inout State, childAction: ChildAction) -> Effect<Action> {
switch childAction {
case let .nameLedger(.delegate(.complete(ledger))):
saveNewLedger(ledger)
case .nameLedger(.delegate(.failedToCreateLedgerFactorSource)):
.send(.delegate(.failedToSaveNewLedger))
default:
.none
}
}
private func saveNewLedger(_ ledger: LedgerHardwareWalletFactorSource) -> Effect<Action> {
.run { send in
try await factorSourcesClient.saveFactorSource(ledger.asGeneral)
loggerGlobal.notice("Saved Ledger factor source! ✅")
await send(.delegate(.savedNewLedger(ledger)))
} catch: { error, _ in
loggerGlobal.error("Failed to save Factor Source, error: \(error)")
errorQueue.schedule(error)
}
}
}