Няма описание

NetworkingModel.swift 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // DataModel.swift
  3. // Comedores Sociales
  4. //
  5. // Created by Hector Carrion on 11/8/20.
  6. //
  7. import Foundation
  8. let serverAddress: String = "http://161.35.60.201/"
  9. class Authentication: Codable {
  10. var username: String
  11. var password: String
  12. init(email: String, password: String) {
  13. self.username = email
  14. self.password = password
  15. }
  16. func isComplete() -> Bool {
  17. if username != "" && password != "" {
  18. return true
  19. } else {
  20. return false
  21. }
  22. }
  23. }
  24. class Registration: Codable {
  25. var username: String
  26. var password: String
  27. var nombre: String
  28. var urbanizacion: String
  29. var calle: String
  30. var pueblo: String
  31. var cpostal: String
  32. var telefono: String
  33. var correo: String
  34. var organizacion: String?
  35. var puesto: String?
  36. init(email: String, password: String, name: String, urb: String, street: String, city: String, zip: String, phone: String, org: String? = nil, position: String? = nil) {
  37. if org != "" {
  38. self.username = email
  39. self.password = password
  40. self.nombre = name
  41. self.urbanizacion = urb
  42. self.calle = street
  43. self.pueblo = city
  44. self.cpostal = zip
  45. self.telefono = phone
  46. self.correo = email
  47. self.organizacion = org
  48. self.puesto = position
  49. } else {
  50. self.username = email
  51. self.password = password
  52. self.nombre = name
  53. self.urbanizacion = urb
  54. self.calle = street
  55. self.pueblo = city
  56. self.cpostal = zip
  57. self.telefono = phone
  58. self.correo = email
  59. self.organizacion = nil
  60. self.puesto = nil
  61. }
  62. }
  63. func isComplete() -> Bool {
  64. if username != "" && password != "" && nombre != "" && urbanizacion != "" && calle != "" && pueblo != "" && cpostal != "" && telefono != "" && correo != "" {
  65. return true
  66. } else {
  67. return false
  68. }
  69. }
  70. }
  71. class LoginViewModel: ObservableObject {
  72. @Published var response: Bool?
  73. func changeResponse(response: Bool) {
  74. self.response = response
  75. }
  76. func login(data: Authentication) {
  77. let service = "login.php"
  78. guard let encodedData = try? JSONEncoder().encode(data)
  79. else {
  80. print("Failed to encode login request")
  81. return
  82. }
  83. //print("JSON to send:", String(data: encodedData, encoding: .utf8)!)
  84. let url = URL(string: serverAddress + service)!
  85. var request = URLRequest(url: url)
  86. request.setValue("applogin/json", forHTTPHeaderField: "Content-Type")
  87. request.httpMethod = "POST"
  88. request.httpBody = encodedData
  89. URLSession.shared.dataTask(with: request) {data, response, error in
  90. //handle result here
  91. guard let data = data
  92. else {
  93. print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
  94. return
  95. }
  96. print(response ?? "No response")
  97. print("Recieved data:", data)
  98. print(error ?? "No error")
  99. if let httpResponse = response as? HTTPURLResponse {
  100. DispatchQueue.main.async {
  101. if httpResponse.statusCode == 200 {
  102. self.changeResponse(response: true)
  103. } else {
  104. self.changeResponse(response: false)
  105. }
  106. }
  107. }
  108. }.resume()
  109. }
  110. }
  111. class RegisterViewModel: ObservableObject {
  112. @Published var response: Bool?
  113. func changeResponse(response: Bool) {
  114. self.response = response
  115. }
  116. func register(data: Registration) {
  117. let service = "signup.php"
  118. guard let encodedData = try? JSONEncoder().encode(data)
  119. else {
  120. print("Failed to encode register request")
  121. return
  122. }
  123. print("Reg JSON to send:", String(data: encodedData, encoding: .utf8)!)
  124. let url = URL(string: serverAddress + service)!
  125. var request = URLRequest(url: url)
  126. request.setValue("appregister/json", forHTTPHeaderField: "Content-Type")
  127. request.httpMethod = "POST"
  128. request.httpBody = encodedData
  129. URLSession.shared.dataTask(with: request) {data, response, error in
  130. //handle result here
  131. guard let data = data
  132. else {
  133. print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
  134. return
  135. }
  136. print(response ?? "No response")
  137. print("Recieved data:", data)
  138. print(error ?? "No error")
  139. if let httpResponse = response as? HTTPURLResponse {
  140. DispatchQueue.main.async {
  141. if httpResponse.statusCode == 200 {
  142. self.changeResponse(response: true)
  143. } else {
  144. self.changeResponse(response: false)
  145. }
  146. }
  147. }
  148. }.resume()
  149. }
  150. }