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

Problem with .push transition. #106

Closed
amineahmmouch opened this issue Nov 3, 2022 · 2 comments
Closed

Problem with .push transition. #106

amineahmmouch opened this issue Nov 3, 2022 · 2 comments

Comments

@amineahmmouch
Copy link

amineahmmouch commented Nov 3, 2022

Hi, i just discover Stinsen and i think it's the best implemetation of Coordinator pattern in SwiftUI. Congratulations.

Here is my problem,

I have two views SignInView and ForgotPasswordView, and i want to perform push from SignInView to ForgetPassword, but its doesn't perform push animation neither it doesn't switch to the second view (ForgetPasswordView).
Note that .modal and .fullscreen transitions work the problem occur just for .push.

Did i implement the flow wrong ?

Thanks in advance.

Here is my code :

struct SignInView: View {
    var body: some View {
            ZStack(alignment: .bottom) {
                VStack {
                    ...
                        
                        Text("Forget password")
                            .foregroundColor(Color.white.opacity(0.7))
                            .accessibilityIdentifier("SIGNIN_FORGET_PASSWORD_BUTTON")
                            .onTapGesture {
                                signInRouter.route(to: \.forgetPassword)
                            }
                            
                            .....
                }
          }
    }
}
protocol SignInCoordinator: AnyObject {
    func pushToForgetPassword()
}
final class DefaultSignInCoordinator: SignInCoordinator, NavigationCoordinatable {
    var stack: NavigationStack<DefaultSignInCoordinator> = NavigationStack(initial: \.start)
 
    @Root var start = makeStart
    @Route(.push) var forgetPassword = makeForgetPassword

    lazy var routerStorable: SignInCoordinator = self
        
    func makeStart() -> some View {
        return SignInView()
    }
    
    func makeForgetPassword() ->  DefaultForgetPasswordCoordinator {
        return DefaultForgetPasswordCoordinator()
    }

    func pushToForgetPassword() {
        self.route(to: \.forgetPassword)
    }
}
struct ForgetPasswordFormView: View {
       
    @EnvironmentObject private var signInRouter: DefaultSignInCoordinator.Router

    var body: some View {
            GeometryReader { _ in
                ZStack(alignment: .top) {
                    HStack {
                        Button(action: {
                            signInRouter.popToRoot()
                        }) {
                            Image(systemName: "arrowshape.turn.up.left.fill")
                                .resizable()
                                .frame(width: 40, height: 40)
                                .foregroundColor(.white)
                                .padding()
                        }
                        Spacer()
                    }
                    
                    .......

            }
    }
}
protocol ForgetPasswordCoordinator: AnyObject {
    func popToSignIn()
}
final class DefaultForgetPasswordCoordinator: ForgetPasswordCoordinator, NavigationCoordinatable {
    
    var stack = NavigationStack(initial: \DefaultForgetPasswordCoordinator.start)

    @Root var start = makeStart
    
    func makeStart() -> some View {
        ForgetPasswordView()
    }
    
    func popToSignIn() {
        
    }
}
@LePips
Copy link
Collaborator

LePips commented Nov 3, 2022

You need to make sure that your NavigationCoordinatable instances are wrapped in a NavigationViewCoordinator.

e.g:

// 1 - wherever you instantiate DefaultSignInCoordinator
NavigationViewCoordinator(DefaultSignInCoordinator())

// 2 - if you want to push views in DefaultForgetPasswordCoordinator, also needs to be wrapped
func makeForgetPassword() -> NavigationViewCoordinator<DefaultForgetPasswordCoordinator> {
    NavigationViewCoordinator(DefaultForgetPasswordCoordinator())
}

@amineahmmouch
Copy link
Author

You are right. I didn't wrap the sign in coordinator in NavigationViewCoordinator.
NavigationViewCoordinator(DefaultSignInCoordinator()).view()

Thank you so much for your help.
Have a nice day.

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