How to migrate to 0.6.0 with dynamic routes? #83
-
I'm trying to upgrade to the new FlowStacks API in 0.6.0. But having some trouble with this previous code snippet: As you can see in the How can I achieve this? enum RootScreen {
case chooseServerView
case login(URL, GetSiteResponse)
case signup(URL, GetSiteResponse)
case main
}
struct RootView: View {
@EnvironmentObject private var secureStorage: SecureStorage
@State var routes: Routes<RootScreen> = []
var body: some View {
Router($routes) { screen, _ in
switch screen {
case .chooseServerView:
ChooseServerView()
case .login(let url, let siteResponse):
LoginView(url: url, siteResponse: siteResponse)
case .signup(let url, let siteResponse):
SignUpView(url: url, siteResponse: siteResponse)
case .main:
MainView()
}
}
.onAppear {
updateRoutes(noUser: $secureStorage.accounts.isEmpty)
}
.onReceive($secureStorage.$accounts.wrappedValue) { accounts in
updateRoutes(noUser: accounts.isEmpty)
}
}
private func updateRoutes(noUser: Bool) {
if noUser {
routes = [.root(.chooseServerView, embedInNavigationView: true)]
} else {
routes = [.root(.main)]
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @fishcharlie ! Usually the best approach is to have a top-level view that either shows the logged out flow (with a FlowStack) or the logged in view (which might also have its own FlowStack), e.g.: struct RootView: View {
@EnvironmentObject private var secureStorage: SecureStorage
var body: some View {
if secureStorage.accounts.isEmpty {
LoggedOutFlow()
} else {
MainView()
}
}
}
enum LoggedOutScreen: Hashable {
case chooseServerView
case login(URL, GetSiteResponse)
case signup(URL, GetSiteResponse)
}
struct LoggedOutFlow: View {
@State var routes: Routes<RootScreen> = []
var body: some View {
FlowStack($routes, withNavigation: true) {
ChooseServerView()
.flowDestination(for: LoggedOutScreen.self) { screen in
switch screen {
case .chooseServerView:
ChooseServerView()
case let .login(url, siteResponse):
LoginView(url: url, siteResponse: siteResponse)
case let .signup(url, siteResponse):
SignUpView(url: url, siteResponse: siteResponse)
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Hi @fishcharlie !
Usually the best approach is to have a top-level view that either shows the logged out flow (with a FlowStack) or the logged in view (which might also have its own FlowStack), e.g.: