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

Would be great to have a HelloWorld app #19

Open
hoopes opened this issue Dec 30, 2020 · 2 comments
Open

Would be great to have a HelloWorld app #19

hoopes opened this issue Dec 30, 2020 · 2 comments

Comments

@hoopes
Copy link

hoopes commented Dec 30, 2020

The Todo app seems nice, but to a beginner like me, there's a lot of ceremony.

I have a incredibly simple test app here, with really only the ContentView.swift really being relevant. Can you see something obvious I'm doing wrong? Somehow conflating/confusing the route vs waypoint concepts?

Please feel free to take what's there as a HelloWorld app (or make it even simpler, if you can/want). In any case, thanks for the library!

@StevenLambion
Copy link
Owner

That is a great idea. I'll see about getting an example app that focuses just on the navigation library. In the meantime, I just pushed a quick update to the todo app with the changes for SwiftDuxNavigation 2.0.

https://github.com/StevenLambion/SwiftUI-Todo-Example/tree/swiftdux-navigation

I'll try out the code ASAP, but taking a quick glance I don't see a Route view in the code. This view is used to declare a new "route" state within the SwiftDux store that manages the routing. You'll typically want it at the root of SwiftUI Scene such as a WindowGroup. Try removing the WaypointView from the ContentView and instead add a Route to the WindowGroup.

One other thing I want to point out is that the WaypointView is meant for use in custom navigational views. You don't need to use it for content views. This means that the OneView and TwoView shouldn't have a WaypointView themselves, instead, you might want to add a NavigationView to the ContentView. Then to add the OneView and TwoView as "stack items". The stackItem function below will internally wrap the OneView and TwoView inside a WaypointView for you.

I wrote this code on the fly, so it may have some errors, but hopefully helps as a reference. To make it recursive, you can add further stack items to the OneView and TwoView in the same way.

// RouteTestApp.swift

struct RouteTestApp: App {
    var body: some Scene {
        WindowGroup {
          Route(name: "main") { // Add the Route here.
            ContentView()
              .provideStore(configureStore())
          }
       }
    }
}

// ContentView.swift

struct ContentView: View {
  @Environment(\.actionDispatcher) private var dispatch
  @Environment(\.waypoint) private var waypoint

  var body: some View {
    NavigationView {
      List {
        Text("Hello, world!")
          .padding()

        Button(action: {
          dispatch(NavigationAction.navigate(to: URL(string: "/one")!))
        }) { Text("Button to one") }

        Button(action: {
          dispatch(NavigationAction.navigate(to: URL(string: "/two")!))
        }) { Text("Button to two") }

        RouteLink(path: "/one") { Text("ONE") }
        RouteLink(path: "/two") { Text("TWO") }
      }
       // These will create the Waypoint for you and will push their views onto the NavigationView.
      .stackItem(.name("one")) { OneView() }
      .stackItem(.name("two")) { TwoView() }
    }
  }
}

struct OneView: View {

  @Environment(\.actionDispatcher) private var dispatch
  @Environment(\.waypoint) private var waypoint

  var body: some View {
    Text("One!")
      .padding()
  }
}

struct TwoView: View {

  @Environment(\.actionDispatcher) private var dispatch
  @Environment(\.waypoint) private var waypoint

  var body: some View {
    Text("Two!")
      .padding()
  }
}

@hoopes
Copy link
Author

hoopes commented Jan 1, 2021

Awesome, thanks so much, I got the stackItem stuff working based on your advice above. I guess something that might help to call out more prominently in the docs is the concrete difference between a Route and a WayPoint. Coming from a web background, (for the most part) everything is a route, which renders a template, or sets some value via parameter. Does a Route need an entire Scene of its own?

So if i have two main sections (Home and Settings, let's say), are home and settings the Routes, and things "beneath" them are the WayPoint objects? Can a Route be within another Route? It looks like RouteLink is only used to navigate within the context of the current Route you're "in"?

Anyway, that's a lot of questions, and i apologize for my slow uptake. I'll look forward to the hello-world version :). I'm really excited about this library, because just like SwiftDux, it really helps make ios act like front-end web stuff (at least, in my mind). Thanks again for working on this!

(I added some probably embarrassing updates to https://github.com/hoopes/RouteTest/blob/main/RouteTest/ContentView.swift showing off my lack of knowledge...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants