Skip to content

Commit

Permalink
Add support for specifying a @State to control column visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdifran committed May 27, 2023
1 parent 12604a1 commit 29db798
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,28 @@ import AdaptiveTabView
@main
struct AdaptiveTabExampleApp: App {
@State private var selectedTab = iPhoneTabView.identifier
@State private var columnVisibility: NavigationSplitViewVisibility = .doubleColumn

var body: some Scene {
WindowGroup {
VStack {
Text(selectedTab.id)
AdaptiveTabView(
appName: "Example",
selectedTab: $selectedTab
) { (containerKind) in
MacOSTabView()
iPhoneTabView()
AppleWatchTabView()
if containerKind == .tabView {
FolderTabView()
}
} defaultDetail: {
ContentView(title: "Empty Details")
} sidebarExtraContent: {
SidebarView()
AdaptiveTabView(
appName: "Example",
selectedTab: $selectedTab,
columnVisibility: $columnVisibility
) { (containerKind) in
MacOSTabView()
iPhoneTabView()
AppleWatchTabView()
if containerKind == .tabView {
FolderTabView()
}
.selectedTabTransformer(transformer)
.navigationSplitViewStyle(.automatic)
} defaultDetail: {
ContentView(title: "Empty Details")
} sidebarExtraContent: {
SidebarView()
}
.selectedTabTransformer(transformer)
.navigationSplitViewStyle(.automatic)
}
}

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Here's an example of how it can be used:
@main
struct MyApp: App {
@State private var selectedTab = MyFirstTab.identifier
@State private var columnVisibility: NavigationSplitViewVisibility = .doubleColumn

var body: some Scene {
WindowGroup {
Expand Down
8 changes: 8 additions & 0 deletions Sources/AdaptiveTabView/AdaptiveTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ public struct AdaptiveTabView<TabContent: Sequence, SidebarExtraContent: View, D
@State private var selectedTabViewTab: TabIdentifier
@State private var selectedSidebarViewTab: TabIdentifier

private let columnVisibilityBinding: Binding<NavigationSplitViewVisibility>?
@State private var columnVisibilityState: NavigationSplitViewVisibility = .doubleColumn

@Environment(\.selectedTabTransformer) var selectedTabTransformer

/// Creates an ``AdaptiveTabView``.
/// - parameter appName: The name of the app. This appears as the navigation title of the sidebar when the kind
/// is ``AdaptiveTabViewContainerKind.sidebarView``.
/// - parameter selectedTab: The identifier of the selected tab in the currently dsisplayed mode.
/// - parameter splitViewKind: The type of split view to use.
/// - parameter columnVisibility: An optional ``Binding`` to the current column visibility of the split view when the kind
/// is ``AdaptiveTabViewContainerKind.sidebarView``.
/// - parameter tabViews: A view builder to provide the views for the tabs. In ``AdaptiveTabViewContainerKind.tabView``, they appear as
/// tabs within a ``NavigationView``. In ``AdaptiveTabViewContainerKind.sidebarView``, they appear at the top of the sidebar. You can
/// use the ``AdaptiveTabViewContainerKind`` to conditionally show tab views.
Expand All @@ -65,6 +70,7 @@ public struct AdaptiveTabView<TabContent: Sequence, SidebarExtraContent: View, D
appName: String,
selectedTab: Binding<TabIdentifier>,
splitViewKind: AdaptiveTabViewSplitViewKind = .threeColumn,
columnVisibility: Binding<NavigationSplitViewVisibility>? = nil,
@SequenceBuilder tabViews: @escaping (AdaptiveTabViewContainerKind) -> TabContent,
@ViewBuilder defaultContent: @escaping () -> DefaultContentView = { EmptyView() },
@ViewBuilder defaultDetail: @escaping () -> DefaultDetailView,
Expand All @@ -73,6 +79,7 @@ public struct AdaptiveTabView<TabContent: Sequence, SidebarExtraContent: View, D
self.appName = appName
self._selectedTab = selectedTab
self.splitViewKind = splitViewKind
self.columnVisibilityBinding = columnVisibility
self.tabViewBuilder = tabViews
self.defaultContentBuilder = defaultContent
self.defaultDetailBuilder = defaultDetail
Expand All @@ -97,6 +104,7 @@ public struct AdaptiveTabView<TabContent: Sequence, SidebarExtraContent: View, D
appName,
selectedTab: $selectedSidebarViewTab,
splitViewKind: splitViewKind,
columnVisibility: columnVisibilityBinding ?? $columnVisibilityState,
tabViewBuilder: tabViewBuilder,
defaultContentBuilder: defaultContentBuilder,
defaultDetailBuilder: defaultDetailBuilder,
Expand Down
21 changes: 17 additions & 4 deletions Sources/AdaptiveTabView/SidebarLayout/SidebarLayoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct SidebarLayoutView<TabContent: Sequence, SidebarExtraContent: View, Defaul
private let appName: String
private let selectedTab: Binding<TabIdentifier>?
private let splitViewKind: AdaptiveTabViewSplitViewKind
private let columnVisibility: Binding<NavigationSplitViewVisibility>
private let tabViewBuilder: (AdaptiveTabViewContainerKind) -> TabContent
private let defaultContentView: DefaultContentView
private let defaultDetailView: DefaultDetailView
Expand All @@ -22,6 +23,7 @@ struct SidebarLayoutView<TabContent: Sequence, SidebarExtraContent: View, Defaul
_ appName: String,
selectedTab: Binding<TabIdentifier>?,
splitViewKind: AdaptiveTabViewSplitViewKind,
columnVisibility: Binding<NavigationSplitViewVisibility>,
@SequenceBuilder tabViewBuilder: @escaping (AdaptiveTabViewContainerKind) -> TabContent,
@ViewBuilder defaultContentBuilder: () -> DefaultContentView,
@ViewBuilder defaultDetailBuilder: () -> DefaultDetailView,
Expand All @@ -30,6 +32,7 @@ struct SidebarLayoutView<TabContent: Sequence, SidebarExtraContent: View, Defaul
self.appName = appName
self.selectedTab = selectedTab
self.splitViewKind = splitViewKind
self.columnVisibility = columnVisibility
self.tabViewBuilder = tabViewBuilder
self.defaultContentView = defaultContentBuilder()
self.defaultDetailView = defaultDetailBuilder()
Expand All @@ -39,7 +42,7 @@ struct SidebarLayoutView<TabContent: Sequence, SidebarExtraContent: View, Defaul
var body: some View {
switch splitViewKind {
case .twoColumn:
NavigationSplitView {
NavigationSplitView(columnVisibility: columnVisibility) {
SidebarView(
appName,
selectedTab: selectedTab,
Expand All @@ -50,7 +53,7 @@ struct SidebarLayoutView<TabContent: Sequence, SidebarExtraContent: View, Defaul
defaultDetailView
}
case .threeColumn:
NavigationSplitView {
NavigationSplitView(columnVisibility: columnVisibility) {
SidebarView(
appName,
selectedTab: selectedTab,
Expand All @@ -68,7 +71,12 @@ struct SidebarLayoutView<TabContent: Sequence, SidebarExtraContent: View, Defaul

struct SidebarLayoutView_Previews: PreviewProvider {
static var previews: some View {
SidebarLayoutView("AdaptiveTabView", selectedTab: nil, splitViewKind: .threeColumn) { (_) in
SidebarLayoutView(
"AdaptiveTabView",
selectedTab: nil,
splitViewKind: .threeColumn,
columnVisibility: .constant(.doubleColumn)
) { (_) in
PreviewTitleImageProvidingView()
} defaultContentBuilder: {
Text("Content")
Expand All @@ -78,7 +86,12 @@ struct SidebarLayoutView_Previews: PreviewProvider {
Text("Hello World")
}

SidebarLayoutView("AdaptiveTabView", selectedTab: nil, splitViewKind: .twoColumn) { (_) in
SidebarLayoutView(
"AdaptiveTabView",
selectedTab: nil,
splitViewKind: .twoColumn,
columnVisibility: .constant(.doubleColumn)
) { (_) in
PreviewTitleImageProvidingView()
} defaultContentBuilder: {
Text("Content")
Expand Down

0 comments on commit 29db798

Please sign in to comment.