Skip to content

Commit

Permalink
Add parameter for the default selected tab
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdifran committed Feb 27, 2023
1 parent 6b4e0de commit aa46555
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Sources/AdaptiveTabView/AdaptiveTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum AdaptiveTabViewSplitViewKind {
public struct AdaptiveTabView<TabContent: Sequence, SidebarExtraContent: View, DefaultContentView: View, DefaultDetailView: View>: View where TabContent.Element: TabContentView {

private let appName: String
private let defaultSelectedTab: UInt
private let splitViewKind: AdaptiveTabViewSplitViewKind
private let tabViewBuilder: (AdaptiveTabViewContainerKind) -> TabContent
private let defaultContentBuilder: () -> DefaultContentView
Expand All @@ -44,6 +45,7 @@ public struct AdaptiveTabView<TabContent: Sequence, SidebarExtraContent: View, D
/// Creates an ``AdaptiveTabView``.
/// - parameter appName: The name of the app. This appears as the navigation title of the sidebar when the kind
/// is ``AdaptiveTabViewContainerKind.sidebarKind``.
/// - parameter defaultSelectedTab: Which tab is selected by default when in ``AdaptiveTabViewContainerKind.tabView``.
/// - parameter splitViewKind: The type of split view to use.
/// - 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
Expand All @@ -56,13 +58,15 @@ public struct AdaptiveTabView<TabContent: Sequence, SidebarExtraContent: View, D
/// is ``AdaptiveTabViewContainerKind.sidebarView``.
public init(
appName: String,
defaultSelectedTab: UInt = 0,
splitViewKind: AdaptiveTabViewSplitViewKind = .threeColumn,
@SequenceBuilder tabViews: @escaping (AdaptiveTabViewContainerKind) -> TabContent,
@ViewBuilder defaultContent: @escaping () -> DefaultContentView = { EmptyView() },
@ViewBuilder defaultDetail: @escaping () -> DefaultDetailView,
@ViewBuilder sidebarExtraContent: @escaping () -> SidebarExtraContent = { EmptyView() }
) {
self.appName = appName
self.defaultSelectedTab = defaultSelectedTab
self.splitViewKind = splitViewKind
self.tabViewBuilder = tabViews
self.defaultContentBuilder = defaultContent
Expand All @@ -77,6 +81,7 @@ public struct AdaptiveTabView<TabContent: Sequence, SidebarExtraContent: View, D
switch horizontalSizeClass {
case .compact:
TabLayoutView(
defaultSelectedTab: defaultSelectedTab,
tabViewBuilder
)
default:
Expand Down
9 changes: 7 additions & 2 deletions Sources/AdaptiveTabView/TabLayout/TabLayoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,33 @@ import SequenceBuilder

struct TabLayoutView<TabContent: Sequence>: View where TabContent.Element: TabContentView {

@State private var selectedTab: UInt

private let tabViews: TabContent

init(
defaultSelectedTab: UInt,
@SequenceBuilder _ tabViewBuilder: (AdaptiveTabViewContainerKind) -> TabContent
) {
self._selectedTab = State(initialValue: defaultSelectedTab)
self.tabViews = tabViewBuilder(.tabView)
}

var body: some View {
TabView {
TabView(selection: $selectedTab) {
ForEach(sequence: tabViews) { (index, tabView) in
TabNavigationView {
tabView
}
.tag(index)
}
}
}
}

struct TabLayoutView_Previews: PreviewProvider {
static var previews: some View {
TabLayoutView { (_) in
TabLayoutView(defaultSelectedTab: 0) { (_) in
PreviewTitleImageProvidingView()
}
}
Expand Down

0 comments on commit aa46555

Please sign in to comment.