-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathPanelFeature.swift
More file actions
176 lines (147 loc) · 6.2 KB
/
PanelFeature.swift
File metadata and controls
176 lines (147 loc) · 6.2 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import AppKit
import ComposableArchitecture
import Foundation
@Reducer
public struct PanelFeature {
@ObservableState
public struct State: Equatable {
public var content: SharedPanelFeature.Content {
get { sharedPanelState.content }
set {
sharedPanelState.content = newValue
suggestionPanelState.content = newValue.suggestion
}
}
// MARK: SharedPanel
var sharedPanelState = SharedPanelFeature.State()
// MARK: SuggestionPanel
var suggestionPanelState = SuggestionPanelFeature.State()
var warningMessage: String?
var warningURL: String?
}
public enum Action: Equatable {
case presentSuggestion
case presentSuggestionProvider(CodeSuggestionProvider, displayContent: Bool)
case presentError(String)
case presentPromptToCode(PromptToCodeGroup.PromptToCodeInitialState)
case displayPanelContent
case expandSuggestion
case discardSuggestion
case removeDisplayedContent
case switchToAnotherEditorAndUpdateContent
case hidePanel
case showPanel
case sharedPanel(SharedPanelFeature.Action)
case suggestionPanel(SuggestionPanelFeature.Action)
case presentWarning(message: String, url: String?)
case dismissWarning
}
@Dependency(\.suggestionWidgetControllerDependency) var suggestionWidgetControllerDependency
@Dependency(\.xcodeInspector) var xcodeInspector
@Dependency(\.activateThisApp) var activateThisApp
var windows: WidgetWindows? { suggestionWidgetControllerDependency.windowsController?.windows }
public var body: some ReducerOf<Self> {
Scope(state: \.suggestionPanelState, action: \.suggestionPanel) {
SuggestionPanelFeature()
}
Scope(state: \.sharedPanelState, action: \.sharedPanel) {
SharedPanelFeature()
}
Reduce { state, action in
switch action {
case .presentSuggestion:
return .run { send in
guard let fileURL = await xcodeInspector.safe.activeDocumentURL,
let provider = await fetchSuggestionProvider(fileURL: fileURL)
else { return }
await send(.presentSuggestionProvider(provider, displayContent: true))
}
case let .presentSuggestionProvider(provider, displayContent):
state.content.suggestion = provider
if displayContent {
return .run { send in
await send(.displayPanelContent)
}.animation(.easeInOut(duration: 0.2))
}
return .none
case let .presentError(errorDescription):
state.content.error = errorDescription
return .run { send in
await send(.displayPanelContent)
}.animation(.easeInOut(duration: 0.2))
case let .presentPromptToCode(initialState):
return .run { send in
await send(.sharedPanel(.promptToCodeGroup(.createPromptToCode(initialState))))
}
case .displayPanelContent:
if !state.sharedPanelState.isEmpty {
state.sharedPanelState.isPanelDisplayed = true
}
if state.suggestionPanelState.content != nil {
state.suggestionPanelState.isPanelDisplayed = true
}
return .none
case .discardSuggestion:
state.content.suggestion = nil
return .none
case .expandSuggestion:
state.content.isExpanded = true
return .none
case .switchToAnotherEditorAndUpdateContent:
return .run { send in
guard let fileURL = await xcodeInspector.safe.realtimeActiveDocumentURL
else { return }
await send(.sharedPanel(
.promptToCodeGroup(
.updateActivePromptToCode(documentURL: fileURL)
)
))
}
case .hidePanel:
state.suggestionPanelState.isPanelDisplayed = false
return .none
case .showPanel:
state.suggestionPanelState.isPanelDisplayed = true
return .none
case .removeDisplayedContent:
state.content.error = nil
state.content.suggestion = nil
return .none
case .sharedPanel(.promptToCodeGroup(.activateOrCreatePromptToCode)),
.sharedPanel(.promptToCodeGroup(.createPromptToCode)):
let hasPromptToCode = state.content.promptToCode != nil
return .run { send in
await send(.displayPanelContent)
if hasPromptToCode {
activateThisApp()
await MainActor.run {
windows?.sharedPanelWindow.makeKey()
}
}
}.animation(.easeInOut(duration: 0.2))
case .sharedPanel:
return .none
case .suggestionPanel:
return .none
case .presentWarning(let message, let url):
state.warningMessage = message
state.warningURL = url
state.suggestionPanelState.warningMessage = message
state.suggestionPanelState.warningURL = url
return .none
case .dismissWarning:
state.warningMessage = nil
state.warningURL = nil
state.suggestionPanelState.warningMessage = nil
state.suggestionPanelState.warningURL = nil
return .none
}
}
}
func fetchSuggestionProvider(fileURL: URL) async -> CodeSuggestionProvider? {
guard let provider = await suggestionWidgetControllerDependency
.suggestionWidgetDataSource?
.suggestionForFile(at: fileURL) else { return nil }
return provider
}
}