Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Use NavigationSplitView rather than TabView for iPad #6 #279

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Basic-Car-Maintenance/Shared/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -3686,6 +3686,16 @@
}
}
},
"Sidebar" : {
"localizations" : {
"ru" : {
"stringUnit" : {
"state" : "translated",
"value" : "Боковая панель"
}
}
}
},
"Sign Out" : {
"localizations" : {
"be" : {
Expand Down Expand Up @@ -4301,6 +4311,16 @@
}
}
},
"Unexpected error occured." : {
"localizations" : {
"ru" : {
"stringUnit" : {
"state" : "translated",
"value" : "Произошла ошибка."
}
}
}
},
"Update" : {
"comment" : "Label for submit button on form to update an existing entry",
"localizations" : {
Expand Down
120 changes: 101 additions & 19 deletions Basic-Car-Maintenance/Shared/MainView/Views/MainTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,40 @@
import SwiftUI
import SwiftData

enum TabSelection: Int {
enum TabSelection: Int, Identifiable, CaseIterable {
var id: Self { self }

case dashboard = 0
case odometer = 1
case settings = 2
}

extension TabSelection {
// you can make TabSelection :String
// insteaf of :Int too and remove label
var label: String {
windrunner21 marked this conversation as resolved.
Show resolved Hide resolved
switch self {
case .dashboard:
return "Dashboard"
case .odometer:
return "Odometer"
case .settings:
return "Settings"
}
}

var image: String {
switch self {
case .dashboard:
return SFSymbol.dashboard
case .odometer:
return SFSymbol.gauge
case .settings:
return SFSymbol.gear
}
}
}

@MainActor
struct MainTabView: View {
@Query var acknowledgedAlerts: [AcknowledgedAlert]
Expand All @@ -24,28 +52,23 @@ struct MainTabView: View {

@AppStorage("lastTabOpen") var selectedTab = TabSelection.dashboard

@State private var selectedTabId: TabSelection.ID? = .dashboard
mikaelacaron marked this conversation as resolved.
Show resolved Hide resolved
@State private var columnVisibility = NavigationSplitViewVisibility.automatic

@State var authenticationViewModel = AuthenticationViewModel()
@State var viewModel = MainTabViewModel()

var body: some View {
TabView(selection: $selectedTab) {
DashboardView(userUID: authenticationViewModel.user?.uid)
.tag(TabSelection.dashboard)
.tabItem {
Label("Dashboard", systemImage: SFSymbol.dashboard)
}

OdometerView(userUID: authenticationViewModel.user?.uid)
.tag(TabSelection.odometer)
.tabItem {
Label("Odometer", systemImage: SFSymbol.gauge)
}

SettingsView(authenticationViewModel: authenticationViewModel)
.tag(TabSelection.settings)
.tabItem {
Label("Settings", systemImage: SFSymbol.gear)
}
Group {
#if os(iOS)
windrunner21 marked this conversation as resolved.
Show resolved Hide resolved
if UIDevice.current.userInterfaceIdiom == .pad {
navigationSplitView()
} else {
tabView()
}
#else
navigationSplitView()
#endif
}
.sheet(item: $viewModel.alert) { alert in
AlertView(alert: alert)
Expand Down Expand Up @@ -83,6 +106,65 @@ struct MainTabView: View {
}
}

extension MainTabView {
windrunner21 marked this conversation as resolved.
Show resolved Hide resolved
private func tabItem(for selection: TabSelection) -> some View {
Label(selection.label, systemImage: selection.image)
}
windrunner21 marked this conversation as resolved.
Show resolved Hide resolved

/// Save screen content for specific selection
/// - Parameter selection: tab selection enum value
@ViewBuilder
private func selectionContent(for selection: TabSelection) -> some View {
switch selection {
case .dashboard:
DashboardView(userUID: authenticationViewModel.user?.uid)
case .odometer:
OdometerView(userUID: authenticationViewModel.user?.uid)
case .settings:
SettingsView(authenticationViewModel: authenticationViewModel)
}
}

/// Returns last column of NavigationSplitView - detail content
@ViewBuilder
private func splitViewDetailContent() -> some View {
windrunner21 marked this conversation as resolved.
Show resolved Hide resolved
if let tabSelection = selectedTabId {
selectionContent(for: tabSelection)
.tag(tabSelection)
} else {
Text("Unexpected error occured.")
}
}

/// Returns NavigationSplitView navigation
/// primarily used on iPad and Mac devices
@ViewBuilder
private func navigationSplitView() -> some View {
windrunner21 marked this conversation as resolved.
Show resolved Hide resolved
NavigationSplitView(columnVisibility: $columnVisibility) {
List(TabSelection.allCases, selection: $selectedTabId) { tabSelection in
Label(tabSelection.label, systemImage: tabSelection.image)
}
.navigationTitle("Sidebar")
windrunner21 marked this conversation as resolved.
Show resolved Hide resolved
} detail: {
splitViewDetailContent()
}
}

/// Returns TabView navigation
/// primarily used on iPhone devices
@ViewBuilder func tabView() -> some View {
windrunner21 marked this conversation as resolved.
Show resolved Hide resolved
TabView(selection: $selectedTab) {
ForEach(TabSelection.allCases) { tabSelection in
selectionContent(for: tabSelection)
.tag(tabSelection)
.tabItem {
tabItem(for: tabSelection)
}
}
}
}
}

#Preview {
MainTabView()
.environment(ActionService.shared)
Expand Down
Loading