-
Notifications
You must be signed in to change notification settings - Fork 9
/
FactoryReset+Reducer.swift
106 lines (89 loc) · 2.41 KB
/
FactoryReset+Reducer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// MARK: - FactoryReset
struct FactoryReset: Sendable, FeatureReducer {
struct State: Sendable, Hashable {
var isRecoverable = true
@PresentationState
var destination: Destination.State?
init() {}
}
enum ViewAction: Sendable, Equatable {
case onFirstTask
case resetWalletButtonTapped
}
enum InternalAction: Sendable, Equatable {
case setIsRecoverable(Bool)
}
struct Destination: DestinationReducer {
@CasePathable
enum State: Sendable, Hashable {
case confirmReset(AlertState<Action.ConfirmReset>)
}
@CasePathable
enum Action: Sendable, Hashable {
case confirmReset(ConfirmReset)
enum ConfirmReset: Sendable, Hashable {
case confirm
}
}
var body: some Reducer<State, Action> {
EmptyReducer()
}
}
@Dependency(\.securityCenterClient) var securityCenterClient
@Dependency(\.resetWalletClient) var resetWalletClient
init() {}
var body: some ReducerOf<FactoryReset> {
Reduce(core)
.ifLet(destinationPath, action: /Action.destination) {
Destination()
}
}
private let destinationPath: WritableKeyPath<State, PresentationState<Destination.State>> = \.$destination
func reduce(into state: inout State, viewAction: ViewAction) -> Effect<Action> {
switch viewAction {
case .onFirstTask:
return isRecoverableEffect()
case .resetWalletButtonTapped:
state.destination = Destination.confirmResetState
return .none
}
}
func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case let .setIsRecoverable(isRecoverable):
state.isRecoverable = isRecoverable
return .none
}
}
func reduce(into state: inout State, presentedAction: Destination.Action) -> Effect<Action> {
switch presentedAction {
case .confirmReset(.confirm):
.run { _ in
await resetWalletClient.resetWallet()
}
}
}
private func isRecoverableEffect() -> Effect<Action> {
.run { send in
for try await isRecoverable in await securityCenterClient.isRecoverable() {
guard !Task.isCancelled else { return }
await send(.internal(.setIsRecoverable(isRecoverable)))
}
}
}
}
extension FactoryReset.Destination {
static let confirmResetState: State = .confirmReset(.init(
title: {
TextState(L10n.FactoryReset.Dialog.title)
},
actions: {
ButtonState(role: .destructive, action: .confirm) {
TextState(L10n.Common.confirm)
}
},
message: {
TextState(L10n.FactoryReset.Dialog.message)
}
))
}