Skip to content

Commit

Permalink
fix: Update to latest CareKit to prevent crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Jul 7, 2024
1 parent cd6d1b7 commit 38172a3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 32 deletions.
2 changes: 1 addition & 1 deletion OCKSample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@
repositoryURL = "https://github.com/cbaker6/CareKit.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = "3.0.0-beta.10";
minimumVersion = "3.0.0-beta.12";
};
};
918FDEAD271B3F8F0045A0EF /* XCRemoteSwiftPackageReference "ParseCareKit" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/cbaker6/CareKit.git",
"state" : {
"revision" : "075d600052f168d49b542444577cf20033eb9666",
"version" : "3.0.0-beta.10"
"revision" : "671cfa7bc608f5ed985aebef5ebd2b135b7e5a3c",
"version" : "3.0.0-beta.12"
}
},
{
Expand Down
26 changes: 16 additions & 10 deletions OCKSample/Main/Care/CareView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ import SwiftUI
import UIKit

struct CareView: UIViewControllerRepresentable {
private static var query: OCKEventQuery {
var query = OCKEventQuery(for: Date())
query.taskIDs = [TaskID.steps]
return query
}

@Environment(\.appDelegate) private var appDelegate
@Environment(\.careStore) private var careStore
@CareStoreFetchRequest(query: query) private var events
@CareStoreFetchRequest(query: query()) private var events

func makeUIViewController(context: Context) -> some UIViewController {
let viewController = createViewController()
Expand All @@ -31,8 +27,10 @@ struct CareView: UIViewControllerRepresentable {
return navigationController
}

func updateUIViewController(_ uiViewController: UIViewControllerType,
context: Context) {
func updateUIViewController(
_ uiViewController: UIViewControllerType,
context: Context
) {
guard let navigationController = uiViewController as? UINavigationController,
let careViewController = navigationController.viewControllers.first as? CareViewController else {
Logger.feed.error("CareView should have been a UINavigationController")
Expand All @@ -48,8 +46,16 @@ struct CareView: UIViewControllerRepresentable {
}

func createViewController() -> UIViewController {
CareViewController(store: careStore,
events: events)
CareViewController(
store: careStore,
events: events
)
}

static func query() -> OCKEventQuery {
var query = OCKEventQuery(for: Date())
query.taskIDs = [TaskID.steps]
return query
}
}

Expand Down
32 changes: 21 additions & 11 deletions OCKSample/Main/Care/CareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ class CareViewController: OCKDailyPageViewController {
///
/// - Parameter store: The store from which to query the tasks.
/// - Parameter computeProgress: Used to compute the combined progress for a series of CareKit events.
init(store: OCKAnyStoreProtocol,
events: CareStoreFetchedResults<OCKAnyEvent, OCKEventQuery>? = nil,
computeProgress: @escaping (OCKAnyEvent) -> CareTaskProgress = { event in
event.computeProgress(by: .checkingOutcomeExists)
}) {
init(
store: OCKAnyStoreProtocol,
events: CareStoreFetchedResults<OCKAnyEvent, OCKEventQuery>? = nil,
computeProgress: @escaping (OCKAnyEvent) -> CareTaskProgress = { event in
event.computeProgress(by: .checkingOutcomeExists)
}
) {
super.init(store: store, computeProgress: computeProgress)
self.events = events
}
Expand Down Expand Up @@ -147,8 +149,11 @@ class CareViewController: OCKDailyPageViewController {
This will be called each time the selected date changes.
Use this as an opportunity to rebuild the content shown to the user.
*/
override func dailyPageViewController(_ dailyPageViewController: OCKDailyPageViewController,
prepare listViewController: OCKListViewController, for date: Date) {
override func dailyPageViewController(
_ dailyPageViewController: OCKDailyPageViewController,
prepare listViewController: OCKListViewController,
for date: Date
) {
Task {
do {
let tasks = try await fetchTasks(on: date)
Expand Down Expand Up @@ -205,8 +210,10 @@ class CareViewController: OCKDailyPageViewController {
events?.last(where: { $0.result.task.id == taskId })
}

private func taskViewController(for task: OCKAnyTask,
on date: Date) -> [UIViewController]? {
private func taskViewController(
for task: OCKAnyTask,
on date: Date
) -> [UIViewController]? {

var query = OCKEventQuery(for: Date())
query.taskIDs = [task.id]
Expand All @@ -216,8 +223,11 @@ class CareViewController: OCKDailyPageViewController {
guard let event = getStoreFetchRequestEvent(for: task.id) else {
return nil
}
let view = NumericProgressTaskView<_NumericProgressTaskViewHeader>(event: event, numberFormatter: .none)
.careKitStyle(CustomStylerKey.defaultValue)
let view = NumericProgressTaskView<_NumericProgressTaskViewHeader>(
event: event,
numberFormatter: .none
)
.careKitStyle(CustomStylerKey.defaultValue)

return [view.formattedHostingController()]

Expand Down
9 changes: 7 additions & 2 deletions OCKSample/Main/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import os.log
import SwiftUI

struct ProfileView: View {
private static var query = OCKPatientQuery(for: Date())
@CareStoreFetchRequest(query: query) private var patients

@CareStoreFetchRequest(query: query()) private var patients
@StateObject private var viewModel = ProfileViewModel()
@ObservedObject var loginViewModel: LoginViewModel

Expand Down Expand Up @@ -79,6 +79,11 @@ struct ProfileView: View {
viewModel.updatePatient(publishedPatient.result)
}
}

static func query() -> OCKPatientQuery {
OCKPatientQuery(for: Date())
}

}

struct ProfileView_Previews: PreviewProvider {
Expand Down
14 changes: 8 additions & 6 deletions OCKWatchSample Extension/Main/Care/CareView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ import SwiftUI
import os.log

struct CareView: View {
private static var query: OCKEventQuery {
var query = OCKEventQuery(for: Date())
query.taskIDs = [TaskID.stretch, TaskID.kegels]
return query
}
@CareStoreFetchRequest(query: query) private var events

@CareStoreFetchRequest(query: query()) private var events

var body: some View {
ScrollView {
Expand All @@ -31,6 +27,12 @@ struct CareView: View {
}
}
}

static func query() -> OCKEventQuery {
var query = OCKEventQuery(for: Date())
query.taskIDs = [TaskID.stretch, TaskID.kegels]
return query
}
}

struct ContentView_Previews: PreviewProvider {
Expand Down

0 comments on commit 38172a3

Please sign in to comment.