Skip to content

Commit 68a640d

Browse files
committed
Migrate ChatTab to latest TCA
1 parent 18a7020 commit 68a640d

3 files changed

Lines changed: 19 additions & 11 deletions

File tree

Tool/Sources/ChatTab/ChatTab.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Foundation
33
import SwiftUI
44

55
/// The information of a tab.
6+
@ObservableState
67
public struct ChatTabInfo: Identifiable, Equatable {
78
public var id: String
89
public var title: String
@@ -52,7 +53,7 @@ public protocol ChatTabType {
5253
}
5354

5455
/// The base class for all chat tabs.
55-
open class BaseChatTab {
56+
open class BaseChatTab: NSObject {
5657
/// A wrapper to support dynamic update of title in view.
5758
struct ContentView: View {
5859
var buildView: () -> any View
@@ -61,18 +62,23 @@ open class BaseChatTab {
6162
}
6263
}
6364

64-
public var id: String { chatTabViewStore.id }
65-
public var title: String { chatTabViewStore.title }
65+
public var id: String = ""
66+
public var title: String = ""
6667
/// The store for chat tab info. You should only access it after `start` is called.
6768
public let chatTabStore: StoreOf<ChatTabItem>
68-
/// The view store for chat tab info. You should only access it after `start` is called.
69-
public let chatTabViewStore: ViewStoreOf<ChatTabItem>
7069

7170
private var didStart = false
71+
private var storeObservation: ObservationToken?
7272

7373
public init(store: StoreOf<ChatTabItem>) {
7474
chatTabStore = store
75-
chatTabViewStore = ViewStore(store)
75+
super.init()
76+
77+
storeObservation = observe { [weak self] in
78+
guard let self else { return }
79+
self.title = store.title
80+
self.id = store.id
81+
}
7682
}
7783

7884
/// The view for this chat tab.
@@ -220,12 +226,12 @@ public class EmptyChatTab: ChatTab {
220226
public convenience init(id: String) {
221227
self.init(store: .init(
222228
initialState: .init(id: id, title: "Empty-\(id)"),
223-
reducer: ChatTabItem()
229+
reducer: { ChatTabItem() }
224230
))
225231
}
226232

227233
public func start() {
228-
chatTabViewStore.send(.updateTitle("Empty-\(id)"))
234+
chatTabStore.send(.updateTitle("Empty-\(id)"))
229235
}
230236
}
231237

Tool/Sources/ChatTab/ChatTabItem.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public struct AnyChatTabBuilder: Equatable {
1313
}
1414
}
1515

16-
public struct ChatTabItem: ReducerProtocol {
16+
@Reducer
17+
public struct ChatTabItem {
1718
public typealias State = ChatTabInfo
1819

1920
public enum Action: Equatable {
@@ -26,7 +27,7 @@ public struct ChatTabItem: ReducerProtocol {
2627

2728
public init() {}
2829

29-
public var body: some ReducerProtocol<State, Action> {
30+
public var body: some ReducerOf<Self> {
3031
Reduce { state, action in
3132
switch action {
3233
case let .updateTitle(title):

Tool/Sources/ChatTab/ChatTabPool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public final class ChatTabPool {
88
public var createStore: (String) -> StoreOf<ChatTabItem> = { id in
99
.init(
1010
initialState: .init(id: id, title: ""),
11-
reducer: ChatTabItem()
11+
reducer: { ChatTabItem() }
1212
)
1313
}
1414

@@ -52,3 +52,4 @@ public extension EnvironmentValues {
5252
set { self[ChatTabPoolEnvironmentKey.self] = newValue }
5353
}
5454
}
55+

0 commit comments

Comments
 (0)