Skip to content

Commit 33aece8

Browse files
authored
Merge pull request #14 from mpdifran/navigationStack
Add property to control whether to embed in a NavigationStack
2 parents 24d0ea2 + 1bee96b commit 33aece8

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

Sources/Views/SettingStack.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,50 @@ public struct SettingStack: View {
2525

2626
@StateObject var settingViewModel = SettingViewModel()
2727

28+
/**
29+
Whether the ``SettingStack`` should automatically embed in a ``NavigationStack`` / ``NavigationView``, or assume the view exists higher up
30+
in the hierarchy.
31+
*/
32+
private let embedInNavigationStack: Bool
33+
2834
/**
2935
Create a new Settings view from a `SettingPage`. The default "no results" view will be used.
36+
- parameters:
37+
- embedInNavigationStack: Whether to embed the Settings views in a ``NavigationStack`` / ``NavigationView`` or not. If this is `false`, you will be responsible for providing the ``NavigationStack`` / ``NavigationView``.
38+
- page:A closure to provide a ``SettingPage`` to the ``SettingStack``.
3039
*/
31-
public init(page: @escaping () -> SettingPage) {
40+
public init(
41+
embedInNavigationStack: Bool = true,
42+
page: @escaping () -> SettingPage
43+
) {
44+
self.embedInNavigationStack = embedInNavigationStack
3245
self.page = page
3346
}
3447

3548
/**
3649
Create a new Settings view from a `SettingPage`, with a custom `SettingViewModel` and custom "no results" view.
50+
- parameters:
51+
- settingViewModel: A custom view model to use for the ``SettingStack``.
52+
- embedInNavigationStack: Whether to embed the Settings views in a ``NavigationStack`` / ``NavigationView`` or not. If this is `false`, you will be responsible for providing the ``NavigationStack`` / ``NavigationView``.
53+
- page:A closure to provide a ``SettingPage`` to the ``SettingStack``.
54+
- customNoResultsView: A view builder to provide the view to use when there's no results.
3755
*/
3856
public init<Content>(
3957
settingViewModel: SettingViewModel,
58+
embedInNavigationStack: Bool = true,
4059
page: @escaping () -> SettingPage,
4160
@ViewBuilder customNoResultsView: @escaping () -> Content
4261
) where Content: View {
4362
self._settingViewModel = StateObject(wrappedValue: settingViewModel)
63+
self.embedInNavigationStack = embedInNavigationStack
4464
self.page = page
4565
self.customNoResultsView = AnyView(customNoResultsView())
4666
}
4767

4868
public var body: some View {
49-
if #available(iOS 16.0, macOS 13.0, *) {
69+
if !embedInNavigationStack {
70+
main
71+
} else if #available(iOS 16.0, macOS 13.0, *) {
5072
NavigationStack {
5173
main
5274
}

0 commit comments

Comments
 (0)