|
| 1 | +import ComposableArchitecture |
1 | 2 | import Foundation |
2 | 3 | import SwiftUI |
3 | 4 |
|
| 5 | +public struct ChatTabInfo: Identifiable, Equatable { |
| 6 | + public var id: String |
| 7 | + public var title: String |
| 8 | + |
| 9 | + public init(id: String, title: String) { |
| 10 | + self.id = id |
| 11 | + self.title = title |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +public struct ChatTabInfoPreferenceKey: PreferenceKey { |
| 16 | + public static var defaultValue: [ChatTabInfo] = [] |
| 17 | + public static func reduce(value: inout [ChatTabInfo], nextValue: () -> [ChatTabInfo]) { |
| 18 | + value.append(contentsOf: nextValue()) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// Every chat tab should conform to this type. |
| 23 | +public typealias ChatTab = BaseChatTab & ChatTabType |
| 24 | + |
4 | 25 | open class BaseChatTab: Equatable { |
5 | | - public let id: UUID |
6 | | - |
7 | | - public static func == (lhs: BaseChatTab, rhs: BaseChatTab) -> Bool { |
8 | | - lhs.id == rhs.id |
| 26 | + final class InfoObservable: ObservableObject { |
| 27 | + @Published var id: String |
| 28 | + @Published var title: String |
| 29 | + init(id: String, title: String) { |
| 30 | + self.title = title |
| 31 | + self.id = id |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + struct ContentView: View { |
| 36 | + @ObservedObject var info: InfoObservable |
| 37 | + var buildView: () -> any View |
| 38 | + var body: some View { |
| 39 | + AnyView(buildView()) |
| 40 | + .preference( |
| 41 | + key: ChatTabInfoPreferenceKey.self, |
| 42 | + value: [ChatTabInfo( |
| 43 | + id: info.id, |
| 44 | + title: info.title |
| 45 | + )] |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public let id: String |
| 51 | + public var title: String { |
| 52 | + didSet { info.title = title } |
9 | 53 | } |
10 | | - |
11 | | - init(id: UUID) { |
| 54 | + |
| 55 | + let info: InfoObservable |
| 56 | + |
| 57 | + public init(id: String, title: String) { |
12 | 58 | self.id = id |
| 59 | + self.title = title |
| 60 | + info = InfoObservable(id: id, title: title) |
13 | 61 | } |
14 | | - |
| 62 | + |
15 | 63 | @ViewBuilder |
16 | 64 | public var body: some View { |
17 | 65 | if let tab = self as? ChatTabType { |
18 | | - AnyView(tab.buildView()).id(id) |
| 66 | + ContentView(info: info, buildView: tab.buildView).id(info.id) |
19 | 67 | } else { |
20 | | - EmptyView() |
| 68 | + EmptyView().id(info.id) |
21 | 69 | } |
22 | 70 | } |
| 71 | + |
| 72 | + public static func == (lhs: BaseChatTab, rhs: BaseChatTab) -> Bool { |
| 73 | + lhs.id == rhs.id |
| 74 | + } |
23 | 75 | } |
24 | 76 |
|
25 | 77 | public protocol ChatTabType { |
26 | 78 | @ViewBuilder |
27 | 79 | func buildView() -> any View |
28 | 80 | } |
29 | 81 |
|
30 | | -public typealias ChatTab = BaseChatTab & ChatTabType |
31 | | - |
32 | | -public class ChatGPTChatTab: ChatTab { |
33 | | - public var provider: ChatProvider |
34 | | - |
| 82 | +public class EmptyChatTab: ChatTab { |
35 | 83 | public func buildView() -> any View { |
36 | | - ChatPanel(chat: provider) |
37 | | - } |
38 | | - |
39 | | - public init(provider: ChatProvider) { |
40 | | - self.provider = provider |
41 | | - super.init(id: provider.id) |
| 84 | + VStack { |
| 85 | + Text("Empty-\(id)") |
| 86 | + } |
| 87 | + .background(Color.blue) |
42 | 88 | } |
43 | | -} |
44 | 89 |
|
45 | | -public class EmptyChatTab: ChatTab { |
46 | | - public func buildView() -> any View { |
47 | | - EmptyView() |
| 90 | + public init(id: String = UUID().uuidString) { |
| 91 | + super.init(id: id, title: "Empty") |
48 | 92 | } |
49 | 93 | } |
50 | 94 |
|
0 commit comments