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

How to manually make view .root #46

Closed
ch05en opened this issue Mar 10, 2021 · 4 comments
Closed

How to manually make view .root #46

ch05en opened this issue Mar 10, 2021 · 4 comments

Comments

@ch05en
Copy link

ch05en commented Mar 10, 2021

I have this app where I use userDefaults to store if the user was previously logged in. If they weren't previousy logged in, the first view is a login page. If they were previously logged in, the first view is a different view. That said, I want to implement a logout feature. But if they were already logged in, and the first view is not the login page, how can I pop back to a view that never appeared ie (the login page).

@mustafaozhan
Copy link
Contributor

mustafaozhan commented Mar 10, 2021

@ch05en
Copy link
Author

ch05en commented Mar 10, 2021

Brother can you help me out please. I cant seem to put your code together and implement it into mine. This is my code

`
import SwiftUI
import Firebase
import CoreData
import NavigationStack

@main
struct USPrimeFreightApp: App {

@ObservedObject var lastLogged = LastLoggedUsersManager()

let persistenceController = PersistenceController.shared
    
var body: some Scene {
    
    WindowGroup {
            
        if lastLogged.lsLogged.count == 0{
           
            StartUpView()
                .environment((\.managedObjectContext), self.persistenceController.container.viewContext)


        }
        else{
            

            if lastLogged.lsLogged[0].lastLoggedUserType == "Driver"{

                DriverContentView(email: $lastLogged.email, userType: $lastLogged.userType, lastLogged: $lastLogged.lsLogged[0])
                    .environment((\.managedObjectContext), self.persistenceController.container.viewContext)




            }
            else if lastLogged.lsLogged[0].lastLoggedUserType == "Admin"{

                AdminContentView(email: $lastLogged.email, userType: $lastLogged.userType, lastLogged: $lastLogged.lsLogged[0])
                    .environment((\.managedObjectContext),self.persistenceController.container.viewContext)
                    .onAppear(perform: {
                        print("\(persistenceController.container.name) is the name")
                    })

            }
        
        }

    }
}

}`

@matteopuc
Copy link
Owner

Hi @ch05en I think something like this might work for you (you can copy paste the example here below and try it yourself):

@main
struct NavigationStackExampleApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationStackView {
                DispatcherView()
            }
        }
    }
}

struct DispatcherView: View {
    @AppStorage("isLoggedIn") var isLoggedIn = false

    var body: some View {
        if isLoggedIn {
            Homepage()
        } else {
            LoginView()
        }
    }
}

struct LoginView: View {
    @EnvironmentObject private var navStack: NavigationStack
    @AppStorage("isLoggedIn") var isLoggedIn = false

    var body: some View {
        VStack {
            Text("LOGIN PAGE")
            Button("Tap to login") {
                isLoggedIn = true
                navStack.push(Homepage())
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

struct Homepage: View {
    @EnvironmentObject private var navStack: NavigationStack

    var body: some View {
        VStack {
            Text("HOMEPAGE")
            Button("Go to logout page") {
                navStack.push(LogoutView())
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

struct LogoutView: View {
    @EnvironmentObject private var navStack: NavigationStack
    @AppStorage("isLoggedIn") var isLoggedIn = false

    var body: some View {
        VStack {
            Text("LOGOUT PAGE")
            Button("Tap to logout") {
                isLoggedIn = false
                navStack.pop(to: .root)
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

Run this example and tap on login. Then kill the app and run it again. This time the homepage will immediately show up. Now, if you tap on logout you'll see the login page appearing on pop.
Please, note that I've used AppStorage just as a convenience way to access UserDefaults, but it's not mandatory.

@ch05en
Copy link
Author

ch05en commented Mar 11, 2021

Hey @matteopuc. IT WORKED!!. Really appreciate the time you took to help me out here. Thank You.

@ch05en ch05en closed this as completed Mar 11, 2021
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

3 participants