No Description

LoginView.swift 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // LogIn.swift
  3. // Comedores Sociales
  4. //
  5. // Created by Hector Carrion on 10/24/20.
  6. //
  7. import SwiftUI
  8. class LoginViewModel: ObservableObject {
  9. @Published var response: Bool?
  10. func changeResponse(response: Bool) {
  11. self.response = response
  12. }
  13. func login(data: Authentication) {
  14. let service = "login.php"
  15. guard let encodedData = try? JSONEncoder().encode(data)
  16. else {
  17. print("Failed to encode login request")
  18. return
  19. }
  20. //print("JSON to send:", String(data: encodedData, encoding: .utf8)!)
  21. let url = URL(string: serverAddress + service)!
  22. var request = URLRequest(url: url)
  23. request.setValue("applogin/json", forHTTPHeaderField: "Content-Type")
  24. request.httpMethod = "POST"
  25. request.httpBody = encodedData
  26. URLSession.shared.dataTask(with: request) {data, response, error in
  27. //handle result here
  28. guard let data = data
  29. else {
  30. print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
  31. return
  32. }
  33. print(response ?? "No response")
  34. print("Recieved data:", data)
  35. print(error ?? "No error")
  36. if let httpResponse = response as? HTTPURLResponse {
  37. DispatchQueue.main.async {
  38. if httpResponse.statusCode == 200 {
  39. self.changeResponse(response: true)
  40. } else {
  41. self.changeResponse(response: false)
  42. }
  43. }
  44. }
  45. }.resume()
  46. }
  47. }
  48. struct LoginView: View {
  49. @State var email: String = ""
  50. @State var password: String = ""
  51. @State var showingRegister: Bool = false
  52. @ObservedObject var loginVM = LoginViewModel()
  53. var body: some View {
  54. VStack {
  55. WelcomeImage()
  56. WelcomeText()
  57. EmailField(email: $email)
  58. PasswordField(password: $password)
  59. if self.loginVM.response == false {
  60. Text("Information incorrect, please try again")
  61. .offset(y: -10)
  62. .foregroundColor(.red)
  63. } else if self.loginVM.response == true {
  64. Text("Login successful 🎉")
  65. .offset(y: -10)
  66. .foregroundColor(.green)
  67. }
  68. Button(action: {
  69. self.loginVM.login(data: Authentication(email: email, password: password))
  70. }) {
  71. LoginText()
  72. }
  73. Button(action: {self.showingRegister.toggle()}) {
  74. RegisterText()
  75. }
  76. .sheet(isPresented: $showingRegister, content: {RegisterView()})
  77. }
  78. .padding()
  79. }
  80. }
  81. struct LogInView_Previews: PreviewProvider {
  82. static var previews: some View {
  83. LoginView()
  84. }
  85. }
  86. struct WelcomeText: View {
  87. var body: some View {
  88. Text("Comedores Sociales")
  89. .font(.largeTitle)
  90. .fontWeight(.semibold)
  91. .padding(.bottom, 20)
  92. }
  93. }
  94. struct WelcomeImage: View {
  95. var body: some View {
  96. Image("logInImage")
  97. .resizable()
  98. .aspectRatio(contentMode: .fill)
  99. .frame(width: 150, height: 150)
  100. .clipped()
  101. //.cornerRadius(150)
  102. //.padding(.bottom, 75)
  103. }
  104. }
  105. struct LoginText: View {
  106. var body: some View {
  107. Text("Login")
  108. .font(.headline)
  109. .foregroundColor(.white)
  110. .padding()
  111. .frame(width: 220, height: 50)
  112. .background(Color.blue)
  113. .cornerRadius(5)
  114. .padding(.bottom, 5)
  115. }
  116. }
  117. struct RegisterText: View {
  118. var body: some View {
  119. Text("Don't have an account? Register")
  120. .font(.headline)
  121. .foregroundColor(.gray)
  122. .padding()
  123. .frame(width: 350, height: 50)
  124. .background(Color.clear)
  125. .cornerRadius(5)
  126. }
  127. }
  128. struct EmailField: View {
  129. @Binding var email: String
  130. @Environment(\.colorScheme) var colorScheme
  131. var body: some View {
  132. TextField("Email", text: $email)
  133. .autocapitalization(.none)
  134. .padding()
  135. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  136. .cornerRadius(5.0)
  137. .padding(.bottom, 5)
  138. }
  139. }
  140. struct PasswordField: View {
  141. @Binding var password: String
  142. @Environment(\.colorScheme) var colorScheme
  143. var body: some View {
  144. SecureField("Password", text: $password)
  145. .autocapitalization(.none)
  146. .padding()
  147. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  148. .cornerRadius(5.0)
  149. .padding(.bottom, 20)
  150. }
  151. }