I created a new SwiftUI project but the code will not load in the live preview window I get the following error every time:
Cannot preview in this file -. but when I run it on simulator it works good also for other views it works.
This is the code
import SwiftUI
import FirebaseAuth
class AppViewModel: ObservableObject {
let auth = Auth.auth()
@Published var LoggedIn = false
var isLoggedIn: Bool{
return auth.currentUser != nil
}
func LogIn(email: String, password: String) {
auth.signIn(withEmail: email, password: password) { [weak self] result, error in
guard result != nil, error == nil else{
return
}
DispatchQueue.main.async {
// Success
self?.LoggedIn = true
}
}
}
func SignUp(email: String, password: String) {
auth.createUser(withEmail: email, password: password) { [weak self] result, error in
guard result != nil, error == nil else{
return
}
DispatchQueue.main.async {
// Success
self?.LoggedIn = true
}
}
}
}
struct ContentView: View {
@EnvironmentObject var ViewModel : AppViewModel
var body: some View {
NavigationView {
if ViewModel.LoggedIn {
Text ("You are Logged In")
} else{
LogInView()
}
}
.onAppear {
ViewModel.LoggedIn = ViewModel.isLoggedIn
}
}
}
struct LogInView: View {
@State var email = ""
@State var password = ""
@EnvironmentObject var ViewModel : AppViewModel
var body: some View {
VStack {
TextField("Email Adress", text: $email)
.disableAutocorrection(true)
.autocapitalization(.none)
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(15)
SecureField("Password", text: $password)
.disableAutocorrection(true)
.autocapitalization(.none)
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(15)
Button(action: {
guard !email.isEmpty, !password.isEmpty else{
return
}
ViewModel.LogIn(email: email, password: password)
}) {
Text("Log In")
.foregroundColor(Color.white)
.font(.headline)
.frame(maxWidth: .infinity)
.frame(height: 50)
.background(Color.accentColor)
.cornerRadius(20.0)
}
NavigationLink("Create an account", destination: SignUpView())
.padding()
}
.padding(.horizontal, 24.0)
.navigationTitle("Welcome")
}
}
struct SignUpView: View {
@State var email = ""
@State var password = ""
@EnvironmentObject var ViewModel : AppViewModel
var body: some View {
VStack {
TextField("Email Adress", text: $email)
.disableAutocorrection(true)
.autocapitalization(.none)
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(15)
SecureField("Password", text: $password)
.disableAutocorrection(true)
.autocapitalization(.none)
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(15)
Button(action: {
guard !email.isEmpty, !password.isEmpty else{
return
}
ViewModel.SignUp(email: email, password: password)
}) {
Text("Sign Up")
.foregroundColor(Color.white)
.font(.headline)
.frame(maxWidth: .infinity)
.frame(height: 50)
.background(Color.accentColor)
.cornerRadius(20.0)
}
}
.padding(.horizontal, 24.0)
.navigationTitle("Create an Account")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}