No Description

LoginView.swift 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // LoginView.swift
  3. // Flowerdex
  4. //
  5. // Created by Víctor A. Hernández on 12/20/20.
  6. //
  7. import SwiftUI
  8. struct LoginView: View {
  9. @EnvironmentObject var lModel: LoginModel
  10. @State var email: String = ""
  11. @State var password: String = ""
  12. @State var showingRegistrationPane: 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. LoginStatusText()
  21. // Login Button
  22. Button(action: {
  23. let authData = LoginData(email: email, password: password)
  24. if authData.isComplete() {
  25. lModel.login(data: authData)
  26. } else {
  27. self.showingAlert = true
  28. }
  29. }) {
  30. LoginButtonText()
  31. }
  32. // Register Button
  33. Button(action: {self.showingRegistrationPane.toggle()}) {
  34. PromptRegisterText()
  35. }
  36. .sheet(isPresented: $showingRegistrationPane, content: { RegisterView(showingRegistrationPane: $showingRegistrationPane) })
  37. .alert(isPresented: $showingAlert) {
  38. Alert(title: Text("Fields incomplete"), message: Text("Please enter a valid email and password"), dismissButton: .default(Text("OK")))
  39. }
  40. }
  41. .padding()
  42. }
  43. }
  44. struct LoginStatusText: View {
  45. @EnvironmentObject var lModel: LoginModel
  46. var body: some View {
  47. if lModel.response == .failure {
  48. Text("No user with given credentials")
  49. .offset(y: -10)
  50. .foregroundColor(.red)
  51. } else if lModel.response == .success {
  52. Text("Login successful 🎉")
  53. .offset(y: -10)
  54. .foregroundColor(.green)
  55. } else if lModel.response == .loading {
  56. ProgressView("Loading…")
  57. .padding()
  58. }
  59. }
  60. }
  61. struct WelcomeText: View {
  62. var body: some View {
  63. Text("Flowerdex")
  64. .font(.largeTitle)
  65. .fontWeight(.semibold)
  66. .foregroundColor(Color("Blue Gray"))
  67. .padding(.bottom, 20)
  68. }
  69. }
  70. struct WelcomeImage: View {
  71. let size: CGFloat = 220
  72. var body: some View {
  73. // TODO: change this to app icon
  74. Image("amapola")
  75. .resizable()
  76. .aspectRatio(contentMode: .fill)
  77. .frame(width: size, height: size)
  78. .clipped()
  79. .cornerRadius(size)
  80. // .padding(.bottom, size / 4)
  81. }
  82. }
  83. struct LoginButtonText: View {
  84. @Environment(\.colorScheme) var colorScheme
  85. var body: some View {
  86. Text("Login")
  87. .font(.headline)
  88. .foregroundColor(colorScheme == .dark ? Constants.Colors.darkGrayColor : .white)
  89. .padding()
  90. .frame(width: 220, height: 50)
  91. .background(Color("Rausch")) // Color.blue
  92. .cornerRadius(5)
  93. .padding(.bottom, 5)
  94. }
  95. }
  96. struct PromptRegisterText: View {
  97. var body: some View {
  98. Text("Don't have an account? Register")
  99. .font(.headline)
  100. .foregroundColor(.gray)
  101. .padding()
  102. .frame(width: 350, height: 50)
  103. .background(Color.clear)
  104. .cornerRadius(5)
  105. }
  106. }
  107. struct EmailField: View {
  108. @Binding var email: String
  109. @Environment(\.colorScheme) var colorScheme
  110. var body: some View {
  111. TextField("Email", text: $email)
  112. .disableAutocorrection(true)
  113. .autocapitalization(.none)
  114. .padding()
  115. .background(colorScheme == .dark ? Constants.Colors.darkGrayColor : Constants.Colors.lightGrayColor)
  116. .cornerRadius(5.0)
  117. .padding(.bottom, 5)
  118. }
  119. }
  120. struct PasswordField: View {
  121. @Binding var password: String
  122. @Environment(\.colorScheme) var colorScheme
  123. var body: some View {
  124. SecureField("Password", text: $password)
  125. .autocapitalization(.none)
  126. .padding()
  127. .background(colorScheme == .dark ? Constants.Colors.darkGrayColor : Constants.Colors.lightGrayColor)
  128. .cornerRadius(5.0)
  129. .padding(.bottom, 20)
  130. }
  131. }