UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Update TabView and move to Home View after login

Forums > SwiftUI

I have the fllowing TabViews in the ContentView. For the onAppear, it works fine. However, I am trying also to reload the tabs and move to the home after the user login from another view.

Following is the code of the ContentView


//  ContentView.swift
//  Matjri
//

//

import SwiftUI

struct ContentView: View {
    @ObservedObject var user = User()

    var body: some View {
        HStack{
            if (user.tokenIsActive) {
                TabView {
                    HomeView()
                        .tabItem {
                            VStack {
                                Image(systemName: "house")
                                Text("Home")
                            }
                    }.tag(0)

                    UserPostsView()
                        .tabItem {
                            VStack {
                                Image(systemName: "person.fill")
                                Text("Me")
                            }
                    }.tag(1)

                    NewPostView()
                        .tabItem {
                            VStack {
                                Image(systemName: "plus")
                                Text("Add")
                            }
                    }.tag(2)
                    SearchView()
                        .tabItem {
                            VStack{
                                Image(systemName: "magnifyingglass")
                                Text("Search")
                            }
                    }.tag(3)
                }
            } else {
                TabView {
                    HomeView()
                        .tabItem {
                            VStack {
                                Image(systemName: "house")
                                Text("Home")
                            }
                    }.tag(0)

                    LoginView()
                        .tabItem {
                            VStack {
                                Image(systemName: "person.fill")
                                Text("Me")
                            }
                    }.tag(1)

                    SearchView()
                        .tabItem{
                            VStack{
                                Image(systemName: "magnifyingglass")
                                Text("Search")
                            }
                    }.tag(2)
                }
            }

        }
        .onAppear() {
            self.checkLoginValidity()
        }
    }

    func checkLoginValidity() {

        let userLogged = UserDefaults.standard.object(forKey: "userIsLogged") as? Bool ?? false

        if (userLogged) {
            let existinLogin = UserDefaults.standard.object(forKey: "loginExpiry") as! Date

            if (existinLogin > Date().addingTimeInterval(86400 * 2)){
                self.user.tokenIsActive = true

            } else {
                self.user.tokenIsActive = false
            }
        } else {
            self.user.tokenIsActive = false
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

When the user login from the LoginView, I set the tokenIsActive to true

struct LoginView: View {

    @ObservedObject var user = User()

    ........

    //Login successful

    self.user.tokenIsActive = true

TabView is not updating. How can I achieve this and I move the user to the homeview after login.

My reference: https://www.hackingwithswift.com/quick-start/swiftui/observable-objects-environment-objects-and-published

2      

I found the solution here.

https://stackoverflow.com/questions/61591474/update-tabview-and-move-to-home-view-after-login/61592035?noredirect=1#comment108949894_61592035

I am sharing it here in case someone new like me face the same problem.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.