Coder Social home page Coder Social logo

Trying to schedule a notification based on the caloriesBurned only if they are less than a certain amount (with query to the health store) about carekit HOT 6 OPEN

fedetop01 avatar fedetop01 commented on August 25, 2024
Trying to schedule a notification based on the caloriesBurned only if they are less than a certain amount (with query to the health store)

from carekit.

Comments (6)

gavirawson-apple avatar gavirawson-apple commented on August 25, 2024

You might want to consider using the HealthKit linked tasks in CareKit. The task can be scheduled to occur every day, and will pull data from HealthKit to populate its outcome. The scheduling logic might look something like this:

func createCalorieTask() -> OCKHealthKitTask {

    let calorieLinkage = OCKHealthKitLinkage(
        quantityIdentifier: .activeEnergyBurned,
        quantityType: .cumulative,
        unit: .kilocalorie()
    )

    let startOfDay = Calendar.current.startOfDay(for: Date())

    let dailySchedule = OCKScheduleElement(
        start: startOfDay,
        end: nil,
        interval: DateComponents(day: 1),
        duration: .allDay,
        targetValues: [OCKOutcomeValue(300)]
    )

    let calorieTask = OCKHealthKitTask(
        id: "calories",
        title: "Burn Calories",
        carePlanUUID: nil,
        schedule: OCKSchedule(composing: [dailySchedule]),
        healthKitLinkage: calorieLinkage
    )

    return calorieTask
}

Now in terms of scheduling a notification, you can choose to either schedule or "unschedule" that 8PM notification whenever the outcome for the task changes.

var query = OCKEventQuery(for: anIntervalEncompassingAFewEvents)
query.taskIDs = ["calories"]

for events in store.anyEvents(matching: query) {

    // schedule or unschedule notifications based on the
    // progress for each event
}

from carekit.

fedetop01 avatar fedetop01 commented on August 25, 2024

Hi, thank you for your response. However I still have problems with the notification, since OCKEventQuery doesn't have a member taskIDs

from carekit.

fedetop01 avatar fedetop01 commented on August 25, 2024

I have problem with the schedule of this task you provided, and I'm struggling to understand how should I use to "schedule and unschedule" the notification based on the calories burned during the day

from carekit.

gavirawson-apple avatar gavirawson-apple commented on August 25, 2024

Hi, thank you for your response. However I still have problems with the notification, since OCKEventQuery doesn't have a member taskIDs

Which branch of CareKit are you using? The main branch should have that property.

I have problem with the schedule of this task you provided

What problem are you running into?

I'm struggling to understand how should I use to "schedule and unschedule" the notification based on the calories burned during the day

You can begin by checking the progress for an event. If the progress is not completed, schedule a notification for 8PM. If the progress is completed, remove the 8PM notification.

from carekit.

fedetop01 avatar fedetop01 commented on August 25, 2024

`func createCalorieTask() -> OCKHealthKitTask {

let calorieLinkage = OCKHealthKitLinkage(
    quantityIdentifier: .activeEnergyBurned,
    quantityType: .cumulative,
    unit: .kilocalorie()
)

let startOfDay = Calendar.current.startOfDay(for: Date())

let dailySchedule = OCKScheduleElement(
    start: startOfDay,
    end: nil,
    interval: DateComponents(day: 1),
    duration: .allDay,
    targetValues: [OCKOutcomeValue(300)]
)

let calorieTask = OCKHealthKitTask(
    id: "calories",
    title: "Burn Calories",
    carePlanUUID: nil,
    schedule: OCKSchedule(composing: [dailySchedule]),
    healthKitLinkage: calorieLinkage
)

return calorieTask

}`
this task should be added to the healthkitstore right? And how do I check the progress to schedule the notification? and the OCKEventQuery(for: anIntervalEncompassingAFewEvents) gives me problem, too

Which branch of CareKit are you using? The main branch should have that property.
it must be 2.1 version

from carekit.

gavirawson-apple avatar gavirawson-apple commented on August 25, 2024

this task should be added to the healthkitstore right?

Yep! The OCKHealthKitPassthroughStore or an OCKStoreCoordinator.

And how do I check the progress to schedule the notification?

On 2.1, fetch the event for the task and check to see if the outcome value matches the target value for the schedule (which looks to be 300 calories based on the snippet above). To check compare those two, see OCKAnyEvent.outcome and OCKAnyEvent.scheduleEvent

OCKEventQuery(for: anIntervalEncompassingAFewEvents) gives me problem

That initializer expects a single date, not a date interval. You can use this initializer instead - OCKEventQuery(dateInterval:)

from carekit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.