ios – SwiftUI NavigationLink becomes activate when button is clicked but deactivate immediately


I am using swiftui with firebase (backend). I am trying to redirect user to other screen (photo screen) when the Sign up button is clicked . I am expecting when user click the sign up button with the information it will store the data into database(firebase) and show the photo screen and stay on that screen. But the problem is when I clicked the button , it navigate to photo screen but Immediately it move to sign up screen again. I am using navigation link with destination ,isActive and label. I also used boolean property to set it true when the NavigationLink becomes active and it is published property.

Here is my @main view code ..

import SwiftUI
import Firebase

@main
struct Twitter_Demo_YoutubeApp: App {
    
    @StateObject var viewModel = AuthViewModel()
    
    init() {
        FirebaseApp.configure()
    }
    var body: some Scene {
        WindowGroup {
            NavigationView {
                ContentView()
            }
            .environmentObject(viewModel)
        }
    }
}

Here is my view model code.

class AuthViewModel: ObservableObject {
    
    @Published var userSession: FirebaseAuth.User?
    @Published var didAuthenciateUser = false
    
    init() {
        self.userSession = Auth.auth().currentUser        
    }
func register(withEmail email: String, password: String, fullname: String, username: String) {
        
        Auth.auth().createUser(withEmail: email, password: password) { result, error in
            if let error = error {
                return
            }
            guard let user = result?.user else { return }
                      
            let data = [
                "email": email,
                "username": username.lowercased(),
                "fullname": fullname,
                "uid": user.uid]
            
            Firestore.firestore().collection("users")
                .document(user.uid)
                .setData(data) { _ in
                    self.didAuthenciateUser = true
            }
        }
    }
}

Here is my Registration view..

import SwiftUI

struct RegistrationView: View {
    @State private var email = ""
    @State private var username = ""
    @State private var fullname = ""
    @State private var password = ""
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var viewModel: AuthViewModel
    
    var body: some View {
        VStack {
            NavigationLink(destination: ProfilePhotoSelecterView(),
                           isActive: $viewModel.didAuthenciateUser, 
                           label: { }) // here is the NavigationLink to be active                              
            
            AuthHeaderView(title1: "Get started.", title2: "Create your account")
            
            VStack(spacing: 40) {
                CustomInputField(imageName: "envelope",
                                 placeholderText: "Email",
                                 text: $email)
                 
                 CustomInputField(imageName: "person",
                                  placeholderText: "username",
                                  text: $username)
                
                CustomInputField(imageName: "person",
                                 placeholderText: "Full Name",
                                 text: $fullname)
                 
                 CustomInputField(imageName: "lock",
                                  placeholderText: "Password",
                                  isSecureField: true,
                                  text: $password)
                
            }
            .padding(32)
            
            Button {
                viewModel.register(withEmail: email,
                                   password: password,
                                   fullname: fullname,
                                   username: username)
            } label: {
                Text("Sign Up")
                    .font(.headline)
                    .foregroundColor(.white)
                    .frame(width: 340, height: 50)
                    .background(Color(.systemBlue))
                    .clipShape(Capsule())
                    .padding()
            }
            .shadow(color: .gray.opacity(0.5), radius: 10, x: 0, y: 0)
            
            
            Spacer()
            
            Button {
                presentationMode.wrappedValue.dismiss()
            } label: {
                HStack {
                    Text("Already have account?")
                        .font(.footnote)
                    Text("Sign In")
                        .font(.footnote)
                        .fontWeight(.semibold)
                }
            }
            .padding(.bottom, 32)
        }
        .ignoresSafeArea()
    }
}

#Preview {
    RegistrationView()
}

Here is the destination view code(ProfilePhotoSelecterView) ..

import SwiftUI

struct ProfilePhotoSelecterView: View {
    
    var body: some View {
        
        VStack {
            AuthHeaderView(title1: "Create your account.", 
                           title2: "Add a profile a photo")
            
            Button {
               print("Pick image here..")
            } label: {
                Image("addphoto")
                    .resizable()
                   // .renderingMode(.template)
                   // .foregroundColor(Color(.systemBlue))
                    .scaledToFill()
                    .frame(width: 180, height: 180)
                    .padding(.top, 44)
            }
            
            Spacer()
        }
        .ignoresSafeArea()
    }
}

#Preview {
    ProfilePhotoSelecterView()
}

Here is the screenshot ..
Initial screen

here is the screenshot when the sign up button is clicked but it redirect with sign up screen automatically .

photo screen

Here is the sign up screen.
enter image description here

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img