Skip to content

Commit 2166af4

Browse files
committed
Isolate chat box and suggestion panel
1 parent 95abf9d commit 2166af4

File tree

7 files changed

+283
-78
lines changed

7 files changed

+283
-78
lines changed

Core/Sources/Service/SuggestionPresenter/PresentInWindowSuggestionPresenter.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ struct PresentInWindowSuggestionPresenter {
4646
controller.presentError(error.localizedDescription)
4747
}
4848
}
49+
50+
func closeChatRoom(fileURL: URL) {
51+
Task { @MainActor in
52+
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
53+
controller.closeChatRoom(fileURL: fileURL)
54+
}
55+
}
4956

5057
func presentChatGPTConversation(_ service: ChatGPTService, fileURL: URL) {
5158
let chatRoom = ChatRoom()
@@ -85,6 +92,13 @@ struct PresentInWindowSuggestionPresenter {
8592
}
8693
}
8794

95+
chatRoom.onClose = {
96+
Task {
97+
await service.stopReceivingMessage()
98+
closeChatRoom(fileURL: fileURL)
99+
}
100+
}
101+
88102
Task { @MainActor in
89103
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
90104
controller.presentChatRoom(chatRoom, fileURL: fileURL)

Core/Sources/SuggestionWidget/ChatRoom.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ public final class ChatRoom: ObservableObject, Equatable {
66
public var onMessageSend: (String) -> Void
77
public var onStop: () -> Void
88
public var onClear: () -> Void
9+
public var onClose: () -> Void
910

1011
public init(
1112
history: [ChatMessage] = [],
1213
isReceivingMessage: Bool = false,
1314
onMessageSend: @escaping (String) -> Void = { _ in },
1415
onStop: @escaping () -> Void = {},
15-
onClear: @escaping () -> Void = {}
16+
onClear: @escaping () -> Void = {},
17+
onClose: @escaping () -> Void = {}
1618
) {
1719
self.history = history
1820
self.isReceivingMessage = isReceivingMessage
1921
self.onMessageSend = onMessageSend
2022
self.onStop = onStop
2123
self.onClear = onClear
24+
self.onClose = onClose
2225
}
2326

2427
public static func == (lhs: ChatRoom, rhs: ChatRoom) -> Bool {
@@ -28,6 +31,7 @@ public final class ChatRoom: ObservableObject, Equatable {
2831
public func send(_ message: String) { onMessageSend(message) }
2932
public func stop() { onStop() }
3033
public func clear() { onClear() }
34+
public func close() { onClose() }
3135
}
3236

3337
public struct ChatMessage: Equatable {

Core/Sources/SuggestionWidget/SuggestionPanelContent/ChatPanel.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ struct ChatPanel: View {
2525

2626
// close button
2727
Button(action: {
28-
viewModel.isPanelDisplayed = false
29-
viewModel.content = .empty
30-
chat.stop()
28+
chat.close()
3129
}) {
3230
Image(systemName: "xmark")
3331
.padding(8)
@@ -196,7 +194,6 @@ struct ChatPanel_Preview: PreviewProvider {
196194

197195
static var previews: some View {
198196
ChatPanel(viewModel: .init(
199-
content: .empty,
200197
isPanelDisplayed: true
201198
), chat: .init(
202199
history: ChatPanel_Preview.history,
@@ -212,7 +209,6 @@ struct ChatPanel_Preview: PreviewProvider {
212209
struct ChatPanel_InputText_Preview: PreviewProvider {
213210
static var previews: some View {
214211
ChatPanel(viewModel: .init(
215-
content: .empty,
216212
isPanelDisplayed: true
217213
), chat: .init(
218214
history: ChatPanel_Preview.history,
@@ -229,7 +225,6 @@ struct ChatPanel_InputMultilineText_Preview: PreviewProvider {
229225
static var previews: some View {
230226
ChatPanel(
231227
viewModel: .init(
232-
content: .empty,
233228
isPanelDisplayed: true
234229
),
235230
chat: .init(
@@ -248,7 +243,6 @@ struct ChatPanel_InputMultilineText_Preview: PreviewProvider {
248243
struct ChatPanel_Light_Preview: PreviewProvider {
249244
static var previews: some View {
250245
ChatPanel(viewModel: .init(
251-
content: .empty,
252246
isPanelDisplayed: true
253247
), chat: .init(
254248
history: ChatPanel_Preview.history,

Core/Sources/SuggestionWidget/SuggestionPanelContent/ErrorPanel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct ErrorPanel: View {
1616
// close button
1717
Button(action: {
1818
viewModel.isPanelDisplayed = false
19-
viewModel.content = .empty
19+
viewModel.content = nil
2020
}) {
2121
Image(systemName: "xmark")
2222
.padding([.leading, .bottom], 16)

0 commit comments

Comments
 (0)