-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy path01-02-03-code-0005.swift
75 lines (66 loc) · 1.81 KB
/
01-02-03-code-0005.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
import ComposableArchitecture
@Reducer
struct CounterFeature {
@ObservableState
struct State {
var count = 0
var fact: String?
var isLoading = false
var isTimerRunning = false
let timerID = UUID()
}
enum Action {
case decrementButtonTapped
case factButtonTapped
case factResponse(String)
case incrementButtonTapped
case timerTick
case toggleTimerButtonTapped
}
enum CancelID: Hashable { case timer(UUID) }
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .decrementButtonTapped:
state.count -= 1
state.fact = nil
return .none
case .factButtonTapped:
state.fact = nil
state.isLoading = true
return .run { [count = state.count] send in
let (data, _) = try await URLSession.shared
.data(from: URL(string: "http://numbersapi.com/\(count)")!)
let fact = String(decoding: data, as: UTF8.self)
await send(.factResponse(fact))
}
case let .factResponse(fact):
state.fact = fact
state.isLoading = false
return .none
case .incrementButtonTapped:
state.count += 1
state.fact = nil
return .none
case .timerTick:
state.count += 1
state.fact = nil
return .none
case .toggleTimerButtonTapped:
state.isTimerRunning.toggle()
let cancelID = CancelID.timer(state.timerID)
if state.isTimerRunning {
return .run { send in
while true {
try await Task.sleep(for: .seconds(1))
await send(.timerTick)
}
}
.cancellable(id: cancelID)
} else {
return .cancel(id: cancelID)
}
}
}
}
}