No Description

OnboardingViewController.swift 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // OnboardingViewController.swift
  3. // EncuestaMarle
  4. //
  5. // Created by Tatiana Castro on 5/21/19.
  6. // Copyright © 2019 Marle. All rights reserved.
  7. //
  8. import UIKit
  9. import ResearchKit
  10. var jsonDict_reg = [String: Any]()
  11. var n_jsonDict_reg = [String: Any]()
  12. var jsonDict_r = [String: Any]()
  13. var jsonString_reg = String()
  14. var jsonString_r = String()
  15. var jsonData_reg = Data()
  16. var response_reg = String()
  17. var response_r = String()
  18. var task = ORKTaskViewController()
  19. var consentDocument = ConsentDocument()
  20. var task_def = String()
  21. func getConsent_task(task: ORKTaskViewController) -> ORKTaskViewController{
  22. return task
  23. }
  24. func limpiarJson_reg(){
  25. let resultados_encuestas = jsonDict_reg["results"] as! [[String:Any]]
  26. if let resultado_pregunta = resultados_encuestas[2]["results"] as? [[String:Any]]{
  27. n_jsonDict_reg["token"]=token
  28. n_jsonDict_reg["email"]=resultado_pregunta[0]["textAnswer"]
  29. n_jsonDict_reg["password"]=resultado_pregunta[1]["textAnswer"]
  30. n_jsonDict_reg["gender"]=resultado_pregunta[3]["choiceAnswers"]
  31. n_jsonDict_reg["job"]=resultado_pregunta[4]["choiceAnswers"]
  32. let date = resultado_pregunta[5]["dateAnswer"] as! String
  33. n_jsonDict_reg["yearBirth"]=date.prefix(4)
  34. }
  35. }
  36. func sendJson_reg() {
  37. let request = NSMutableURLRequest(url: NSURL(string: "http://emaapp.online/registration.php")! as URL)
  38. request.httpMethod = "POST"
  39. let postString = "data=\(jsonString_reg)"
  40. request.httpBody = postString.data(using: String.Encoding.utf8)
  41. let task = URLSession.shared.dataTask(with: request as URLRequest) {
  42. data, response, error in
  43. if error != nil {
  44. print("error=\(String(describing: error))")
  45. return
  46. }
  47. print("response solito = \(String(describing: response))") //Error: 1062
  48. let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  49. print("responseString reg= \(String(describing: responseString))")
  50. response_reg = responseString! as String
  51. }
  52. task.resume()
  53. }
  54. func sendJson_r() {
  55. let request = NSMutableURLRequest(url: NSURL(string: "http://emaapp.online/recoverAccount.php")! as URL)
  56. request.httpMethod = "POST"
  57. let postString = "data=\(jsonString_r)"
  58. request.httpBody = postString.data(using: String.Encoding.utf8)
  59. let task = URLSession.shared.dataTask(with: request as URLRequest) {
  60. data, response, error in
  61. if error != nil {
  62. print("error=\(String(describing: error))")
  63. return
  64. }
  65. print("response solito = \(String(describing: response))") //Error: 1062
  66. let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  67. print("responseString reg= \(String(describing: responseString))")
  68. response_r = responseString as! String
  69. }
  70. task.resume()
  71. }
  72. class OnboardingViewController: UIViewController {
  73. @IBAction func joinButtonTapped(_ sender: UIButton) {
  74. let consentStep = ORKVisualConsentStep(identifier: "VisualConsentStep", document: consentDocument)
  75. let registrationTitle = NSLocalizedString("Registration", comment: "")
  76. let passcodeValidationRegularExpressionPattern = "^(?=.*\\d).{4,8}$"
  77. let passcodeValidationRegularExpression = try! NSRegularExpression(pattern: passcodeValidationRegularExpressionPattern)
  78. let passcodeInvalidMessage = NSLocalizedString("A valid password must be 4 to 8 characters long and include at least one numeric character.", comment: "")
  79. let registrationOptions: ORKRegistrationStepOption = [.includeGender, .includeDOB, .includeJob]
  80. let registrationStep = ORKRegistrationStep(identifier: "registration", title: registrationTitle, text: "Register for MARLE Study", passcodeValidationRegularExpression: passcodeValidationRegularExpression, passcodeInvalidMessage: passcodeInvalidMessage, options: registrationOptions)
  81. let signature = consentDocument.signatures!.first!
  82. let reviewConsentStep = ORKConsentReviewStep(identifier: "ConsentReviewStep", signature: signature, in: consentDocument)
  83. reviewConsentStep.text = "Review the consent form."
  84. reviewConsentStep.reasonForConsent = "Consent to join the MARLE Study."
  85. let passcodeStep = ORKPasscodeStep(identifier: "Passcode")
  86. passcodeStep.text = "Now you will create a passcode to identify yourself to the app and protect access to information you've entered."
  87. let completionStep = ORKCompletionStep(identifier: "CompletionStep")
  88. completionStep.title = "Welcome aboard."
  89. completionStep.text = "Thank you for joining this study."
  90. print("checko")
  91. task_def = "registration"
  92. let orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, registrationStep])
  93. let taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  94. taskViewController.delegate = self
  95. if response_reg == "Error:Email"{
  96. let orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, registrationStep])
  97. let taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  98. taskViewController.delegate = self
  99. present(taskViewController, animated: true){
  100. let alertTitle = NSLocalizedString("Error: Email Already Registered", comment: "")
  101. let alertMessage = NSLocalizedString("The email used is already registered.", comment: "")
  102. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  103. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  104. taskViewController.present(alert, animated: true, completion: nil)
  105. self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  106. }
  107. }
  108. else if response_reg == "Error:Token"{
  109. let orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, registrationStep])
  110. let taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  111. taskViewController.delegate = self
  112. present(taskViewController, animated: true){
  113. let alertTitle = NSLocalizedString("Error: Phone Already Used", comment: "")
  114. let alertMessage = NSLocalizedString("This phone has been used to create an account already. Please contact your mentor.", comment: "")
  115. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  116. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  117. taskViewController.present(alert, animated: true, completion: nil)
  118. self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  119. }
  120. }
  121. else{
  122. let c_orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, registrationStep, completionStep])
  123. let c_taskViewController = ORKTaskViewController(task: c_orderedTask, taskRun: nil)
  124. c_taskViewController.delegate = self
  125. present(c_taskViewController, animated: true, completion: nil)
  126. let taskViewController = ORKTaskViewController(task: EncuestaMarle, taskRun: nil)
  127. taskViewController.delegate = (self as ORKTaskViewControllerDelegate)
  128. present(taskViewController, animated: true, completion: nil)
  129. }
  130. }
  131. @IBAction func recover_account(_ sender: UIButton) {
  132. class LoginViewController : ORKLoginStepViewController {
  133. override func forgotPasswordButtonTapped() {
  134. let alertTitle = NSLocalizedString("Help", comment: "")
  135. let alertMessage = NSLocalizedString("You will need to contact your P.I., so they can determine the next steps.", comment: "")
  136. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  137. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  138. self.present(alert, animated: true, completion: nil)
  139. }
  140. }
  141. let loginTitle = NSLocalizedString("Recover Account", comment: "")
  142. let loginStep = ORKLoginStep(identifier: "recover" , title: loginTitle, text: "Fill out information to recover account", loginViewControllerClass: LoginViewController.self)
  143. let orderedTask = ORKOrderedTask(identifier: "Join", steps: [loginStep])
  144. let taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  145. taskViewController.delegate = self
  146. task_def = "recover"
  147. present(taskViewController, animated: true, completion: nil)
  148. }
  149. }
  150. extension OnboardingViewController : ORKTaskViewControllerDelegate {
  151. public func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
  152. switch reason {
  153. case .completed:
  154. if task_def == "recover"{
  155. let taskResult_r = taskViewController.result
  156. let jsonData_r = try! ORKESerializer.jsonData(for: taskResult_r)
  157. jsonString_r = NSString(data: jsonData_r, encoding: String.Encoding.utf8.rawValue)! as String
  158. jsonDict_r = convertToDictionary(text: jsonString_r as String) as! [String: Any]
  159. do {
  160. let jsonData_r = try JSONSerialization.data(withJSONObject: jsonDict_r, options: .prettyPrinted)
  161. jsonString_r = NSString(data: jsonData_r, encoding: String.Encoding.utf8.rawValue)! as String
  162. }
  163. catch {
  164. print(error.localizedDescription)
  165. }
  166. sendJson_r()
  167. if response_r == "Error:No"{
  168. let alertTitle = NSLocalizedString("Incorrect credentials", comment: "")
  169. let alertMessage = NSLocalizedString("The password and/or email provided were not found.", comment: "")
  170. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  171. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  172. self.present(alert, animated: true, completion: nil)
  173. performSegue(withIdentifier: "toOnboarding", sender: nil)
  174. }
  175. // else if response_r == "Error: token"{
  176. // let alertTitle = NSLocalizedString("Token found", comment: "")
  177. // let alertMessage = NSLocalizedString("The password and/or email provided were not found.", comment: "")
  178. // let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  179. // alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  180. // self.present(alert, animated: true, completion: nil)
  181. // performSegue(withIdentifier: "toOnboarding", sender: nil)
  182. // }
  183. else{
  184. performSegue(withIdentifier: "unwindToStudy", sender: nil)
  185. }
  186. }
  187. else{
  188. let taskResult_reg = taskViewController.result
  189. // let signatureResult = taskResult_reg as! ORKConsentSignatureResult
  190. // let signature = signatureResult.signature!
  191. // print(signature)
  192. let jsonData_reg = try! ORKESerializer.jsonData(for: taskResult_reg)
  193. jsonString_reg = NSString(data: jsonData_reg, encoding: String.Encoding.utf8.rawValue)! as String
  194. jsonDict_reg = convertToDictionary(text: jsonString_reg as String) as! [String: Any]
  195. limpiarJson_reg()
  196. do {
  197. let jsonData_reg = try JSONSerialization.data(withJSONObject: n_jsonDict_reg, options: .prettyPrinted)
  198. jsonString_reg = NSString(data: jsonData_reg, encoding: String.Encoding.utf8.rawValue)! as String
  199. }
  200. catch {
  201. print(error.localizedDescription)
  202. }
  203. sendJson_reg()
  204. if response_reg == "Error:email"{
  205. present(taskViewController, animated: true){
  206. let alertTitle = NSLocalizedString("Error: Email Already Registered", comment: "")
  207. let alertMessage = NSLocalizedString("The email used is already registered.", comment: "")
  208. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  209. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  210. taskViewController.present(alert, animated: true, completion: nil)
  211. self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  212. }
  213. }
  214. else if response_reg == "Error:token"{
  215. present(taskViewController, animated: true){
  216. let alertTitle = NSLocalizedString("Error: Phone Already Used", comment: "")
  217. let alertMessage = NSLocalizedString("This phone has been used to create an account already. Please contact your mentor.", comment: "")
  218. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  219. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  220. taskViewController.present(alert, animated: true, completion: nil)
  221. self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  222. }
  223. }
  224. else{
  225. UserDefaults.standard.set(true, forKey: "registered")
  226. // let vc = ResearchContainerViewController()
  227. // self.present(vc, animated: true, completion: nil)
  228. performSegue(withIdentifier: "unwindToStudy", sender: nil)
  229. // performSegue(withIdentifier: "toStudy2", sender: self)
  230. }
  231. // else{
  232. // present(taskViewController, animated: true){
  233. // let completionStep = ORKCompletionStep(identifier: "CompletionStep")
  234. // completionStep.title = "Welcome aboard."
  235. // completionStep.text = "Thank you for joining this study."
  236. // let orderedTask = ORKOrderedTask(identifier: "Join", steps: [completionStep])
  237. // let c_taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  238. // c_taskViewController.delegate = self
  239. // taskViewController.present(c_taskViewController, animated: true)
  240. // }
  241. // }
  242. }
  243. case .discarded, .failed, .saved:
  244. dismiss(animated: true, completion: nil)
  245. }
  246. }
  247. }