-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTabBarView.swift
186 lines (171 loc) · 6.23 KB
/
TabBarView.swift
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// TabBarView.swift
// Cronica (iOS)
//
// Created by Alexandre Madeira on 01/05/22.
//
import SwiftUI
#if !os(macOS)
/// A TabBar for switching views, only used on iPhone.
struct TabBarView: View {
@AppStorage("lastTabSelected") private var tabSelection: Screens?
var persistence = PersistenceController.shared
private var selectedTab: Binding<Screens> {
return .init {
return tabSelection ?? .home
} set: { newValue in
if newValue == tabSelection {
switch newValue {
case .home:
if !homePath.isEmpty {
homePath = .init()
}
case .explore:
if !explorePath.isEmpty {
explorePath = .init()
}
case .watchlist:
if !watchlistPath.isEmpty {
watchlistPath = .init()
}
case .search:
if !searchPath.isEmpty {
searchPath = .init()
} else {
shouldOpenOnSearchField = true
}
default: return
}
}
tabSelection = newValue
}
}
@State private var homePath: NavigationPath = .init()
@State private var explorePath: NavigationPath = .init()
@State private var watchlistPath: NavigationPath = .init()
@State private var searchPath: NavigationPath = .init()
@State private var shouldOpenOnSearchField = false
var body: some View {
#if os(iOS)
if UIDevice.isIPad {
if #available(iOS 18, *) {
newTabView
.onAppear {
let settings = SettingsStore.shared
if settings.isPreferredLaunchScreenEnabled {
tabSelection = settings.preferredLaunchScreen
}
}
.appTint()
.appTheme()
} else {
details
.onAppear {
let settings = SettingsStore.shared
if settings.isPreferredLaunchScreenEnabled {
tabSelection = settings.preferredLaunchScreen
}
}
.appTint()
.appTheme()
}
} else {
details
.onAppear {
let settings = SettingsStore.shared
if settings.isPreferredLaunchScreenEnabled {
tabSelection = settings.preferredLaunchScreen
}
}
.appTint()
.appTheme()
}
#else
details
#endif
}
#if os(tvOS)
private var details: some View {
TabView {
NavigationStack { HomeView() }
.tag(HomeView.tag)
.tabItem { Label("Home", systemImage: "house").labelStyle(.titleOnly) }
.ignoresSafeArea(.all, edges: .horizontal)
NavigationStack { ExploreView() }
.tag(ExploreView.tag)
.tabItem { Label("Explore", systemImage: "popcorn").labelStyle(.titleOnly) }
NavigationStack {
WatchlistView()
.environment(\.managedObjectContext, persistence.container.viewContext)
}
.tabItem { Label("Watchlist", systemImage: "square.stack").labelStyle(.titleOnly) }
.tag(WatchlistView.tag)
NavigationStack { SearchView() }
.tabItem { Image(systemName: "magnifyingglass").accessibilityLabel("Search") }
SettingsView()
.tabItem { Image(systemName: "gearshape").accessibilityLabel("Settings") }
}
.ignoresSafeArea(.all, edges: .horizontal)
}
#endif
@available(iOS 18, *)
private var newTabView: some View {
TabView(selection: selectedTab) {
Tab("Home", systemImage: "house", value: .home) {
NavigationStack(path: $homePath) {
HomeView()
}
}
Tab("Discover", systemImage: "popcorn", value: .explore) {
NavigationStack(path: $explorePath) {
ExploreView()
}
}
Tab("Watchlist", systemImage: "rectangle.on.rectangle", value: .watchlist) {
NavigationStack(path: $watchlistPath) {
WatchlistView()
.environment(\.managedObjectContext, persistence.container.viewContext)
}
}
Tab("Search", systemImage: "magnifyingglass", value: .search, role: .search) {
NavigationStack(path: $searchPath) {
SearchView(shouldFocusOnSearchField: $shouldOpenOnSearchField)
}
}
}
.tabViewStyle(.sidebarAdaptable)
.appTheme()
}
#if os(iOS) || os(visionOS)
private var details: some View {
TabView(selection: selectedTab) {
NavigationStack(path: $homePath) {
HomeView()
}
.tag(Screens.home)
.tabItem { Label("Home", systemImage: "house") }
NavigationStack(path: $explorePath) {
ExploreView()
}
.tag(Screens.explore)
.tabItem { Label("Discover", systemImage: "popcorn") }
NavigationStack(path: $watchlistPath) {
WatchlistView()
.environment(\.managedObjectContext, persistence.container.viewContext)
}
.tabItem { Label("Watchlist", systemImage: "rectangle.on.rectangle") }
.tag(Screens.watchlist)
NavigationStack(path: $searchPath) {
SearchView(shouldFocusOnSearchField: $shouldOpenOnSearchField)
}
.tag(Screens.search)
.tabItem { Label("Search", systemImage: "magnifyingglass") }
}
.appTheme()
}
#endif
}
#Preview {
TabBarView()
}
#endif