forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIKeyManangement.swift
More file actions
80 lines (65 loc) · 2.4 KB
/
APIKeyManangement.swift
File metadata and controls
80 lines (65 loc) · 2.4 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import ComposableArchitecture
import Foundation
struct APIKeyManagement: ReducerProtocol {
struct State: Equatable {
var availableAPIKeyNames: [String] = []
@PresentationState var apiKeySubmission: APIKeySubmission.State?
}
enum Action: Equatable {
case appear
case closeButtonClicked
case addButtonClicked
case deleteButtonClicked(name: String)
case refreshAvailableAPIKeyNames
case apiKeySubmission(PresentationAction<APIKeySubmission.Action>)
}
@Dependency(\.toast) var toast
@Dependency(\.apiKeyKeychain) var keychain
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case .appear:
if isPreview { return .none }
return .run { send in
await send(.refreshAvailableAPIKeyNames)
}
case .closeButtonClicked:
return .none
case .addButtonClicked:
state.apiKeySubmission = .init()
return .none
case let .deleteButtonClicked(name):
do {
try keychain.remove(name)
return .run { send in
await send(.refreshAvailableAPIKeyNames)
}
} catch {
toast(error.localizedDescription, .error)
return .none
}
case .refreshAvailableAPIKeyNames:
do {
let pairs = try keychain.getAll()
state.availableAPIKeyNames = Array(pairs.keys).sorted()
} catch {
toast(error.localizedDescription, .error)
}
return .none
case .apiKeySubmission(.presented(.saveFinished)):
state.apiKeySubmission = nil
return .run { send in
await send(.refreshAvailableAPIKeyNames)
}
case .apiKeySubmission(.presented(.cancelButtonClicked)):
state.apiKeySubmission = nil
return .none
case .apiKeySubmission:
return .none
}
}
.ifLet(\.$apiKeySubmission, action: /Action.apiKeySubmission) {
APIKeySubmission()
}
}
}