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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.