Skip to content

Commit

Permalink
refactor: Update dependencies and store code (#134)
Browse files Browse the repository at this point in the history
* refactor: Update dependencies and store code

* essentials update
  • Loading branch information
cbaker6 authored Mar 3, 2025
1 parent e5b081c commit 7b1c8b4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 68 deletions.
4 changes: 2 additions & 2 deletions OCKSample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1245,15 +1245,15 @@
repositoryURL = "https://github.com/netreconlab/CareKitEssentials";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = "1.0.0-alpha.37";
minimumVersion = "1.0.0-alpha.39";
};
};
70202EBF2807333900CF73FB /* XCRemoteSwiftPackageReference "CareKit" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/cbaker6/CareKit.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = "3.0.0-beta.35";
minimumVersion = "3.0.0-beta.40";
};
};
918FDEAD271B3F8F0045A0EF /* XCRemoteSwiftPackageReference "ParseCareKit" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/cbaker6/CareKit.git",
"state" : {
"revision" : "b4424ee9eb8e7b214f7cbe27febf3d510250d33c",
"version" : "3.0.0-beta.35"
"revision" : "4257a8b1e8df6214b10c3d5953f8c090d386b782",
"version" : "3.0.0-beta.40"
}
},
{
"identity" : "carekitessentials",
"kind" : "remoteSourceControl",
"location" : "https://github.com/netreconlab/CareKitEssentials",
"state" : {
"revision" : "c8e199027e6f24f170d67f2b9b3c71ad65b8ab81",
"version" : "1.0.0-alpha.37"
"revision" : "31565b672d953650aed48d70e600c88b287f7b18",
"version" : "1.0.0-alpha.39"
}
},
{
"identity" : "fhirmodels",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/FHIRModels.git",
"state" : {
"revision" : "861afd5816a98d38f86220eab2f812d76cad84a0",
"version" : "0.5.0"
"revision" : "ee8b55c245e92389e05b0a56ad2b84d37c1985e5",
"version" : "0.6.1"
}
},
{
"identity" : "parse-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/netreconlab/Parse-Swift.git",
"state" : {
"revision" : "3f02f591398d6cf6467ecc8ced1e45f5714aa868",
"version" : "5.11.3"
"revision" : "157401b3d89700300af1895911f9c44c4a8bcc29",
"version" : "5.11.5"
}
},
{
Expand All @@ -51,8 +51,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-async-algorithms",
"state" : {
"revision" : "6ae9a051f76b81cc668305ceed5b0e0a7fd93d20",
"version" : "1.0.1"
"revision" : "4c3ea81f81f0a25d0470188459c6d4bf20cf2f97",
"version" : "1.0.3"
}
},
{
Expand Down
24 changes: 11 additions & 13 deletions OCKSample/Extensions/OCKHealthKitPassthroughStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import os.log

extension OCKHealthKitPassthroughStore {

func addTasksIfNotPresent(_ tasks: [OCKHealthKitTask]) async throws {
func addTasksIfNotPresent(_ tasks: [OCKHealthKitTask]) async throws -> [OCKHealthKitTask] {
let tasksToAdd = tasks
let taskIdsToAdd = tasksToAdd.compactMap { $0.id }

Expand All @@ -22,24 +22,22 @@ extension OCKHealthKitPassthroughStore {
query.ids = taskIdsToAdd

let foundTasks = try await fetchTasks(query: query)
var tasksNotInStore = [OCKHealthKitTask]()

// Check results to see if there's a missing task
tasksToAdd.forEach { potentialTask in
if foundTasks.first(where: { $0.id == potentialTask.id }) == nil {
tasksNotInStore.append(potentialTask)
// Find all missing tasks.
let tasksNotInStore = tasks.filter { potentialTask -> Bool in
guard foundTasks.first(where: { $0.id == potentialTask.id }) == nil else {
return false
}
return true
}

// Only add if there's a new task
if tasksNotInStore.count > 0 {
do {
_ = try await addTasks(tasksNotInStore)
Logger.ockHealthKitPassthroughStore.info("Added tasks into HealthKitPassthroughStore!")
} catch {
Logger.ockHealthKitPassthroughStore.error("Error adding HealthKitTasks: \(error)")
}
guard tasksNotInStore.count > 0 else {
return []
}

let addedTasks = try await addTasks(tasksNotInStore)
return addedTasks
}

func populateSampleData() async throws {
Expand Down
57 changes: 14 additions & 43 deletions OCKSample/Extensions/OCKStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import CareKitEssentials
import CareKitStore
import Contacts
import os.log
Expand All @@ -15,60 +16,30 @@ import ParseCareKit

extension OCKStore {

func addTasksIfNotPresent(_ tasks: [OCKTask]) async throws {
let taskIdsToAdd = tasks.compactMap { $0.id }

// Prepare query to see if tasks are already added
var query = OCKTaskQuery(for: Date())
query.ids = taskIdsToAdd

let foundTasks = try await fetchTasks(query: query)
var tasksNotInStore = [OCKTask]()

// Check results to see if there's a missing task
tasks.forEach { potentialTask in
if foundTasks.first(where: { $0.id == potentialTask.id }) == nil {
tasksNotInStore.append(potentialTask)
}
}

// Only add if there's a new task
if tasksNotInStore.count > 0 {
do {
_ = try await addTasks(tasksNotInStore)
Logger.ockStore.info("Added tasks into OCKStore!")
} catch {
Logger.ockStore.error("Error adding tasks: \(error)")
}
}
}

func addContactsIfNotPresent(_ contacts: [OCKContact]) async throws {
func addContactsIfNotPresent(_ contacts: [OCKContact]) async throws -> [OCKContact] {
let contactIdsToAdd = contacts.compactMap { $0.id }

// Prepare query to see if contacts are already added
var query = OCKContactQuery(for: Date())
query.ids = contactIdsToAdd

let foundContacts = try await fetchContacts(query: query)
var contactsNotInStore = [OCKContact]()

// Check results to see if there's a missing task
contacts.forEach { potential in
if foundContacts.first(where: { $0.id == potential.id }) == nil {
contactsNotInStore.append(potential)
// Find all missing tasks.
let contactsNotInStore = contacts.filter { potentialContact -> Bool in
guard foundContacts.first(where: { $0.id == potentialContact.id }) == nil else {
return false
}
return true
}

// Only add if there's a new task
if contactsNotInStore.count > 0 {
do {
_ = try await addContacts(contactsNotInStore)
Logger.ockStore.info("Added contacts into OCKStore!")
} catch {
Logger.ockStore.error("Error adding contacts: \(error)")
}
guard contactsNotInStore.count > 0 else {
return []
}

let addedContacts = try await addContacts(contactsNotInStore)
return addedContacts
}

// Adds tasks and contacts into the store
Expand Down Expand Up @@ -160,7 +131,7 @@ extension OCKStore {
stretch.impactsAdherence = true
stretch.asset = "figure.walk"

try await addTasksIfNotPresent(
_ = try await addTasksIfNotPresent(
[
nausea,
doxylamine,
Expand Down Expand Up @@ -208,7 +179,7 @@ extension OCKStore {
return address
}()

try await addContactsIfNotPresent(
_ = try await addContactsIfNotPresent(
[
contact1,
contact2
Expand Down

0 comments on commit 7b1c8b4

Please sign in to comment.