Login View for SwiftUI

PHOTO EMBED

Sun Aug 22 2021 23:11:07 GMT+0000 (UTC)

Saved by @zwadzinski616 #swift

//
//  ContentView.swift
//  Dyana
//
//  Created by Zach Wadzinski on 3/29/21.
//
// Copyright Dyana Inc.

import SwiftUI
import Firebase

struct LoginView: View {
    @State var email = ""
    @State var password = ""
    @State var isFocused = false
    @State var showAlert = false
    @State var alertMessage = "Something went wrong."
    @State var isLoading = false
    @State var isSuccessful = false
    @EnvironmentObject var user: UserStore
    @State var signupToggle = false

    func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }

    func login() {
        self.hideKeyboard()
        self.isFocused = false
        self.isLoading = true

        Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
            self.isLoading = false

            if error != nil {
                self.alertMessage = error?.localizedDescription ?? ""
                self.showAlert = true
            } else {
                self.isSuccessful = true
                self.user.isLogged = true
                UserDefaults.standard.set(true, forKey: "isLogged")

                DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                    self.isSuccessful = false
                    self.email = ""
                    self.password = ""
                    self.user.showLogin = false
            }
        }
        }
    }


    var body: some View {
        ZStack {
            MainPageView()
            Color(#colorLiteral(red: 0.9787462354, green: 0.9567045569, blue: 0.9562209249, alpha: 1)).edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)

            LoginLogo()
                .padding(.top)


            VStack {


                VinesView()
                    .padding(.vertical, 30)

                LoginTextView()
                    .padding(.vertical, 40)

                GoogleSignInView()

                //EmailLoginView(email: $email, password: $password, isFocused: $isFocused)
                VStack {
                    ZStack {
                        RoundedRectangle(cornerRadius: 8)
                            .fill(Color(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)))
                            .frame(width: .infinity, height: 46)

                        HStack {
                            VStack(alignment: .leading) {
                                TextField("User Email", text: $email, onEditingChanged: { (changed) in
                                        print("Suggestion onEditingChanged - \(changed)")
                                })
                                {
                                        print("Suggestion onCommit")
                                    }.font(.system(size: 14)).foregroundColor(Color(#colorLiteral(red: 0.82, green: 0.82, blue: 0.82, alpha: 1)))


                            }.keyboardType(.emailAddress)
                            .onTapGesture {
                                self.isFocused = true
                            }

                            Spacer()
                        }.padding(.leading)

                    }.padding(.horizontal, 70)

                ZStack {
                    RoundedRectangle(cornerRadius: 8)
                        .fill(Color(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)))
                        .frame(width: .infinity, height: 46)

                    HStack {
                        VStack(alignment: .leading) {
                            TextField("Password", text: $password, onEditingChanged: { (changed) in
                                    print("Suggestion onEditingChanged - \(changed)")
                                }) {
                                    print("Suggestion onCommit")
                                }.font(.system(size: 14)).foregroundColor(Color(#colorLiteral(red: 0.82, green: 0.82, blue: 0.82, alpha: 1)))
                        }.onTapGesture {
                            self.isFocused = true
                        }
                        Spacer()
                    }.padding(.leading)

                }.padding(.horizontal, 70)
                }

                NavigationLink(destination: NameInput()){
                    Text("Forgot Password?").font(.custom("Lato Medium", size: 12)).foregroundColor(Color(#colorLiteral(red: 0.09, green: 0.78, blue: 0.6, alpha: 1))).multilineTextAlignment(.center)
                        .frame(width: nil, height: 15)
                        //.padding(.bottom)
                }

                Button(action: {
                    self.login()
                })
                {
                    SendLoginView()
                        .padding(.top,20)
                        .alert(isPresented: $showAlert) {
                            Alert(title: Text("Error"), message: Text(self.alertMessage), dismissButton: .default(Text("OK")))
                }
                }
                VStack {
                    Button(action: {
                        self.signupToggle.toggle()
                    }) {
                    Text("New to Dyana? Create an account.").font(.custom("Lato Medium", size: 12)).foregroundColor(Color(#colorLiteral(red: 0.09, green: 0.78, blue: 0.6, alpha: 1))).multilineTextAlignment(.center)
                        .frame(width: .infinity, height: 15)
                        .padding(.bottom)
                    }
                }

            }

            ShapesView()

            if isLoading {
                LoadingView()
            }
            if isSuccessful {
                MainPageView()
            }
        }.offset(y: isFocused ? -300 : 0)
        .animation(.easeInOut)
        .onTapGesture {
            self.isFocused = false
            self.hideKeyboard()
        }


    }
}



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


struct LoginLogo: View {
    var body: some View {
        VStack {
            Image("LoginLogo")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 134, height: 45)
                .clipped()
                .frame(width: 134, height: 45)
            Spacer()
        }
    }
}

struct VinesView: View {
    var body: some View {
        VStack {
            //VINES
            Image("LoginVines")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 500, height: 70)
                .clipped()
                .frame(width: .infinity, height: 45)
        }
    }
}

struct LoginTextView: View {
    var body: some View {
        VStack {
            Text("Log In").font(.system(size: 26)).fontWeight(.heavy).foregroundColor(Color(#colorLiteral(red: 0.1, green: 0.27, blue: 0.42, alpha: 1))).multilineTextAlignment(.center)
        }
    }
}

struct GoogleSignInView: View {
    var body: some View {
            ZStack {
                //GOOGLE SIGN IN
                RoundedRectangle(cornerRadius: 8)
                    .fill(Color(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)))
                    .frame(width: .infinity, height: 46)

                RoundedRectangle(cornerRadius: 8)
                    .strokeBorder(Color(#colorLiteral(red: 0.08627451211214066, green: 0.7803921699523926, blue: 0.6039215922355652, alpha: 1)), lineWidth: 1)
                    .frame(width: .infinity, height: 46)

                HStack {
                    //googlelogo 1
                    Image("LoginGoogleLogo")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 29, height: 29)
                        .clipped()
                        .frame(width: 29, height: 29)
                        .padding(.leading, 40)
                    Spacer()

                    //Continue with Google
                    Text("Continue with Google").font(.system(size: 14)).foregroundColor(Color(#colorLiteral(red: 0.09, green: 0.78, blue: 0.6, alpha: 1))).multilineTextAlignment(.center)
                        .padding(.trailing, 90)
                }
            }.padding(.horizontal, 70)


    }
}
content_copyCOPY