暂无描述

RegisterView.swift 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // RegisterView.swift
  3. // Comedores Sociales
  4. //
  5. // Created by Hector Carrion on 10/24/20.
  6. //
  7. import SwiftUI
  8. class RegisterViewModel: ObservableObject {
  9. @Published var response: Bool?
  10. func changeResponse(response: Bool) {
  11. self.response = response
  12. }
  13. func register(data: Registration) {
  14. let service = "signup.php"
  15. guard let encodedData = try? JSONEncoder().encode(data)
  16. else {
  17. print("Failed to encode register request")
  18. return
  19. }
  20. print("Reg JSON to send:", String(data: encodedData, encoding: .utf8)!)
  21. let url = URL(string: serverAddress + service)!
  22. var request = URLRequest(url: url)
  23. request.setValue("appregister/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 RegisterView: View {
  49. @State var name: String = ""
  50. @State var phoneNumber: String = ""
  51. @State var email: String = ""
  52. @State var password: String = ""
  53. @State var urb: String = ""
  54. @State var city: String = ""
  55. @State var street: String = ""
  56. @State var org: String = ""
  57. @State var position: String = ""
  58. @State var zip: String = ""
  59. @Environment(\.colorScheme) var colorScheme
  60. @ObservedObject var registerVM = RegisterViewModel()
  61. @State private var showingAlert = false
  62. var body: some View {
  63. VStack {
  64. Text("Details")
  65. .font(.largeTitle)
  66. .fontWeight(.semibold)
  67. .frame(width: 350, height: 25, alignment: .topLeading)
  68. .padding(.bottom, 20)
  69. ScrollView {
  70. Group {
  71. TextField("Full Name", text: $name)
  72. .padding()
  73. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  74. .cornerRadius(5.0)
  75. .padding(.bottom, 5)
  76. TextField("Email", text: $email)
  77. .autocapitalization(.none)
  78. .padding()
  79. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  80. .cornerRadius(5.0)
  81. .padding(.bottom, 5)
  82. SecureField("Password", text: $password)
  83. .autocapitalization(.none)
  84. .padding()
  85. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  86. .cornerRadius(5.0)
  87. .padding(.bottom, 5)
  88. TextField("Phone Number", text: $phoneNumber)
  89. .keyboardType(.numberPad)
  90. .padding()
  91. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  92. .cornerRadius(5.0)
  93. .padding(.bottom, 5)
  94. TextField("Urbanization", text: $urb)
  95. .padding()
  96. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  97. .cornerRadius(5.0)
  98. .padding(.bottom, 5)
  99. TextField("Street", text: $street)
  100. .padding()
  101. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  102. .cornerRadius(5.0)
  103. .padding(.bottom, 5)
  104. TextField("City", text: $city)
  105. .padding()
  106. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  107. .cornerRadius(5.0)
  108. .padding(.bottom, 5)
  109. TextField("Zip Code", text: $zip)
  110. .keyboardType(.numberPad)
  111. .padding()
  112. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  113. .cornerRadius(5.0)
  114. .padding(.bottom, 5)
  115. TextField("Organization (optional)", text: $org)
  116. .padding()
  117. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  118. .cornerRadius(5.0)
  119. .padding(.bottom, 5)
  120. TextField("Position (optional)", text: $position)
  121. .padding()
  122. .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
  123. .cornerRadius(5.0)
  124. //.padding(.bottom, 5)
  125. }.padding(5)
  126. }
  127. if self.registerVM.response == false {
  128. Text("Registration failed, please try again")
  129. //.offset(y: -5)
  130. .foregroundColor(.red)
  131. } else if self.registerVM.response == true {
  132. Text("Registration successful 🎉")
  133. //.offset(y: -5)
  134. .foregroundColor(.green)
  135. }
  136. Button(action: {
  137. print("register tapped")
  138. let registrationData = Registration(email: email, password: password, name: name, urb: urb, street: street, city: city, zip: zip, phone: phoneNumber, org: org, position: position)
  139. if registrationData.isComplete() {
  140. self.registerVM.register(data: registrationData)
  141. } else {
  142. // Alert here
  143. self.showingAlert = true
  144. }
  145. }) {
  146. Text("Register")
  147. .font(.headline)
  148. .foregroundColor(.white)
  149. .padding()
  150. .frame(width: 220, height: 50)
  151. .background(Color.blue)
  152. .cornerRadius(5)
  153. .padding(.bottom, 5)
  154. }
  155. }.alert(isPresented: $showingAlert) {
  156. Alert(title: Text("Fields incomplete"), message: Text("Please enter a all required information"), dismissButton: .default(Text("OK")))}
  157. .padding()
  158. }
  159. }
  160. struct RegisterView_Previews: PreviewProvider {
  161. static var previews: some View {
  162. RegisterView()
  163. }
  164. }