-
Notifications
You must be signed in to change notification settings - Fork 9
/
ScanQRCoordinator+Reducer.swift
85 lines (72 loc) · 1.87 KB
/
ScanQRCoordinator+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
import ComposableArchitecture
import SwiftUI
// MARK: - ScanQRCoordinator
struct ScanQRCoordinator: Sendable, FeatureReducer {
struct State: Sendable, Hashable {
enum Step: Sendable, Hashable {
case cameraPermission(CameraPermission.State)
case scanQR(ScanQR.State)
init() {
self = .cameraPermission(.init())
}
}
var step: Step
let kind: ScanQR.Kind
init(
kind: ScanQR.Kind,
step: Step = .init()
) {
self.kind = kind
self.step = step
}
}
enum InternalAction: Sendable, Equatable {
case proceedWithScan
}
enum ChildAction: Sendable, Equatable {
case cameraPermission(CameraPermission.Action)
case scanQR(ScanQR.Action)
}
enum DelegateAction: Sendable, Equatable {
case dismiss
case scanned(String)
}
@Dependency(\.continuousClock) var clock
init() {}
var body: some ReducerOf<Self> {
Scope(state: \.step, action: /.self) {
Scope(state: /State.Step.cameraPermission, action: /Action.child .. ChildAction.cameraPermission) {
CameraPermission()
}
Scope(state: /State.Step.scanQR, action: /Action.child .. ChildAction.scanQR) {
ScanQR()
}
}
Reduce(core)
}
func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case .proceedWithScan:
state.step = .scanQR(.init(kind: state.kind))
return .none
}
}
func reduce(into state: inout State, childAction: ChildAction) -> Effect<Action> {
switch childAction {
case let .cameraPermission(.delegate(.permissionResponse(allowed))):
if allowed {
.run { send in
// FIXME: temporary hack to try to solve some navigation issues
try await clock.sleep(for: .milliseconds(900))
await send(.internal(.proceedWithScan))
}
} else {
.send(.delegate(.dismiss))
}
case let .scanQR(.delegate(.scanned(content))):
.send(.delegate(.scanned(content)))
default:
.none
}
}
}