Skip to content

Commit a8f54c6

Browse files
authored
feat: Add OCKHealthKitPassthroughStore back to watchOS (#140)
1 parent 211f2df commit a8f54c6

File tree

6 files changed

+50
-41
lines changed

6 files changed

+50
-41
lines changed

OCKSample.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@
12751275
repositoryURL = "https://github.com/netreconlab/CareKitEssentials";
12761276
requirement = {
12771277
kind = upToNextMajorVersion;
1278-
minimumVersion = "1.0.0-alpha.44";
1278+
minimumVersion = "1.0.0-alpha.46";
12791279
};
12801280
};
12811281
70202EBF2807333900CF73FB /* XCRemoteSwiftPackageReference "CareKit" */ = {

OCKSample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OCKSample/Main/MainView.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright © 2020 Network Reconnaissance Lab. All rights reserved.
77

88
import CareKit
9+
import CareKitEssentials
910
import CareKitStore
1011
import CareKitUI
1112
import SwiftUI
@@ -44,18 +45,15 @@ struct MainView: View {
4445
}
4546
}
4647
.environment(\.careStore, storeCoordinator)
47-
.onReceive(loginViewModel.$isLoggedOut, perform: { isLoggedOut in
48+
.onChange(of: appDelegate.storeCoordinator) { newStoreCoordinator in
49+
storeCoordinator = newStoreCoordinator
50+
}
51+
.onReceive(loginViewModel.$isLoggedOut) { isLoggedOut in
4852
guard !isLoggedOut else {
4953
updatePath([])
5054
return
5155
}
5256
updatePath([.tabs])
53-
})
54-
.onReceive(appDelegate.$storeCoordinator) { newStoreCoordinator in
55-
guard storeCoordinator !== newStoreCoordinator else {
56-
return
57-
}
58-
storeCoordinator = newStoreCoordinator
5957
}
6058
}
6159

OCKWatchSample Extension/AppDelegate.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ class AppDelegate: NSObject, WKApplicationDelegate, ObservableObject {
2727
self.objectWillChange.send()
2828
}
2929
}
30+
@Published private(set) var storeCoordinator: OCKStoreCoordinator = .init() {
31+
willSet {
32+
StoreCoordinatorKey.defaultValue = newValue
33+
self.objectWillChange.send()
34+
}
35+
}
3036
private(set) var parseRemote: ParseRemote!
37+
private(set) var healthKitStore: OCKHealthKitPassthroughStore!
3138

3239
// MARK: Private read/write properties
3340

@@ -96,13 +103,17 @@ class AppDelegate: NSObject, WKApplicationDelegate, ObservableObject {
96103
WCSession.default.activate()
97104
return
98105
}
99-
parseRemote = try await ParseRemote(uuid: uuid,
100-
auto: false,
101-
subscribeToRemoteUpdates: true,
102-
defaultACL: PCKUtility.getDefaultACL())
103-
let store = OCKStore(name: Constants.watchOSParseCareStoreName,
104-
type: .onDisk(),
105-
remote: parseRemote)
106+
parseRemote = try await ParseRemote(
107+
uuid: uuid,
108+
auto: false,
109+
subscribeToRemoteUpdates: true,
110+
defaultACL: PCKUtility.getDefaultACL()
111+
)
112+
let store = OCKStore(
113+
name: Constants.watchOSParseCareStoreName,
114+
type: .onDisk(),
115+
remote: parseRemote
116+
)
106117
parseRemote?.parseRemoteDelegate = self
107118
sessionDelegate.store = store
108119
self.store = store
@@ -117,6 +128,12 @@ class AppDelegate: NSObject, WKApplicationDelegate, ObservableObject {
117128
self.store = store
118129
}
119130
WCSession.default.activate()
131+
132+
healthKitStore = OCKHealthKitPassthroughStore(store: store)
133+
let storeCoordinator = OCKStoreCoordinator()
134+
storeCoordinator.attach(store: store)
135+
storeCoordinator.attach(eventStore: healthKitStore)
136+
self.storeCoordinator = storeCoordinator
120137
} catch {
121138
Logger.appDelegate.error("Error setting up remote: \(error)")
122139
throw error

OCKWatchSample Extension/Main/Care/CareView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ struct CareView: View {
2929

3030
var body: some View {
3131
ScrollView {
32-
ForEach(orderedEvents) { event in
32+
ForEach(orderedEvents) { event in
3333
if event.result.task.id == TaskID.kegels {
3434
SimpleTaskView(event: event)
35-
} else {
35+
} else {
3636
InstructionsTaskView(event: event)
3737
}
3838
}

OCKWatchSample Extension/Main/MainView.swift

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66
// Copyright © 2022 Network Reconnaissance Lab. All rights reserved.
77
//
88

9-
import SwiftUI
9+
import CareKitEssentials
1010
import CareKitStore
1111
import CareKitUI
12+
import SwiftUI
1213
import os.log
1314

1415
struct MainView: View {
1516
@EnvironmentObject private var appDelegate: AppDelegate
1617
@StateObject private var loginViewModel = LoginViewModel()
1718
@State private var path = [MainViewPath]()
18-
@State private var store = OCKStore(
19-
name: Constants.noCareStoreName,
20-
type: .inMemory
21-
)
19+
@State private var storeCoordinator = OCKStoreCoordinator()
2220

2321
var body: some View {
2422
NavigationStack(path: $path) {
@@ -43,8 +41,19 @@ struct MainView: View {
4341
updatePath([.tabs])
4442
}
4543
}
46-
.environment(\.careStore, store)
47-
.onReceive(loginViewModel.$isLoggedOut, perform: { isLoggedOut in
44+
.environment(\.careStore, storeCoordinator)
45+
.onChange(of: appDelegate.storeCoordinator) { newStoreCoordinator in
46+
Task {
47+
await loginViewModel.checkStatus()
48+
}
49+
storeCoordinator = newStoreCoordinator
50+
guard isSyncingWithRemote else {
51+
updatePath([.tabs])
52+
return
53+
}
54+
updatePath([.tabs])
55+
}
56+
.onReceive(loginViewModel.$isLoggedOut) { isLoggedOut in
4857
guard isSyncingWithRemote else {
4958
updatePath([.tabs])
5059
return
@@ -54,21 +63,6 @@ struct MainView: View {
5463
return
5564
}
5665
updatePath([.tabs])
57-
})
58-
.onReceive(appDelegate.$store) { newStore in
59-
Task {
60-
await loginViewModel.checkStatus()
61-
}
62-
guard let newStore = newStore,
63-
store !== newStore else {
64-
return
65-
}
66-
store = newStore
67-
guard isSyncingWithRemote else {
68-
updatePath([.tabs])
69-
return
70-
}
71-
updatePath([.tabs])
7266
}
7367
}
7468

0 commit comments

Comments
 (0)