carekit-apple / CareKit

CareKit is an open source software framework for creating apps that help people better understand and manage their health.
https://www.researchandcare.org
Other
2.41k stars 443 forks source link

Problems integrating HealthKit data into an OCKCartesianChartViewController #683

Closed federicatopazio closed 1 year ago

federicatopazio commented 1 year ago

Hi, I was following the recover app tutorials from WWDC21, and I was wondering if there was a way of taking the sleep data directly from the healthApp (HealthKit). However, I'm unable to understand how to integrate this data automatically into the "barChart" he creates where he compares sleep data and pain data, the user manually puts by answering the daily forms:

`let painSeries = OCKDataSeriesConfiguration( taskID: TaskIDs.checkIn, legendTitle: "Pain (1-10)", gradientStartColor: #colorLiteral(red: 1, green: 0.462745098, blue: 0.368627451, alpha: 1), gradientEndColor: #colorLiteral(red: 1, green: 0.462745098, blue: 0.368627451, alpha: 1), markerSize: 10, eventAggregator: .custom({ events in events .first? .answer(kind: Surveys.checkInPainItemIdentifier) ?? 0 }) )

    let sleepSeries = OCKDataSeriesConfiguration(
        taskID: TaskIDs.checkIn,
        legendTitle: "Sleep (hours)",
        gradientStartColor: UIColor.systemBlue,
        gradientEndColor: UIColor.systemBlue,
        markerSize: 10,
        eventAggregator: .custom({ events in
            events
                .first?
                .answer(kind: Surveys.checkInSleepItemIdentifier)
            ?? 0
        })
    )

    let barChart = OCKCartesianChartViewController(
        plotType: .bar,
        selectedDate: Date(),
        configurations: [painSeries, sleepSeries],
        storeManager: storeManager
    )

    appendViewController(barChart, animated: false)`

Furthermore, if there was also a way of displaying the sleep form only if the user did not register any sleep data into the health application, and so how to change the code here: `let sleepAnswerFormat = ORKAnswerFormat.scale( withMaximumValue: 12, minimumValue: 0, defaultValue: 0, step: 1, vertical: false, maximumValueDescription: nil, minimumValueDescription: nil ) let sleepItem = ORKFormItem( identifier: checkInSleepItemIdentifier, text: "How many hours of sleep did you get last night?", answerFormat: sleepAnswerFormat ) sleepItem.isOptional = false

    let formStep = ORKFormStep(
        identifier: checkInFormIdentifier,
        title: "Check In",
        text: "Please answer the following questions."
    )
    formStep.formItems = [painItem, sleepItem]
    formStep.isOptional = false

    let surveyTask = ORKOrderedTask(
        identifier: checkInIdentifier,
        steps: [formStep]
    )`

but the main focus of my question is if there is a way to integrate sleep data into the barChart, using an OCKCartesianChartViewController.

gavirawson-apple commented 1 year ago

Hi! Addressing your first question, are you looking to pull sleep analysis data from HealthKit? Unfortunately that's a category type and at the moment CareKit only supports linking to quantity types in HealthKit. If there are use cases for linking to category types we can look into how that might fit into the API. It does seem like there are use cases, say if a user has a task to "sleep 8 hours a night".

Given that limitation, I would recommend using HealthKit APIs to read the sleep analysis data and map it to a CareKit chart. The HKAnchoredObjectQuery is a great API to start with. You could also consider mapping the data to a Swift Chart if you want a more custom visualization.