Skip to content

Commit 3f1cbc6

Browse files
mattevigoMatteo
and
Matteo
committed
Suggestions from #37
Co-authored-by: Matteo <matteo@biobeats.com>
1 parent 9216af2 commit 3f1cbc6

File tree

2 files changed

+44
-62
lines changed

2 files changed

+44
-62
lines changed

README.md

Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -85,63 +85,6 @@ NavigationStackView(transitionType: .custom(AnyTransition.scale.animation(.sprin
8585

8686
attaching the easing directly to the transition. **Don't do this**. SwiftUI has still some problems with implicit animations attached to transitions, so it may not work. For example, implicit animations attached to a .slide transition won't work.
8787

88-
## NavigationStack injection
89-
90-
If you want to use the NavigationStack also outside the views hierachy you need to create you custom NavigationStack and pass it as parameter to the NavigationStackView.
91-
92-
This is useful when you want to _decouple your routing logic from views by using your own router class_, for example:
93-
94-
```
95-
class MyRouter {
96-
let navStack: NavigationStack
97-
98-
init(navStack: NavigationStack) {
99-
self.navStack = navStack
100-
}
101-
102-
func rootView() {
103-
if userIsLoggedIn() {
104-
return HomeScreen()
105-
} else {
106-
return LoginScreen()
107-
}
108-
}
109-
110-
func toLogin() {
111-
self.navStack.push(LoginScreen())
112-
}
113-
114-
//...
115-
}
116-
117-
struct ContentView: View {
118-
let navStack: NavigationStack
119-
let router: MyRouter
120-
121-
var body: some View {
122-
NavigationStackView(navigationStack: navStack) {
123-
router.rootView()
124-
}
125-
}
126-
}
127-
```
128-
129-
Setup the ContentView in your SceneDelegate (or similarly in the App class)
130-
131-
```
132-
// Create the SwiftUI view that provides the window contents.
133-
let contentView = ContentView(navStack: Injector.shared.navStack,
134-
router: Injector.shared.router)
135-
136-
// Use a UIHostingController as window root view controller.
137-
if let windowScene = scene as? UIWindowScene {
138-
let window = UIWindow(windowScene: windowScene)
139-
window.rootViewController = UIHostingController(rootView: contentView)
140-
self.window = window
141-
window.makeKeyAndVisible()
142-
}
143-
```
144-
14588
## Push
14689

14790
In order to navigate forward you have two options:
@@ -335,6 +278,47 @@ struct ChildView: View {
335278
}
336279
```
337280

281+
## NavigationStack injection
282+
283+
By default you can programmatically push and pop only inside the `NavigationStackView` hierarchy (by accessing the `NavigationStack` environment object). If you want to use the `NavigationStack` outside the `NavigationStackView` you need to create your own `NavigationStack` (wherever you want) **and pass it as parameter to the `NavigationStackView`**.
284+
285+
**Important:** Every `NavigationStack` must be associated to a `NavigationStackView`. A `NavigationStack` cannot be shared between multiple `NavigationStackView`.
286+
287+
This is useful when you want to _decouple your routing logic from views by using your own router class_, for example:
288+
289+
```
290+
class MyRouter {
291+
private let navStack: NavigationStack
292+
293+
init(navStack: NavigationStack) {
294+
self.navStack = navStack
295+
}
296+
297+
func rootView() -> some View {
298+
RootView()
299+
}
300+
301+
func toLogin() {
302+
self.navStack.push(LoginScreen())
303+
}
304+
305+
func toSignUp() {
306+
self.navStack.push(SignUpScreen())
307+
}
308+
}
309+
310+
struct RootView: View {
311+
let navStack: NavigationStack
312+
let router: MyRouter
313+
314+
var body: some View {
315+
NavigationStackView(navigationStack: navStack) {
316+
router.rootView()
317+
}
318+
}
319+
}
320+
```
321+
338322
## Important
339323

340324
Please, note that `NavigationStackView` navigates between views and two views may be smaller than the entire screen. In that case the transition animation won't involve the whole screen, but just the two views. Let's make an example:
@@ -468,5 +452,3 @@ struct MyView: View {
468452

469453
SwiftUI is really new, there are some bugs in the framework (or unexpected behaviours) and several API not yet documented. Please, report any issue may arise and feel free to suggest any improvement or changing to this first implementation of a navigation stack.
470454

471-
472-

Sources/NavigationStack/NavigationStack.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public enum PopDestination {
4747
public class NavigationStack: ObservableObject {
4848

4949
/// Default transition animation
50-
public static let defaultAnimation = Animation.easeOut(duration: 0.2)
50+
public static let defaultEasing = Animation.easeOut(duration: 0.2)
5151

5252
fileprivate private(set) var navigationType = NavigationType.push
5353

5454
/// Customizable easing to apply in pop and push transitions
5555
private let easing: Animation
5656

57-
public init(easing: Animation = defaultAnimation) {
57+
public init(easing: Animation = defaultEasing) {
5858
self.easing = easing
5959
}
6060

@@ -158,7 +158,7 @@ public struct NavigationStackView<Root>: View where Root: View {
158158
/// - transitionType: The type of transition to apply between views in every push and pop operation.
159159
/// - easing: The easing function to apply to every push and pop operation.
160160
/// - rootView: The very first view in the NavigationStack.
161-
public init(transitionType: NavigationTransition = .default, easing: Animation = NavigationStack.defaultAnimation, @ViewBuilder rootView: () -> Root) {
161+
public init(transitionType: NavigationTransition = .default, easing: Animation = NavigationStack.defaultEasing, @ViewBuilder rootView: () -> Root) {
162162
self.init(transitionType: transitionType, navigationStack: NavigationStack(easing: easing), rootView: rootView)
163163
}
164164

0 commit comments

Comments
 (0)