I'm writing a generic navigator for SwiftUI views
protocol FlowNavigator {
associatedtype FlowRoute
func navigate<T: View>(_ route: FlowRoute, content: () -> T) -> AnyView
}
to use it with an enum
representing current app flow
enum AppFlow {
case access, createAccount, login, loginAndSecurity
func getNavigator<T: FlowNavigator>() -> T? {
switch self {
case .access:
return nil
case .createAccount:
return CreateAccountNavigator() as? T
case .login:
return nil
case .loginAndSecurity:
return LoginAndSecurityNavigator() as? T
}
}
}
with FlowNavigator
implementation like this
class LoginAndSecurityNavigator: FlowNavigator {
func navigate<T: View>(_ route: LoginAndSecurityRoute, content: () -> T) -> AnyView {
switch route {
case .changePassword:
return NavigationLink(
destination: Text("CHANGE PASSWORD")) {
content()
}.toAnyView()
case .linkedSocials:
return NavigationLink(
destination: LinkedSocialsScreen()) {
content()
}.toAnyView()
}
}
}
where LoginAndSecurityRoute
is a basic enum
enum LoginAndSecurityRoute {
case changePassword
case linkedSocials
}
(CreateAccountNavigator
is basically the same, it differs only for navigationLinks)
Xcode doesn't complain with this code but when I try to use it with
let flow = AppState.AppFlow.loginAndSecurity
let flowNavigator: FlowNavigator? = flow.getNavigator()
compiler says
Protocol 'FlowNavigator' as a type cannot conform to the protocol
itself
I'm pretty sure solution is trivial but I cannot find a way to make it work...