-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeymeTestsStartFeature.swift
90 lines (74 loc) · 2.78 KB
/
KeymeTestsStartFeature.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
//
// KeymeTestsStartFeature.swift
// Features
//
// Created by 김영인 on 2023/08/12.
// Copyright © 2023 team.humanwave. All rights reserved.
//
import CoreFoundation
import ComposableArchitecture
import Domain
public struct KeymeTestsStartFeature: Reducer {
public struct State: Equatable {
public var keymeTests: KeymeTestsFeature.State?
public var isAnimating: Bool = false
public var nickname: String?
public var testId: Int = 18 // TODO: change
public var icon: IconModel = .EMPTY
public init() { }
}
public enum Action {
case viewWillAppear
case fetchDailyTests(TaskResult<KeymeTestsModel>)
case startAnimation([IconModel])
case setIcon(IconModel)
case startButtonDidTap
case keymeTests(KeymeTestsFeature.Action)
}
@Dependency(\.continuousClock) var clock
@Dependency(\.keymeTestsClient) var keymeTestsClient
public init() { }
public var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .viewWillAppear:
return .run { send in
await send(.fetchDailyTests(
TaskResult {
try await self.keymeTestsClient.fetchDailyTests()
}
))
}
case let .fetchDailyTests(.success(tests)):
state.nickname = tests.nickname
state.testId = tests.testId
state.isAnimating = true
return .send(.startAnimation(tests.icons))
case .fetchDailyTests(.failure):
state.nickname = "키미" // TODO: 변경
return .send(.startAnimation([IconModel.EMPTY]))
case .startAnimation(let icons):
return .run { send in
repeat {
for icon in icons {
await send(.setIcon(icon))
try await self.clock.sleep(for: .seconds(1.595))
}
} while true
}
case let .setIcon(icon):
state.icon = icon
state.isAnimating = true
case .startButtonDidTap:
let url = "https://keyme-frontend.vercel.app/test/\(state.testId)"
state.keymeTests = KeymeTestsFeature.State(url: url)
case .keymeTests:
return .none
}
return .none
}
.ifLet(\.keymeTests, action: /Action.keymeTests) {
KeymeTestsFeature()
}
}
}