-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathChatTabPool.swift
More file actions
57 lines (45 loc) · 1.37 KB
/
ChatTabPool.swift
File metadata and controls
57 lines (45 loc) · 1.37 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
import ComposableArchitecture
import Dependencies
import Foundation
import SwiftUI
/// A pool that stores all the available tabs.
public final class ChatTabPool {
public var createStore: (ChatTabInfo) -> StoreOf<ChatTabItem> = { info in
.init(
initialState: info,
reducer: { ChatTabItem() }
)
}
private var pool: [String: any ChatTab]
public init(_ pool: [String: any ChatTab] = [:]) {
self.pool = pool
}
public func getTab(of id: String) -> (any ChatTab)? {
pool[id]
}
public func setTab(_ tab: any ChatTab) {
pool[tab.id] = tab
}
public func removeTab(of id: String) {
guard getTab(of: id) != nil else { return }
pool.removeValue(forKey: id)
}
}
public struct ChatTabPoolDependencyKey: DependencyKey {
public static let liveValue = ChatTabPool()
}
public extension DependencyValues {
var chatTabPool: ChatTabPool {
get { self[ChatTabPoolDependencyKey.self] }
set { self[ChatTabPoolDependencyKey.self] = newValue }
}
}
public struct ChatTabPoolEnvironmentKey: EnvironmentKey {
public static let defaultValue = ChatTabPool()
}
public extension EnvironmentValues {
var chatTabPool: ChatTabPool {
get { self[ChatTabPoolEnvironmentKey.self] }
set { self[ChatTabPoolEnvironmentKey.self] = newValue }
}
}