forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabContainer.swift
More file actions
82 lines (73 loc) · 2.17 KB
/
TabContainer.swift
File metadata and controls
82 lines (73 loc) · 2.17 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
import Dependencies
import Foundation
import SwiftUI
public final class ExternalTabContainer {
public static var tabContainers = [String: ExternalTabContainer]()
public struct TabItem: Identifiable {
public var id: String
public var title: String
public var description: String
public var image: String
public let viewBuilder: () -> AnyView
public init<V: View>(
id: String,
title: String,
description: String = "",
image: String = "",
@ViewBuilder viewBuilder: @escaping () -> V
) {
self.id = id
self.title = title
self.description = description
self.image = image
self.viewBuilder = { AnyView(viewBuilder()) }
}
}
public var tabs: [TabItem] = []
public init() { tabs = [] }
public static func tabContainer(for id: String) -> ExternalTabContainer {
if let tabContainer = tabContainers[id] {
return tabContainer
}
let tabContainer = ExternalTabContainer()
tabContainers[id] = tabContainer
return tabContainer
}
@ViewBuilder
public func tabView(for id: String) -> some View {
if let tab = tabs.first(where: { $0.id == id }) {
tab.viewBuilder()
}
}
public func registerTab<V: View>(
id: String,
title: String,
description: String = "",
image: String = "",
@ViewBuilder viewBuilder: @escaping () -> V
) {
tabs.append(TabItem(
id: id,
title: title,
description: description,
image: image,
viewBuilder: viewBuilder
))
}
public static func registerTab<V: View>(
for tabContainerId: String,
id: String,
title: String,
description: String = "",
image: String = "",
@ViewBuilder viewBuilder: @escaping () -> V
) {
tabContainer(for: tabContainerId).registerTab(
id: id,
title: title,
description: description,
image: image,
viewBuilder: viewBuilder
)
}
}