1

I have a Google Sign-in screen, and then it should transition to a TabView. I tried using a ZStack, but it's causing the app to glitch before each tab is loaded. It will show the sign-in button for a second, and then the correct tab will appear.

Is there a way to totally segue similar to ViewControllers? Or is there a way to totally remove the sign-in button before I call the new view (MainView)?

MainView is just a SwiftUI view with a tabView, and tabItems.

I have a SwiftUI SignInView:

import SwiftUI
import GoogleSignIn

struct SignInView: View {

    @State var loggedIn = false

    let logo = Image('googleLogo')


    var body: some View {
        ZStack {
            Button(action: {
                self.signIn {
                    self.loggedIn = true
                }

            }, label: {
                VStack {
                    logo
                        .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
                        .background(Color.white)
                        .cornerRadius(8.0)
                        .shadow(radius: 4.0)
                     Text("Sign-In")
                        .foregroundColor(.primary)
                        .multilineTextAlignment(.center)
                        .padding(5)
                }
            }).zIndex(-1)
            if loggedIn {
                MainView()
            }
        }
    }
    func signIn(completion: @escaping () -> Void) {
           GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
           GIDSignIn.sharedInstance()?.signIn()
           DispatchQueue(label: "SignIn Check", qos: DispatchQoS.background).async(execute: { () -> Void in
               while true {
                   if GIDSignIn.sharedInstance()?.currentUser != nil {
                       completion()
                       break
                   }
               }

           })

       }
    }

    struct SignInView_Previews: PreviewProvider {
        static var previews: some View {
          SignInView()
        }
    }
Eli Front
  • 695
  • 1
  • 8
  • 28

1 Answers1

2

You can keep your ZStack the way it is and make the sign in button a part of the conditional too in order to fix the glitching you're seeing, but a cleaner solution would be to use a Group instead of a ZStack with the login conditional inside of it:

var body: some View {
    Group {
        if !loggedIn {
            Button(action: {
                self.signIn {
                    self.loggedIn = true
                }

            }, label: {
                VStack {
                    logo
                        .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
                        .background(Color.white)
                        .cornerRadius(8.0)
                        .shadow(radius: 4.0)
                    Text("Sign-In")
                        .foregroundColor(.primary)
                        .multilineTextAlignment(.center)
                        .padding(5)
                }
            })
        } else {
            MainView()
        }
    }
}
RPatel99
  • 7,448
  • 2
  • 37
  • 45