carekit-apple/CareKit

percentage of completed tasks

ykworld opened this issue · 3 comments

Screen Shot 2022-08-12 at 12 11 15 AM

I want to get the percentage of completed tasks. How can I implement it?

You can do it using OCKAdherenceQuery. If successful, adherence queries return one of three values: noTasks, if there are no tasks; noEvents, if there are no task events (actual occurrences) scheduled for the given interval; or progress with an associated value between 0 and 1, if there are events. If you multiply that value by a 100, you can get the completion percentage.

For example:

let store = OCKStore("MyStore")

func fetchAdherence(for date: Date) {
    let startOfDay = Calendar.current.startOfDay(for: date)
    let endOfDay = Calendar.current.date(bySettingHour: 23, minute: 59, second: 59, of: date)!
    var query = OCKAdherenceQuery(
        taskIDs: [],       // If you want adherence for all tasks. You can add task IDs if you want to check adherence for specific tasks
        dateInterval: DateInterval(start: startOfDay, end: endOfDay)
    )
    query.aggregator = OCKAdherenceAggregator.compareTargetValues

    store.fetchAdherence(query: query) { result in
        switch result {
            case .success(let adherence):
                switch adherence[0] {
                case .noTasks:
                    // handle no tasks
                case .noEvents:
                    // handle no events
                case .progress(let double):
                    let percentage = double * 100
                    // handle that
                }
            case .failure(let error):
                // handle the error
            }
        }
    }

Hope this helps!

You can do it using OCKAdherenceQuery. If successful, adherence queries return one of three values: noTasks, if there are no tasks; noEvents, if there are no task events (actual occurrences) scheduled for the given interval; or progress with an associated value between 0 and 1, if there are events. If you multiply that value by a 100, you can get the completion percentage.

For example:

let store = OCKStore("MyStore")

func fetchAdherence(for date: Date) {
    let startOfDay = Calendar.current.startOfDay(for: date)
    let endOfDay = Calendar.current.date(bySettingHour: 23, minute: 59, second: 59, of: date)!
    var query = OCKAdherenceQuery(
        taskIDs: [],       // If you want adherence for all tasks. You can add task IDs if you want to check adherence for specific tasks
        dateInterval: DateInterval(start: startOfDay, end: endOfDay)
    )
    query.aggregator = OCKAdherenceAggregator.compareTargetValues

    store.fetchAdherence(query: query) { result in
        switch result {
            case .success(let adherence):
                switch adherence[0] {
                case .noTasks:
                    // handle no tasks
                case .noEvents:
                    // handle no events
                case .progress(let double):
                    let percentage = double * 100
                    // handle that
                }
            case .failure(let error):
                // handle the error
            }
        }
    }

Hope this helps!

Thanks a lot!

Hello @ykworld,

I also want to add the OverView section to my app. Can you please guide me on how you append that section to the view?
How did you change the Completion Ring Shape?

Thanks,

  • Majid S.