-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAppTabView.swift
More file actions
133 lines (123 loc) · 3.92 KB
/
Copy pathAppTabView.swift
File metadata and controls
133 lines (123 loc) · 3.92 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
//
// AppTabView.swift
// NativeAppTemplate
//
import SwiftUI
struct AppTabView<
ShopListView: View,
ScanView: View,
SettingsView: View
> {
@Environment(\.sessionController) private var sessionController
@Environment(DataManager.self) private var dataManager
@Environment(TabViewModel.self) private var model
@State var navigationPathShops = NavigationPath()
private let shopListView: () -> ShopListView
private let scanView: () -> ScanView
private let settingsView: () -> SettingsView
init(
shopListView: @escaping () -> ShopListView,
scanView: @escaping () -> ScanView,
settingsView: @escaping () -> SettingsView
) {
self.shopListView = shopListView
self.scanView = scanView
self.settingsView = settingsView
}
}
// MARK: - View
extension AppTabView: View {
var body: some View {
ScrollViewReader { proxy in
TabView(
selection: .init(
get: { model.selectedTab },
set: { selection in
switch model.selectedTab {
case selection:
withAnimation {
proxy.scrollTo(
ScrollToTopID(
mainTab: selection, detail: model.showingDetailView[selection]!
),
anchor: .top
)
}
default:
model.selectedTab = selection
}
}
)
) {
tab(
content: shopListView,
navigationPath: $navigationPathShops,
text: .shops,
imageName: "storefront.fill",
tab: .shops
)
tab(
content: scanView,
navigationPath: nil,
text: .scan,
imageName: "platter.filled.bottom.iphone",
tab: .scan
)
tab(
content: settingsView,
navigationPath: nil,
text: .settings,
imageName: "gearshape.fill",
tab: .settings
)
}
}
.tint(.accent)
.onChange(of: sessionController.client) {
navigationPathShops = NavigationPath()
}
.onChange(of: sessionController.shouldPopToRootView) {
if sessionController.shouldPopToRootView {
navigationPathShops = NavigationPath()
sessionController.shouldPopToRootView = false
}
}
}
}
struct AppTabView_Previews: PreviewProvider {
static var previews: some View {
AppTabView(
shopListView: { Text(verbatim: "SHOPS") },
scanView: { Text(verbatim: "SCAN") },
settingsView: { Text(verbatim: "SETTINGS") }
).environment(TabViewModel())
}
}
// MARK: - private
@MainActor private func tab(
content: @escaping () -> some View,
navigationPath: Binding<NavigationPath>?,
text: String,
imageName: String,
tab: MainTab
) -> some View {
if let navigationPath {
NavigationStack(path: navigationPath, root: content)
.tabItem {
Text(text)
Image(systemName: imageName)
}
.tag(tab)
.environment(\.mainTab, tab)
.accessibility(label: .init(text))
} else {
NavigationStack(root: content)
.tabItem {
Text(text)
Image(systemName: imageName)
}
.tag(tab)
.environment(\.mainTab, tab)
.accessibility(label: .init(text))
}
}