설명 없음

LoginView.swift 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. @State private var showingAlert = false
  54. var body: some View {
  55. VStack {
  56. WelcomeImage()
  57. WelcomeText()
  58. EmailField(email: $email)
  59. PasswordField(password: $password)
  60. if self.loginVM.response == false {
  61. Text("Information incorrect, please try again")
  62. .offset(y: -10)
  63. .foregroundColor(.red)
  64. } else if self.loginVM.response == true {
  65. Text("Login successful 🎉")
  66. .offset(y: -10)
  67. .foregroundColor(.green)
  68. }
  69. Button(action: {
  70. let authData = Authentication(email: email, password: password)
  71. if authData.isComplete() {
  72. self.loginVM.login(data: authData)
  73. } else {
  74. // alert
  75. self.showingAlert = true
  76. }
  77. }) {
  78. LoginText()
  79. }
  80. Button(action: {self.showingRegister.toggle()}) {
  81. RegisterText()
  82. }
  83. .sheet(isPresented: $showingRegister, content: {RegisterView()})
  84. .alert(isPresented: $showingAlert) {
  85. Alert(title: Text("Fields incomplete"), message: Text("Please enter a valid email and password"), dismissButton: .default(Text("OK")))
  86. }
  87. }
  88. .padding()
  89. }
  90. }
  91. struct LogInView_Previews: PreviewProvider {
  92. static var previews: some View {
  93. LoginView()
  94. }
  95. }
  96. struct WelcomeText: View {
  97. var body: some View {
  98. Text("Comedores Sociales")
  99. .font(.largeTitle)
  100. .fontWeight(.semibold)
  101. .padding(.bottom, 20)
  102. }
  103. }
  104. struct WelcomeImage: View {
  105. var body: some View {
  106. Image("logInImage")
  107. .resizable()
  108. .aspectRatio(contentMode: .fill)
  109. .frame(width: 150, height: 150)
  110. .clipped()
  111. //.cornerRadius(150)
  112. //.padding(.bottom, 75)
  113. }
  114. }
  115. struct LoginText: View {
  116. var body: some View {
  117. Text("Login")
  118. .font(.headline)
  119. .foregroundColor(.white)
  120. .padding()
  121. .frame(width: 220, height: 50)
  122. .background(Color.blue)
  123. .cornerRadius(5)
  124. .padding(.bottom, 5)
  125. }
  126. }
  127. struct RegisterText: View {
  128. var body: some View {
  129. Text("Don't have an account? Register")
  130. .font(.headline)
  131. .foregroundColor(.gray)
  132. .padding()
  133. .frame(width: 350, height: 50)
  134. .background(Color.clear)
  135. .cornerRadius(5)
  136. }
  137. }
  138. struct EmailField: View {
  139. @Binding var email: String
  140. @Environment(\.colorScheme) var colorScheme
  141. var body: some View {
  142. TextField("Email", text: $email)
  143. .autocapitalization(.none)
  144. .padding()
  145. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  146. .cornerRadius(5.0)
  147. .padding(.bottom, 5)
  148. }
  149. }
  150. struct PasswordField: View {
  151. @Binding var password: String
  152. @Environment(\.colorScheme) var colorScheme
  153. var body: some View {
  154. SecureField("Password", text: $password)
  155. .autocapitalization(.none)
  156. .padding()
  157. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  158. .cornerRadius(5.0)
  159. .padding(.bottom, 20)
  160. }
  161. }