Aucune description

OnboardingViewController.swift 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 n_jsonDict_r = [String: Any]()
  14. var jsonString_reg = String()
  15. var jsonString_r = String()
  16. var jsonData_reg = Data()
  17. var newResponseReg: Bool = false
  18. var response_reg: String = "" {
  19. didSet {
  20. newResponseReg = true
  21. }
  22. }
  23. var newResponseR: Bool = false
  24. var response_r: String = "" {
  25. didSet {
  26. newResponseR = true
  27. }
  28. }
  29. var task = ORKTaskViewController()
  30. var consentDocument = ConsentDocument()
  31. var task_def = String()
  32. var sentPDF: Bool = false
  33. var jsonPDF = String()
  34. var response_PDF = String()
  35. var tokenPDF = String()
  36. var resetPassword: Bool = false
  37. var jsonString_reset = String()
  38. var jsonDict_reset = [String: Any]()
  39. var n_jsonDict_reset = [String: Any]()
  40. var response_reset = String()
  41. var response_id = String()
  42. func getConsent_task(task: ORKTaskViewController) -> ORKTaskViewController{
  43. return task
  44. }
  45. func limpiarJson_reg(){
  46. let resultados_encuestas = jsonDict_reg["results"] as! [[String:Any]]
  47. if let resultado_pregunta = resultados_encuestas[2]["results"] as? [[String:Any]]{
  48. n_jsonDict_reg["token"]=token
  49. tokenPDF = token
  50. n_jsonDict_reg["email"]=resultado_pregunta[0]["textAnswer"]
  51. n_jsonDict_reg["password"]=resultado_pregunta[1]["textAnswer"]
  52. n_jsonDict_reg["gender"]=resultado_pregunta[3]["choiceAnswers"]
  53. // -------------------------------------------- ☹️ ----------------------------------------------
  54. // Removed due to Apple screwing up the keyboard constraints
  55. n_jsonDict_reg["job"]=resultado_pregunta[4]["choiceAnswers"]
  56. let date = resultado_pregunta[5]["dateAnswer"] as! String
  57. n_jsonDict_reg["yearBirth"]=date.prefix(4)
  58. }
  59. }
  60. func limpiarJson_recovery(){
  61. let resultados_encuestas = jsonDict_r["results"] as! [[String:Any]]
  62. if let resultado_pregunta = resultados_encuestas[0]["results"] as? [[String:Any]]{
  63. n_jsonDict_r["token"]=token
  64. n_jsonDict_r["email"]=resultado_pregunta[0]["textAnswer"]
  65. n_jsonDict_r["password"]=resultado_pregunta[1]["textAnswer"]
  66. }
  67. }
  68. //func limpiarJson_reset(){
  69. // let resultados_encuestas = jsonDict_reset["results"] as! [[String:Any]]
  70. // if let resultado_pregunta = resultados_encuestas[0]["results"] as? [[String:Any]]{
  71. // n_jsonDict_reset["email"]=resultado_pregunta[0]["textAnswer"]
  72. // }
  73. //}
  74. func sendEmail(email: String) {
  75. //impiarJson_reset()
  76. let jsonObject: NSMutableDictionary = NSMutableDictionary()
  77. jsonObject.setValue(email, forKey: "email")
  78. let request = NSMutableURLRequest(url: NSURL(string: "http://tania.uprrp.edu/askForEmail.php")! as URL)
  79. request.httpMethod = "POST"
  80. let jsonData: NSData
  81. do {
  82. jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: JSONSerialization.WritingOptions()) as NSData
  83. let jsonEmail = NSString(data: jsonData as Data, encoding: String.Encoding.utf8.rawValue) as! String
  84. print("✅ Email recieved, sending request...")
  85. let postString = "data=\(jsonEmail)"
  86. request.httpBody = postString.data(using: String.Encoding.utf8)
  87. let task = URLSession.shared.dataTask(with: request as URLRequest) {
  88. data, response, error in
  89. if error != nil {
  90. print("error=\(String(describing: error))")
  91. return
  92. }
  93. print("response solito Email = \(String(describing: response))") //Error: 1062
  94. let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  95. print("responseString Email = \(String(describing: responseString))")
  96. response_reset = responseString as! String
  97. }
  98. task.resume()
  99. } catch {
  100. print ("❌ JSON Failure")
  101. }
  102. }
  103. func sendJson_reg() {
  104. let request = NSMutableURLRequest(url: NSURL(string: "http://tania.uprrp.edu/registration.php")! as URL)
  105. request.httpMethod = "POST"
  106. let postString = "data=\(jsonString_reg)"
  107. print("json Sign-Up ✅")
  108. request.httpBody = postString.data(using: String.Encoding.utf8)
  109. let task = URLSession.shared.dataTask(with: request as URLRequest) {
  110. data, response, error in
  111. if error != nil {
  112. print("error=\(String(describing: error))")
  113. return
  114. }
  115. print("response solito Sign Up = \(String(describing: response))") //Error: 1062
  116. let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  117. print("responseString Sign Up = \(String(describing: responseString))")
  118. response_reg = responseString! as String
  119. }
  120. task.resume()
  121. }
  122. func sendJson_r() {
  123. limpiarJson_recovery()
  124. do {
  125. let jsonData_r = try JSONSerialization.data(withJSONObject: n_jsonDict_r, options: .prettyPrinted)
  126. jsonString_r = NSString(data: jsonData_r, encoding: String.Encoding.utf8.rawValue)! as String
  127. }
  128. catch {
  129. print(error.localizedDescription)
  130. }
  131. let request = NSMutableURLRequest(url: NSURL(string: "http://tania.uprrp.edu/recoverAccount.php")! as URL)
  132. request.httpMethod = "POST"
  133. let postString = "data=\(jsonString_r)"
  134. request.httpBody = postString.data(using: String.Encoding.utf8)
  135. let task = URLSession.shared.dataTask(with: request as URLRequest) {
  136. data, response, error in
  137. if error != nil {
  138. print("error=\(String(describing: error))")
  139. return
  140. }
  141. print("response Recover Account ⚠️ solito = \(String(describing: response))") //Error: 1062
  142. let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  143. print("response Recover Account ⚠️ reg= \(String(describing: responseString))")
  144. response_r = responseString as! String
  145. // REMEMBER TO PRESENT ERRORS BASED ON RESPONSE (EMAIL MAL OR PW MAL)
  146. }
  147. task.resume()
  148. }
  149. func sendPDF(_ base64EncodedPDF: String, _ date: Date) {
  150. if sentPDF == false {
  151. let request = NSMutableURLRequest(url: NSURL(string: "http://tania.uprrp.edu/getSignatureIOS2.php")! as URL)
  152. request.httpMethod = "POST"
  153. // let jsonObject1: [String: Any] = [
  154. // "data": [
  155. // "signaturePDFBase64": base64EncodedPDF,
  156. // "signatureDate": date,
  157. // "token": tokenPDF
  158. // ],
  159. // ]
  160. let jsonObject: NSMutableDictionary = NSMutableDictionary()
  161. jsonObject.setValue(base64EncodedPDF, forKey: "signaturePDFBase64")
  162. jsonObject.setValue(date.description, forKey: "signatureDate")
  163. jsonObject.setValue(tokenPDF, forKey: "token")
  164. print("Date is formatted as: 📆")
  165. print(date.description)
  166. let jsonData: NSData
  167. do {
  168. jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: JSONSerialization.WritingOptions()) as NSData
  169. let jsonString_PDF = NSString(data: jsonData as Data, encoding: String.Encoding.utf8.rawValue) as! String
  170. print("✅ jsonPDF Created, sending request...")
  171. let postString = "data=\(jsonString_PDF)"
  172. request.httpBody = postString.data(using: String.Encoding.utf8)
  173. let task = URLSession.shared.dataTask(with: request as URLRequest) {
  174. data, response, error in
  175. if error != nil {
  176. print("error=\(String(describing: error))")
  177. return
  178. }
  179. print("response solito PDF = \(String(describing: response))") //Error: 1062
  180. let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
  181. print("responseString PDF = \(String(describing: responseString))")
  182. response_PDF = responseString as! String
  183. }
  184. sentPDF = true
  185. task.resume()
  186. } catch {
  187. print ("❌ JSON Failure")
  188. }
  189. }
  190. }
  191. class OnboardingViewController: UIViewController {
  192. @IBAction func joinButtonTapped(_ sender: UIButton) {
  193. let consentStep = ORKVisualConsentStep(identifier: "VisualConsentStep", document: consentDocument)
  194. let registrationTitle = NSLocalizedString("Registration", comment: "")
  195. let passcodeValidationRegularExpressionPattern = "^(?=.*\\d).{4,8}$"
  196. let passcodeValidationRegularExpression = try! NSRegularExpression(pattern: passcodeValidationRegularExpressionPattern)
  197. let passcodeInvalidMessage = NSLocalizedString("A valid password must be 4 to 8 characters long and include at least one numeric character.", comment: "")
  198. let registrationOptions: ORKRegistrationStepOption = [.includeGender, .includeJob, .includeDOB]
  199. let registrationStep = ORKRegistrationStep(identifier: "registration", title: registrationTitle, text: "Register for MARLE Study", passcodeValidationRegularExpression: passcodeValidationRegularExpression, passcodeInvalidMessage: passcodeInvalidMessage, options: registrationOptions)
  200. let signature = consentDocument.signatures!.first!
  201. let reviewConsentStep = ORKConsentReviewStep(identifier: "ConsentReviewStep", signature: signature, in: consentDocument)
  202. reviewConsentStep.text = "Review the consent form."
  203. reviewConsentStep.reasonForConsent = "Consent to join the MARLE Study."
  204. let passcodeStep = ORKPasscodeStep(identifier: "Passcode")
  205. passcodeStep.text = "Now you will create a passcode to identify yourself to the app and protect access to information you've entered."
  206. let completionStep = ORKCompletionStep(identifier: "CompletionStep")
  207. completionStep.title = "Welcome aboard."
  208. completionStep.text = "Thank you for joining this study."
  209. print("checko")
  210. task_def = "registration"
  211. let orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, registrationStep])
  212. let taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  213. taskViewController.delegate = self
  214. if response_reg == "Error:Email"{
  215. let orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, registrationStep])
  216. let taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  217. taskViewController.delegate = self
  218. present(taskViewController, animated: true){
  219. let alertTitle = NSLocalizedString("Error: Email Already Registered", comment: "")
  220. let alertMessage = NSLocalizedString("Exit and try recovering your account", comment: "")
  221. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  222. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  223. taskViewController.present(alert, animated: true, completion: nil)
  224. self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  225. }
  226. }
  227. else if response_reg == "Error:Token"{
  228. let orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, registrationStep])
  229. let taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  230. taskViewController.delegate = self
  231. present(taskViewController, animated: true){
  232. let alertTitle = NSLocalizedString("Error: Phone Already Used", comment: "")
  233. let alertMessage = NSLocalizedString("This phone has been used to create an account already. Please contact your mentor.", comment: "")
  234. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  235. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  236. taskViewController.present(alert, animated: true, completion: nil)
  237. self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  238. }
  239. }
  240. else{
  241. let c_orderedTask = ORKOrderedTask(identifier: "Join", steps: [consentStep, reviewConsentStep, registrationStep, completionStep])
  242. let c_taskViewController = ORKTaskViewController(task: c_orderedTask, taskRun: nil)
  243. c_taskViewController.delegate = self
  244. present(c_taskViewController, animated: true, completion: nil)
  245. let taskViewController = ORKTaskViewController(task: EncuestaMarle, taskRun: nil)
  246. taskViewController.delegate = (self as ORKTaskViewControllerDelegate)
  247. present(taskViewController, animated: true, completion: nil)
  248. }
  249. }
  250. @IBAction func recover_account(_ sender: UIButton) {
  251. class LoginViewController : ORKLoginStepViewController {
  252. override func forgotPasswordButtonTapped() {
  253. var email: String = ""
  254. let alertTitle = NSLocalizedString("Enter your email", comment: "")
  255. let alertMessage = NSLocalizedString("You will recieve instructions to reset your password.", comment: "")
  256. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  257. alert.addTextField { (textField) in
  258. textField.placeholder = "Email"
  259. }
  260. alert.addAction(UIAlertAction(title: "Submit", style: UIAlertAction.Style.default, handler: { [weak alert] (_) in
  261. let textField = alert!.textFields![0] // Force unwrapping because we know it exists.
  262. print("Text field email: \(String(describing: textField.text))")
  263. email = textField.text!
  264. sendEmail(email: email)
  265. let alertTitle2 = NSLocalizedString("If the email is registered we will send you an email", comment: "")
  266. let alertMessage2 = NSLocalizedString("Check \(email) for instructions to reset your password", comment: "")
  267. let alert2 = UIAlertController(title: alertTitle2, message: alertMessage2, preferredStyle: UIAlertController.Style.alert)
  268. alert2.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  269. self.present(alert2, animated: true, completion: nil)
  270. }))
  271. alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel , handler: nil))
  272. self.present(alert, animated: true, completion: nil)
  273. }
  274. }
  275. let loginTitle = NSLocalizedString("Recover Account", comment: "")
  276. let loginStep = ORKLoginStep(identifier: "recover" , title: loginTitle, text: "Fill out information to recover account", loginViewControllerClass: LoginViewController.self)
  277. let orderedTask = ORKOrderedTask(identifier: "Join", steps: [loginStep])
  278. let taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  279. taskViewController.delegate = self
  280. if (resetPassword) {
  281. task_def = "reset"
  282. resetPassword = false
  283. let alertTitle2 = NSLocalizedString("Email will arrive soon", comment: "")
  284. let alertMessage2 = NSLocalizedString("Check your inbox for instructions to reset your password", comment: "")
  285. let alert2 = UIAlertController(title: alertTitle2, message: alertMessage2, preferredStyle: UIAlertController.Style.alert)
  286. alert2.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  287. self.present(alert2, animated: true, completion: nil)
  288. }
  289. else {
  290. task_def = "recover"
  291. }
  292. present(taskViewController, animated: true, completion: nil)
  293. }
  294. }
  295. extension OnboardingViewController : ORKTaskViewControllerDelegate {
  296. public func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
  297. switch reason {
  298. case .completed:
  299. if task_def == "reset" {
  300. let taskResult_reset = taskViewController.result
  301. let jsonData_reset = try! ORKESerializer.jsonData(for: taskResult_reset)
  302. jsonString_reset = NSString(data: jsonData_reset, encoding: String.Encoding.utf8.rawValue)! as String
  303. jsonDict_reset = convertToDictionary(text: jsonString_r as String) as! [String: Any]
  304. do {
  305. let jsonData_reset = try JSONSerialization.data(withJSONObject: jsonDict_reset, options: .prettyPrinted)
  306. jsonString_reset = NSString(data: jsonData_reset, encoding: String.Encoding.utf8.rawValue)! as String
  307. }
  308. catch {
  309. print(error.localizedDescription)
  310. }
  311. //sendEmail()
  312. }
  313. else if task_def == "recover"{
  314. let taskResult_r = taskViewController.result
  315. let jsonData_r = try! ORKESerializer.jsonData(for: taskResult_r)
  316. jsonString_r = NSString(data: jsonData_r, encoding: String.Encoding.utf8.rawValue)! as String
  317. jsonDict_r = convertToDictionary(text: jsonString_r as String) as! [String: Any]
  318. do {
  319. let jsonData_r = try JSONSerialization.data(withJSONObject: jsonDict_r, options: .prettyPrinted)
  320. jsonString_r = NSString(data: jsonData_r, encoding: String.Encoding.utf8.rawValue)! as String
  321. }
  322. catch {
  323. print(error.localizedDescription)
  324. }
  325. sendJson_r()
  326. // TODO:
  327. // THIS REPRESENTS HOW TO RESPOND TO WRONG INPUT
  328. while(newResponseR == false) {}
  329. newResponseR = false
  330. // implement if (resetPassword == true) workflow
  331. if (response_r == "Error:hash" || response_r == "Error:password") {
  332. let alertTitle = NSLocalizedString("Email and Password do not match", comment: "")
  333. let alertMessage = NSLocalizedString("Try again or tap forgot password.", comment: "")
  334. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  335. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  336. taskViewController.present(alert, animated: true, completion: nil)
  337. //self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  338. }
  339. else if response_r == "Error:token"{
  340. let alertTitle = NSLocalizedString("Phone already registered", comment: "")
  341. let alertMessage = NSLocalizedString("This phone has been used to create an account already. Please contact your mentor and inform them about this error message", comment: "")
  342. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  343. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  344. taskViewController.present(alert, animated: true, completion: nil)
  345. //self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  346. }
  347. else {
  348. UserDefaults.standard.set(true, forKey: "registered")
  349. UserDefaults.standard.synchronize()
  350. performSegue(withIdentifier: "unwindToStudy", sender: nil)
  351. }
  352. }
  353. else{
  354. let taskResult_reg = taskViewController.result
  355. if let stepResult = taskResult_reg.stepResult(forStepIdentifier: "ConsentReviewStep"),
  356. let signatureResult = stepResult.results?.first as? ORKConsentSignatureResult {
  357. print("Signature generated ✅")
  358. print(signatureResult)
  359. let signatureDate = signatureResult.endDate
  360. // lets try to make a pdf for this
  361. let documentCopy = consentDocument
  362. signatureResult.apply(to: documentCopy)
  363. documentCopy.makePDF { (data, error) -> Void in
  364. var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last as NSURL?
  365. docURL = docURL?.appendingPathComponent("ConsentDocument.pdf") as NSURL?
  366. do {
  367. try data?.write(to:docURL! as URL)
  368. print("PDF generated ✅")
  369. print(docURL! as URL)
  370. if let pdfBase64 = data?.base64EncodedString() {
  371. sendPDF(pdfBase64, signatureDate)
  372. }
  373. } catch let error {
  374. print("❌")
  375. print(error.localizedDescription)
  376. }
  377. }
  378. // let sigJson = try! ORKESerializer.jsonData(for: signatureResult)
  379. // print(sigJson.base64EncodedString())
  380. }
  381. let jsonData_reg = try! ORKESerializer.jsonData(for: taskResult_reg)
  382. jsonString_reg = NSString(data: jsonData_reg, encoding: String.Encoding.utf8.rawValue)! as String
  383. jsonDict_reg = convertToDictionary(text: jsonString_reg as String) as! [String: Any]
  384. limpiarJson_reg()
  385. do {
  386. let jsonData_reg = try JSONSerialization.data(withJSONObject: n_jsonDict_reg, options: .prettyPrinted)
  387. jsonString_reg = NSString(data: jsonData_reg, encoding: String.Encoding.utf8.rawValue)! as String
  388. }
  389. catch {
  390. print(error.localizedDescription)
  391. }
  392. // TODO:
  393. // MAYBE THIS DOES IT BETTER
  394. sendJson_reg()
  395. while(newResponseReg == false) {}
  396. newResponseReg = false
  397. if response_reg == "Error:email"{
  398. let alertTitle = NSLocalizedString("Email Already Registered", comment: "")
  399. let alertMessage = NSLocalizedString("Exit and try recovering your account", comment: "")
  400. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  401. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  402. taskViewController.present(alert, animated: true, completion: nil)
  403. //self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  404. }
  405. else if response_reg == "Error:token"{
  406. let alertTitle = NSLocalizedString("Phone Already Used", comment: "")
  407. let alertMessage = NSLocalizedString("This phone has been used to create an account already. Please contact your mentor.", comment: "")
  408. let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert)
  409. alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
  410. taskViewController.present(alert, animated: true, completion: nil)
  411. //self.performSegue(withIdentifier: "toOnboarding", sender: nil)
  412. }
  413. else{
  414. UserDefaults.standard.set(true, forKey: "registered")
  415. UserDefaults.standard.synchronize()
  416. // let vc = ResearchContainerViewController()
  417. // self.present(vc, animated: true, completion: nil)
  418. performSegue(withIdentifier: "unwindToStudy", sender: nil)
  419. // performSegue(withIdentifier: "toStudy2", sender: self)
  420. }
  421. // else{
  422. // present(taskViewController, animated: true){
  423. // let completionStep = ORKCompletionStep(identifier: "CompletionStep")
  424. // completionStep.title = "Welcome aboard."
  425. // completionStep.text = "Thank you for joining this study."
  426. // let orderedTask = ORKOrderedTask(identifier: "Join", steps: [completionStep])
  427. // let c_taskViewController = ORKTaskViewController(task: orderedTask, taskRun: nil)
  428. // c_taskViewController.delegate = self
  429. // taskViewController.present(c_taskViewController, animated: true)
  430. // }
  431. // }
  432. }
  433. case .discarded, .failed, .saved:
  434. dismiss(animated: true, completion: nil)
  435. }
  436. }
  437. }
  438. @IBDesignable extension UIButton {
  439. @IBInspectable var borderWidth: CGFloat {
  440. set {
  441. layer.borderWidth = newValue
  442. }
  443. get {
  444. return layer.borderWidth
  445. }
  446. }
  447. @IBInspectable var cornerRadius: CGFloat {
  448. set {
  449. layer.cornerRadius = newValue
  450. }
  451. get {
  452. return layer.cornerRadius
  453. }
  454. }
  455. @IBInspectable var borderColor: UIColor? {
  456. set {
  457. guard let uiColor = newValue else { return }
  458. layer.borderColor = uiColor.cgColor
  459. }
  460. get {
  461. guard let color = layer.borderColor else { return nil }
  462. return UIColor(cgColor: color)
  463. }
  464. }
  465. }
  466. //extension NSLayoutConstraint {
  467. //
  468. // override public var description: String {
  469. // let id = identifier ?? ""
  470. // return "id: \(id), constant: \(constant)" //you may print whatever you want here
  471. // }
  472. //}