Sin descripción

LoginView.swift 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // LogIn.swift
  3. // Comedores Sociales
  4. //
  5. // Created by Hector Carrion on 10/24/20.
  6. //
  7. import SwiftUI
  8. struct LoginView: View {
  9. @EnvironmentObject var loginVM: LoginViewModel
  10. @State var email: String = ""
  11. @State var password: String = ""
  12. @State var showingRegister: Bool = false
  13. @State private var showingAlert = false
  14. var body: some View {
  15. VStack {
  16. WelcomeImage()
  17. WelcomeText()
  18. EmailField(email: $email)
  19. PasswordField(password: $password)
  20. if loginVM.response == .failure {
  21. Text(NSLocalizedString("incorrect", comment:""))
  22. .offset(y: -10)
  23. .foregroundColor(.red)
  24. } else if loginVM.response == .success {
  25. Text("Login successful 🎉")
  26. .offset(y: -10)
  27. .foregroundColor(.green)
  28. } else if loginVM.response == .loading {
  29. ProgressView("Loading…")
  30. .padding()
  31. }
  32. Button(action: {
  33. let authData = Authentication(email: email, password: password)
  34. if authData.isComplete() {
  35. loginVM.login(data: authData)
  36. } else {
  37. // alert
  38. self.showingAlert = true
  39. }
  40. }) {
  41. LoginText()
  42. }
  43. Button(action: {self.showingRegister.toggle()}) {
  44. RegisterText()
  45. }
  46. .sheet(isPresented: $showingRegister, content: {RegisterView()})
  47. .alert(isPresented: $showingAlert) {
  48. Alert(title: Text(NSLocalizedString("incomplete", comment:"")), message: Text(NSLocalizedString("enter_valid", comment:"")), dismissButton: .default(Text("OK")))
  49. }
  50. }
  51. .padding()
  52. }
  53. }
  54. struct WelcomeText: View {
  55. var body: some View {
  56. Text("Comedores Sociales")
  57. .font(.largeTitle)
  58. .fontWeight(.semibold)
  59. .padding(.bottom, 20)
  60. }
  61. }
  62. struct WelcomeImage: View {
  63. var body: some View {
  64. Image("logInImage")
  65. .resizable()
  66. .aspectRatio(contentMode: .fill)
  67. .frame(width: 150, height: 150)
  68. .clipped()
  69. //.cornerRadius(150)
  70. //.padding(.bottom, 75)
  71. }
  72. }
  73. struct LoginText: View {
  74. var body: some View {
  75. Text(NSLocalizedString("login", comment:""))
  76. .font(.headline)
  77. .foregroundColor(.white)
  78. .padding()
  79. .frame(width: 220, height: 50)
  80. .background(Color.blue)
  81. .cornerRadius(5)
  82. .padding(.bottom, 5)
  83. }
  84. }
  85. struct RegisterText: View {
  86. var body: some View {
  87. Text(NSLocalizedString("register", comment:""))
  88. .font(.headline)
  89. .foregroundColor(.gray)
  90. .padding()
  91. .frame(width: 350, height: 50)
  92. .background(Color.clear)
  93. .cornerRadius(5)
  94. }
  95. }
  96. struct EmailField: View {
  97. @Binding var email: String
  98. @Environment(\.colorScheme) var colorScheme
  99. var body: some View {
  100. TextField("Email", text: $email)
  101. .disableAutocorrection(true)
  102. .autocapitalization(.none)
  103. .padding()
  104. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  105. .cornerRadius(5.0)
  106. .padding(.bottom, 5)
  107. }
  108. }
  109. struct PasswordField: View {
  110. @Binding var password: String
  111. @Environment(\.colorScheme) var colorScheme
  112. var body: some View {
  113. SecureField("Password", text: $password)
  114. .autocapitalization(.none)
  115. .padding()
  116. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  117. .cornerRadius(5.0)
  118. .padding(.bottom, 20)
  119. }
  120. }