Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Jul 3, 2024
1 parent d466847 commit f9155c5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CareKitEssentials.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@
isa = PBXGroup;
children = (
OBJ_26 /* Calendar+Dates.swift */,
700DA9062C2A66BF00435E2C /* Logger.swift */,
911BDB272A11C491004F8442 /* CGFloat.swift */,
OBJ_32 /* CustomLinearCareTaskProgress.swift */,
OBJ_27 /* Image.swift */,
700DA9062C2A66BF00435E2C /* Logger.swift */,
OBJ_29 /* OCKAnyEvent.swift */,
OBJ_28 /* OCKAnyEvent+CustomStringConvertable.swift */,
OBJ_30 /* OCKAnyOutcome.swift */,
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,18 @@
![Codecov](https://codecov.io/gh/netreconlab/CareKitEssentials/branches/main/graph/badge.svg)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/netreconlab/ParseCareKit/#license)

Provides essential cards, viewa, models, protocols, and extentions to expedite building [CareKit](https://github.com/carekit-apple/CareKit) based applications.
Provides essential cards, views, models, protocols, and extentions to expedite building [CareKit](https://github.com/carekit-apple/CareKit) based applications.

## Entensions
A number of public extensions are available to make using CareKit easier. All of the extensions can be found in the [Extensions](https://github.com/netreconlab/CareKitEssentials/tree/main/Sources/CareKitEssentials/Extensions) folder.

## Usage
You can create SwiftUI views that conform to [CareKitEssentialView](https://github.com/netreconlab/CareKitEssentials/blob/main/Sources/CareKitEssentials/Cards/Shared/CareKitEssentialView.swift) to obtain a number of convenience methods for saving and deleting outcomes. The following views are based on `CareKitEssentialView`:

### iOS
[SliderLogTaskView](https://github.com/netreconlab/CareKitEssentials/blob/main/Sources/CareKitEssentials/Cards/iOS/SliderLog/SliderLogTaskView.swift) can be used to quickly create a slider view
<img width="342" alt="image" src="https://github.com/netreconlab/CareKitEssentials/assets/8621344/3efb4226-50e2-41e1-beef-91bc84cc7d63">

### watchOS
[DigitalCrownView](https://github.com/netreconlab/CareKitEssentials/blob/main/Sources/CareKitEssentials/Cards/watchOS/DigitalCrown/DigitalCrownView.swift) can be used to quickly create a view that responds to the crown
<img width="332" alt="image" src="https://github.com/netreconlab/CareKitEssentials/assets/8621344/02023682-75f4-4dff-a575-fa3ffd213cc3">
75 changes: 26 additions & 49 deletions Sources/CareKitEssentials/Cards/Shared/CareKitEssentialView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ public protocol CareKitEssentialView: View {
with values: [OCKOutcomeValue]?
) async throws

/// Update an `OCKAnyEvent` with new a `OCKOutcome`.
/// Save a new `OCKAnyOutcome`.
/// - Parameters:
/// - event: The event to update.
/// - outcome: A new `OCKOutcome`.
/// - Throws: An error if the outcome values cannot be updated.
func updateEvent(
_ event: OCKAnyEvent,
with outcome: OCKOutcome?
/// - outcome: A new `OCKAnyOutcome`.
/// - Throws: An error if the outcome cannot be updated.
func saveOutcome(
_ outcome: OCKAnyOutcome
) async throws

/// Create an `OCKEventQuery` constrained to a set of `taskIDs` on a particular date.
Expand All @@ -50,43 +48,31 @@ public protocol CareKitEssentialView: View {

public extension CareKitEssentialView {

func deleteEventOutcome(_ event: OCKAnyEvent) async throws {
guard let outcome = event.outcome else {
throw CareKitEssentialsError.errorString("The event does not contain an outcome: \(event)")
}
_ = try await careStore.deleteAnyOutcome(outcome)
}

func updateEvent(
_ event: OCKAnyEvent,
with values: [OCKOutcomeValue]?
) async throws {
guard let values = values else {
// Attempts to delete outcome if it already exists.
_ = try await self.saveOutcomeValues(
[],
event: event,
store: careStore
)
try await deleteEventOutcome(event)
return
}
_ = try await self.appendOutcomeValues(
values,
event: event,
store: careStore
event: event
)
}

func updateEvent(
_ event: OCKAnyEvent,
with outcome: OCKOutcome?
func saveOutcome(
_ outcome: OCKAnyOutcome
) async throws {
guard let outcome = outcome else {
guard let task = event.task as? OCKAnyVersionableTask else {
throw CareKitEssentialsError.errorString("Cannot make outcome for event: \(event)")
}
let outcome = OCKOutcome(
taskUUID: task.uuid,
taskOccurrenceIndex: event.scheduleEvent.occurrence,
values: []
)
// Attempts to set the latest outcome values to an empty array.
_ = try await careStore.deleteAnyOutcome(outcome)
return
}
_ = try await careStore.addAnyOutcome(outcome)
}

Expand All @@ -98,22 +84,20 @@ public extension CareKitEssentialView {
/// Otherwise a new `OCKOutcome` is created with the respective outcome values.
func appendOutcomeValues(
_ values: [OCKOutcomeValue],
event: OCKAnyEvent,
store: OCKAnyStoreProtocol
event: OCKAnyEvent
) async throws {

// Update the outcome with the new value
guard var outcome = event.outcome else {
let outcome = createOutcomeWithValues(
values,
event: event,
store: store
event: event
)
_ = try await store.addAnyOutcome(outcome)
_ = try await careStore.addAnyOutcome(outcome)
return
}
outcome.values.append(contentsOf: values)
_ = try await store.updateAnyOutcome(outcome)
_ = try await careStore.updateAnyOutcome(outcome)
return
}

Expand All @@ -124,18 +108,13 @@ public extension CareKitEssentialView {
/// - Note: Setting `values` to an empty array will delete the current `OCKOutcome` if it currently exists.
func saveOutcomeValues(
_ values: [OCKOutcomeValue],
event: OCKAnyEvent,
store: OCKAnyStoreProtocol
event: OCKAnyEvent
) async throws {

// Check if outcome values need to be updated.
guard !values.isEmpty else {
// If the event has already been completed
guard let oldOutcome = event.outcome else {
return
}
// Delete the outcome, and create a new one.
_ = try await store.deleteAnyOutcome(oldOutcome)
try await deleteEventOutcome(event)
return
}

Expand All @@ -144,24 +123,22 @@ public extension CareKitEssentialView {
// Create a new outcome with the new values.
let outcome = createOutcomeWithValues(
values,
event: event,
store: store
event: event
)
_ = try await store.addAnyOutcome(outcome)
_ = try await careStore.addAnyOutcome(outcome)
return
}
// Update the outcome with the new values.
currentOutcome.values = values
_ = try await store.updateAnyOutcome(currentOutcome)
_ = try await careStore.updateAnyOutcome(currentOutcome)
}

/// Create an outcome for an event with the given outcome values.
/// - Parameters:
/// - values: The outcome values to attach to the outcome.
func createOutcomeWithValues(
_ values: [OCKOutcomeValue],
event: OCKAnyEvent,
store: OCKAnyStoreProtocol
event: OCKAnyEvent
) -> OCKAnyOutcome {
let outcome = OCKOutcome(
taskUUID: event.task.uuid ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public extension LabeledValueTaskView where Header == InformationHeaderView {
/// - Parameters:
/// - event: The data that appears in the view.
/// - numberFormatter: An object that formats the progress and target values.
init(event: CareStoreFetchedResult<OCKAnyEvent>,
numberFormatter: NumberFormatter? = nil) {
init(
event: CareStoreFetchedResult<OCKAnyEvent>,
numberFormatter: NumberFormatter? = nil
) {

let currentEvent = event.result

Expand Down

0 comments on commit f9155c5

Please sign in to comment.