forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicalUserInterfaceController.swift.swift
More file actions
157 lines (138 loc) · 5.4 KB
/
GraphicalUserInterfaceController.swift.swift
File metadata and controls
157 lines (138 loc) · 5.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
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
import AppKit
import ChatTab
import ComposableArchitecture
import Environment
import Preferences
import SuggestionWidget
struct GUI: ReducerProtocol {
struct State: Equatable {
var suggestionWidgetState = WidgetFeature.State()
var chatTabGroup: ChatPanelFeature.ChatTabGroup {
get { suggestionWidgetState.chatPanelState.chatTapGroup }
set { suggestionWidgetState.chatPanelState.chatTapGroup = newValue }
}
}
enum Action {
case openChatPanel(forceDetach: Bool)
case createChatGPTChatTabIfNeeded
case sendCustomCommandToActiveChat(CustomCommand)
case suggestionWidget(WidgetFeature.Action)
}
var body: some ReducerProtocol<State, Action> {
Scope(state: \.suggestionWidgetState, action: /Action.suggestionWidget) {
WidgetFeature()
}
Scope(
state: \.chatTabGroup,
action: /Action.suggestionWidget .. /WidgetFeature.Action.chatPanel
) {
Reduce { _, action in
switch action {
case let .createNewTapButtonClicked(type):
_ = type // always ChatGPTChatTab at the moment.
let chatTap = ChatGPTChatTab()
return .run { send in
await send(.appendAndSelectTab(chatTap))
}
default:
return .none
}
}
}
Reduce { state, action in
switch action {
case let .openChatPanel(forceDetach):
return .run { send in
await send(
.suggestionWidget(.chatPanel(.presentChatPanel(forceDetach: forceDetach)))
)
}
case .createChatGPTChatTabIfNeeded:
if state.chatTabGroup.tabs.contains(where: { $0 is ChatGPTChatTab }) {
return .none
}
let chatTab = ChatGPTChatTab()
state.chatTabGroup.tabs.append(chatTab)
return .none
case let .sendCustomCommandToActiveChat(command):
@Sendable func stopAndHandleCommand(_ tab: ChatGPTChatTab) async {
if tab.service.isReceivingMessage {
await tab.service.stopReceivingMessage()
}
try? await tab.service.handleCustomCommand(command)
}
if let activeTab = state.chatTabGroup.activeChatTab as? ChatGPTChatTab {
return .run { send in
await send(.openChatPanel(forceDetach: false))
await stopAndHandleCommand(activeTab)
}
}
if let chatTab = state.chatTabGroup.tabs.first(where: {
guard $0 is ChatGPTChatTab else { return false }
return true
}) as? ChatGPTChatTab {
state.chatTabGroup.selectedTabId = chatTab.id
return .run { send in
await send(.openChatPanel(forceDetach: false))
await stopAndHandleCommand(chatTab)
}
}
let chatTab = ChatGPTChatTab()
state.chatTabGroup.tabs.append(chatTab)
return .run { send in
await send(.openChatPanel(forceDetach: false))
await stopAndHandleCommand(chatTab)
}
case .suggestionWidget:
return .none
}
}
}
}
@MainActor
public final class GraphicalUserInterfaceController {
public static let shared = GraphicalUserInterfaceController()
private let store: StoreOf<GUI>
let widgetController: SuggestionWidgetController
let widgetDataSource: WidgetDataSource
let viewStore: ViewStoreOf<GUI>
private init() {
let suggestionDependency = SuggestionWidgetControllerDependency()
let store = StoreOf<GUI>(
initialState: .init(),
reducer: GUI()
) { dependencies in
dependencies.suggestionWidgetControllerDependency = suggestionDependency
dependencies.suggestionWidgetUserDefaultsObservers = .init()
}
self.store = store
viewStore = ViewStore(store)
widgetDataSource = .init()
widgetController = SuggestionWidgetController(
store: store.scope(
state: \.suggestionWidgetState,
action: GUI.Action.suggestionWidget
),
dependency: suggestionDependency
)
suggestionDependency.suggestionWidgetDataSource = widgetDataSource
suggestionDependency.onOpenChatClicked = { [weak self] in
Task { [weak self] in
await self?.viewStore.send(.createChatGPTChatTabIfNeeded).finish()
self?.viewStore.send(.openChatPanel(forceDetach: false))
}
}
suggestionDependency.onCustomCommandClicked = { command in
Task {
let commandHandler = PseudoCommandHandler()
await commandHandler.handleCustomCommand(command)
}
}
}
public func openGlobalChat() {
Task {
await self.viewStore.send(.createChatGPTChatTabIfNeeded).finish()
viewStore.send(.openChatPanel(forceDetach: true))
}
}
}