forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIKeySubmission.swift
More file actions
59 lines (48 loc) · 1.55 KB
/
APIKeySubmission.swift
File metadata and controls
59 lines (48 loc) · 1.55 KB
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
import ComposableArchitecture
import Foundation
struct APIKeySubmission: ReducerProtocol {
struct State: Equatable {
@BindingState var name: String = ""
@BindingState var key: String = ""
}
enum Action: Equatable, BindableAction {
case binding(BindingAction<State>)
case saveButtonClicked
case cancelButtonClicked
case saveFinished
}
@Dependency(\.toast) var toast
@Dependency(\.apiKeyKeychain) var keychain
enum E: Error, LocalizedError {
case nameIsEmpty
case keyIsEmpty
}
var body: some ReducerProtocol<State, Action> {
BindingReducer()
Reduce { state, action in
switch action {
case .saveButtonClicked:
do {
guard !state.name.isEmpty else { throw E.nameIsEmpty }
guard !state.key.isEmpty else { throw E.keyIsEmpty }
try keychain.update(
state.key,
key: state.name.trimmingCharacters(in: .whitespacesAndNewlines)
)
return .run { send in
await send(.saveFinished)
}
} catch {
toast(error.localizedDescription, .error)
return .none
}
case .cancelButtonClicked:
return .none
case .saveFinished:
return .none
case .binding:
return .none
}
}
}
}