You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
4.6 KiB
Swift
102 lines
4.6 KiB
Swift
//
|
|
// TasksViewController.swift
|
|
// RK-Journals
|
|
//
|
|
// Created by Juan David Lopez Regalado on 11/11/24.
|
|
//
|
|
|
|
import UIKit
|
|
import ResearchKitUI
|
|
|
|
class TasksViewController: UIViewController, ORKTaskViewControllerDelegate {
|
|
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
// Do any additional setup after loading the view.
|
|
}
|
|
|
|
|
|
|
|
@IBAction func leaveButtonTapped(_ sender: UIButton) {
|
|
ORKPasscodeViewController.removePasscodeFromKeychain()
|
|
performSegue(withIdentifier: "returnToConsent", sender: nil)
|
|
|
|
}
|
|
@IBAction func consentButtonTapped(_ sender: UIButton) {
|
|
let taskViewController = ORKTaskViewController(task: consentPDFViewerTask(), taskRun: nil)
|
|
taskViewController.delegate = self
|
|
taskViewController.modalPresentationStyle = .fullScreen
|
|
present(taskViewController, animated: true, completion: nil)
|
|
|
|
}
|
|
@IBAction func fichaButtonTapped(_ sender: UIButton) {
|
|
let taskViewController = ORKTaskViewController(task: FichaManager.shared.createFichaTask(), taskRun: nil)
|
|
taskViewController.delegate = self
|
|
taskViewController.modalPresentationStyle = .fullScreen
|
|
present(taskViewController, animated: true, completion: nil)
|
|
}
|
|
|
|
@IBAction func testButtonTapped(_ sender: UIButton) {
|
|
let taskViewController = ORKTaskViewController(task: TestManager.shared.createTestTask(), taskRun: nil)
|
|
taskViewController.delegate = self
|
|
taskViewController.modalPresentationStyle = .fullScreen
|
|
present(taskViewController, animated: true, completion: nil)
|
|
}
|
|
|
|
@IBAction func activitiesButtonTapped(_ sender: UIButton) {
|
|
let taskViewController = ORKTaskViewController(task: ActiveTaskManager.shared.createActiveTasks(), taskRun: nil)
|
|
taskViewController.delegate = self
|
|
taskViewController.modalPresentationStyle = .fullScreen
|
|
present(taskViewController, animated: true, completion: nil)
|
|
}
|
|
|
|
func consentPDFViewerTask() -> ORKOrderedTask{
|
|
var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last
|
|
docURL = docURL?.appendingPathComponent("consent.pdf")
|
|
let PDFViewerStep = ORKPDFViewerStep.init(identifier: "ConsentPDFViewer", pdfURL: docURL)
|
|
PDFViewerStep.title = "Consent"
|
|
return ORKOrderedTask(identifier: String("ConsentPDF"), steps: [PDFViewerStep])
|
|
}
|
|
|
|
|
|
func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskFinishReason, error: Error?) {
|
|
// 1. Dismiss the task view controller
|
|
taskViewController.dismiss(animated: true, completion: nil)
|
|
|
|
// 2. Get the task result from the taskViewController
|
|
let taskResult = taskViewController.result
|
|
print("Estructura completa de los resultados: \(taskResult)")
|
|
|
|
|
|
// 3. Verificar que los resultados estén presentes
|
|
if let stepResults = taskResult.results as? [ORKStepResult] {
|
|
// 4. Imprimir los resultados de los pasos
|
|
for stepResult in stepResults {
|
|
print("Identificador del paso: \(stepResult.identifier)")
|
|
|
|
// Dependiendo del tipo de resultado, extraemos la información adecuada
|
|
if let textResult = stepResult.results?.first as? ORKTextQuestionResult {
|
|
// Resultado de una pregunta de texto
|
|
print("Respuesta de \(stepResult.identifier): \(textResult.textAnswer ?? "Sin respuesta")")
|
|
} else if let choiceResult = stepResult.results?.first as? ORKChoiceQuestionResult {
|
|
// Resultado de una pregunta de opción múltiple
|
|
print("Respuesta de \(stepResult.identifier): \(choiceResult.choiceAnswers ?? [])")
|
|
} else if let numericResult = stepResult.results?.first as? ORKNumericQuestionResult {
|
|
// Resultado de una pregunta numérica
|
|
print("Respuesta de \(stepResult.identifier): \(numericResult.numericAnswer ?? 0)")
|
|
}
|
|
}
|
|
}
|
|
|
|
// 4. Ahora intentamos extraer los resultados específicos de cada pregunta usando el identificador
|
|
if let nombreResult = taskResult.result(forIdentifier: "Nombre") as? ORKTextQuestionResult {
|
|
if let nombre = nombreResult.textAnswer {
|
|
print("Nombre: \(nombre)") // Debería imprimir "David"
|
|
} else {
|
|
print("No se encontró respuesta para el nombre.")
|
|
}
|
|
}
|
|
}
|
|
}
|