-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathChatTab.swift
More file actions
303 lines (264 loc) · 8.85 KB
/
ChatTab.swift
File metadata and controls
303 lines (264 loc) · 8.85 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import ComposableArchitecture
import Foundation
import SwiftUI
/// Preview info used in ChatHistoryView
public struct ChatTabPreviewInfo: Identifiable, Equatable, Codable {
public let id: String
public let title: String?
public let isSelected: Bool
public let updatedAt: Date
public init(id: String, title: String?, isSelected: Bool, updatedAt: Date) {
self.id = id
self.title = title
self.isSelected = isSelected
self.updatedAt = updatedAt
}
}
/// The information of a tab.
@ObservableState
public struct ChatTabInfo: Identifiable, Equatable, Codable {
public var id: String
public var title: String? = nil
public var isTitleSet: Bool {
if let title = title, !title.isEmpty { return true }
return false
}
public var focusTrigger: Int = 0
public var isSelected: Bool
public var CLSConversationID: String?
public var createdAt: Date
// used in chat history view
// should be updated when chat tab info changed or chat message of it changed
public var updatedAt: Date
// The `workspacePath` and `username` won't be save into database
private(set) public var workspacePath: String
private(set) public var username: String
public init(id: String, title: String? = nil, isSelected: Bool = false, CLSConversationID: String? = nil, workspacePath: String, username: String) {
self.id = id
self.title = title
self.isSelected = isSelected
self.CLSConversationID = CLSConversationID
self.workspacePath = workspacePath
self.username = username
let now = Date.now
self.createdAt = now
self.updatedAt = now
}
// for restoring
public init(id: String, title: String? = nil, focusTrigger: Int = 0, isSelected: Bool, CLSConversationID: String? = nil, createdAt: Date, updatedAt: Date, workspacePath: String, username: String) {
self.id = id
self.title = title
self.focusTrigger = focusTrigger
self.isSelected = isSelected
self.CLSConversationID = CLSConversationID
self.createdAt = createdAt
self.updatedAt = updatedAt
self.workspacePath = workspacePath
self.username = username
}
}
/// Every chat tab should conform to this type.
public typealias ChatTab = BaseChatTab & ChatTabType
/// Defines a bunch of things a chat tab should implement.
public protocol ChatTabType {
/// The type of the external dependency required by this chat tab.
associatedtype ExternalDependency
/// Build the view for this chat tab.
@ViewBuilder
func buildView() -> any View
/// Build the tabItem for this chat tab.
@ViewBuilder
func buildTabItem() -> any View
/// Build the chatConversationItem
@ViewBuilder
func buildChatConversationItem() -> any View
/// Build the icon for this chat tab.
@ViewBuilder
func buildIcon() -> any View
/// Build the menu for this chat tab.
@ViewBuilder
func buildMenu() -> any View
/// The name of this chat tab type.
static var name: String { get }
/// Available builders for this chat tab.
/// It's used to generate a list of tab types for user to create.
static func chatBuilders(externalDependency: ExternalDependency) -> [ChatTabBuilder]
/// Restorable state
func restorableState() async -> Data
/// Restore state
static func restore(
from data: Data,
externalDependency: ExternalDependency
) async throws -> any ChatTabBuilder
/// Whenever the body or menu is accessed, this method will be called.
/// It will be called only once so long as you don't call it yourself.
/// It will be called from MainActor.
func start()
}
/// The base class for all chat tabs.
open class BaseChatTab {
/// A wrapper to support dynamic update of title in view.
struct ContentView: View {
var buildView: () -> any View
var body: some View {
AnyView(buildView())
}
}
public var id: String = ""
public var title: String = ""
/// The store for chat tab info. You should only access it after `start` is called.
public let chatTabStore: StoreOf<ChatTabItem>
private var didStart = false
private let storeObserver = NSObject()
public init(store: StoreOf<ChatTabItem>) {
chatTabStore = store
storeObserver.observe { [weak self] in
guard let self else { return }
self.title = store.title ?? ""
self.id = store.id
}
}
/// The view for this chat tab.
@ViewBuilder
public var body: some View {
let id = "ChatTabBody\(id)"
if let tab = self as? (any ChatTabType) {
ContentView(buildView: tab.buildView).id(id)
.onAppear {
Task { @MainActor in self.startIfNotStarted() }
}
} else {
EmptyView().id(id)
}
}
/// The tab item for this chat tab.
@ViewBuilder
public var tabItem: some View {
let id = "ChatTabTab\(id)"
if let tab = self as? (any ChatTabType) {
ContentView(buildView: tab.buildTabItem).id(id)
.onAppear {
Task { @MainActor in self.startIfNotStarted() }
}
} else {
EmptyView().id(id)
}
}
@ViewBuilder
public var chatConversationItem: some View {
let id = "ChatTabTab\(id)"
if let tab = self as? (any ChatTabType) {
ContentView(buildView: tab.buildChatConversationItem).id(id)
} else {
EmptyView().id(id)
}
}
/// The icon for this chat tab.
@ViewBuilder
public var icon: some View {
let id = "ChatTabIcon\(id)"
if let tab = self as? (any ChatTabType) {
ContentView(buildView: tab.buildIcon).id(id)
} else {
EmptyView().id(id)
}
}
/// The tab item for this chat tab.
@ViewBuilder
public var menu: some View {
let id = "ChatTabMenu\(id)"
if let tab = self as? (any ChatTabType) {
ContentView(buildView: tab.buildMenu).id(id)
.onAppear {
Task { @MainActor in self.startIfNotStarted() }
}
} else {
EmptyView().id(id)
}
}
@MainActor
func startIfNotStarted() {
guard !didStart else { return }
didStart = true
if let tab = self as? (any ChatTabType) {
tab.start()
}
}
}
/// A factory of a chat tab.
public protocol ChatTabBuilder {
/// A visible title for user.
var title: String { get }
/// Build the chat tab.
func build(store: StoreOf<ChatTabItem>) async -> (any ChatTab)?
}
/// A chat tab builder that doesn't build.
public struct DisabledChatTabBuilder: ChatTabBuilder {
public var title: String
public func build(store: StoreOf<ChatTabItem>) async -> (any ChatTab)? {
return nil
}
public init(title: String) {
self.title = title
}
}
public extension ChatTabType {
/// The name of this chat tab type.
var name: String { Self.name }
}
public extension ChatTabType where ExternalDependency == Void {
/// Available builders for this chat tab.
/// It's used to generate a list of tab types for user to create.
static func chatBuilders() -> [ChatTabBuilder] {
chatBuilders(externalDependency: ())
}
}
/// A chat tab that does nothing.
public class EmptyChatTab: ChatTab {
public static var name: String { "Empty" }
struct Builder: ChatTabBuilder {
let title: String
func build(store: StoreOf<ChatTabItem>) async -> (any ChatTab)? {
EmptyChatTab(store: store)
}
}
public static func chatBuilders(externalDependency: Void) -> [ChatTabBuilder] {
[Builder(title: "Empty")]
}
public func buildView() -> any View {
VStack {
Text("Empty-\(id)")
}
.background(Color.blue)
}
public func buildTabItem() -> any View {
Text("Empty-\(id)")
}
public func buildChatConversationItem() -> any View {
Text("Empty-\(id)")
}
public func buildIcon() -> any View {
Image(systemName: "square")
}
public func buildMenu() -> any View {
Text("Empty-\(id)")
}
public func restorableState() async -> Data {
return Data()
}
public static func restore(
from data: Data,
externalDependency: Void
) async throws -> any ChatTabBuilder {
return Builder(title: "Empty")
}
public convenience init(id: String) {
self.init(store: .init(
initialState: .init(id: id, title: "Empty-\(id)", workspacePath: "", username: ""),
reducer: { ChatTabItem() }
))
}
public func start() {
chatTabStore.send(.updateTitle("Empty-\(id)"))
}
}