forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatRoom.swift
More file actions
47 lines (41 loc) · 1.43 KB
/
ChatRoom.swift
File metadata and controls
47 lines (41 loc) · 1.43 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
import Foundation
public final class ChatRoom: ObservableObject, Equatable {
@Published public var history: [ChatMessage] = []
@Published public var isReceivingMessage = false
public var onMessageSend: (String) -> Void
public var onStop: () -> Void
public var onClear: () -> Void
public var onClose: () -> Void
public init(
history: [ChatMessage] = [],
isReceivingMessage: Bool = false,
onMessageSend: @escaping (String) -> Void = { _ in },
onStop: @escaping () -> Void = {},
onClear: @escaping () -> Void = {},
onClose: @escaping () -> Void = {}
) {
self.history = history
self.isReceivingMessage = isReceivingMessage
self.onMessageSend = onMessageSend
self.onStop = onStop
self.onClear = onClear
self.onClose = onClose
}
public static func == (lhs: ChatRoom, rhs: ChatRoom) -> Bool {
lhs.history == rhs.history && lhs.isReceivingMessage == rhs.isReceivingMessage
}
public func send(_ message: String) { onMessageSend(message) }
public func stop() { onStop() }
public func clear() { onClear() }
public func close() { onClose() }
}
public struct ChatMessage: Equatable {
public var id: String
public var isUser: Bool
public var text: String
public init(id: String, isUser: Bool, text: String) {
self.id = id
self.isUser = isUser
self.text = text
}
}