-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add EventQueryView wrappers (#30)
* feat: Add EventQueryView wrappers * Update README.md
- Loading branch information
Showing
20 changed files
with
425 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Sources/CareKitEssentials/Cards/Shared/Chart/CareEssentialChartView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Sources/CareKitEssentials/Cards/Shared/Chart/Data/CKEDataSeries.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Sources/CareKitEssentials/Cards/Shared/Chart/Data/CKEDataSeriesConfiguration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Sources/CareKitEssentials/Cards/Shared/Chart/Data/CKEPoint.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Sources/CareKitEssentials/Cards/Shared/Chart/Progress/TemporalProgress.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Sources/CareKitEssentials/Cards/Shared/Chart/Progress/TemporalTaskProgress.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
Sources/CareKitEssentials/Cards/Shared/EventViews/EventQueryContentView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// EventQueryContentView.swift | ||
// CareKitEssentials | ||
// | ||
// Created by Corey Baker on 12/10/24. | ||
// Copyright © 2024 NetReconLab. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import CareKit | ||
import CareKitStore | ||
import CareKitUI | ||
import os.log | ||
|
||
/// A view that wraps any view that is `EventWithContentViewable` and provides | ||
/// the respective view with an up-to-date latest event matching the | ||
/// specified event query. | ||
/// - note: This view is useful to wrap around SwiftUI views that will be shown in | ||
/// UIKit view controllers. | ||
/// - important: This view requires `OCKAnyEvent` to conform to `Hashable` | ||
/// and `Equatable` to update view properly. | ||
public struct EventQueryContentView<CareView: EventWithContentViewable>: View { | ||
@Environment(\.careStore) private var store | ||
@CareStoreFetchRequest(query: defaultQuery) var events | ||
|
||
var query: OCKEventQuery | ||
@ViewBuilder let content: () -> CareView.Content | ||
|
||
public var body: some View { | ||
CareView( | ||
event: event.result, | ||
store: store, | ||
content: content | ||
) | ||
.onAppear { | ||
events.query = query | ||
} | ||
} | ||
|
||
private static var defaultQuery: OCKEventQuery { | ||
var query = OCKEventQuery(for: Date()) | ||
query.taskIDs = [""] | ||
return query | ||
} | ||
|
||
private var event: CareStoreFetchedResult<OCKAnyEvent> { | ||
guard let latestEvent = events.latest.last else { | ||
// Create empty result to be fill space in UIKit | ||
let taskID = query.taskIDs.first ?? "No taskID supplied in query" | ||
let emptyEvent = OCKAnyEvent.createDummyEvent( | ||
withTaskID: "Task id \"\(taskID)\" not found" | ||
) | ||
let emptyResult = CareStoreFetchedResult<OCKAnyEvent>( | ||
id: UUID().uuidString, | ||
result: emptyEvent, | ||
store: store | ||
) | ||
return emptyResult | ||
} | ||
return latestEvent | ||
} | ||
|
||
/// Create an instance of this view. | ||
/// - Parameters: | ||
/// - query: A query that limits which events will be returned when fetching. | ||
/// - content: Additonal view content to be displayed. | ||
public init( | ||
query: OCKEventQuery, | ||
content: @escaping () -> CareView.Content | ||
) { | ||
self.query = query | ||
self.content = content | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
Sources/CareKitEssentials/Cards/Shared/EventViews/EventQueryView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// EventQueryView.swift | ||
// CareKitEssentials | ||
// | ||
// Created by Corey Baker on 12/10/24. | ||
// Copyright © 2024 NetReconLab. All rights reserved. | ||
// | ||
|
||
import CareKit | ||
import CareKitStore | ||
import CareKitUI | ||
import SwiftUI | ||
|
||
/// A view that wraps any view that is `EventViewable` and provides | ||
/// the respective view with an up-to-date latest event matching the | ||
/// specified event query. | ||
/// - note: This view is useful to wrap around SwiftUI views that will be shown in | ||
/// UIKit view controllers. | ||
/// - important: This view requires `OCKAnyEvent` to conform to `Hashable` | ||
/// and `Equatable` to update view properly. | ||
public struct EventQueryView<CareView: EventViewable>: View { | ||
@Environment(\.careStore) private var store | ||
@CareStoreFetchRequest(query: defaultQuery) var events | ||
|
||
var query: OCKEventQuery | ||
|
||
public var body: some View { | ||
CareView( | ||
event: event, | ||
store: store | ||
) | ||
.onAppear { | ||
events.query = query | ||
} | ||
} | ||
|
||
private static var defaultQuery: OCKEventQuery { | ||
var query = OCKEventQuery(for: Date()) | ||
query.taskIDs = [""] | ||
return query | ||
} | ||
|
||
private var event: OCKAnyEvent { | ||
guard let latestEvent = events.latest.last?.result else { | ||
// Create empty result to be fill space in UIKit | ||
let taskID = query.taskIDs.first ?? "No taskID supplied in query" | ||
let emptyEvent = OCKAnyEvent.createDummyEvent( | ||
withTaskID: "Task id \"\(taskID)\" not found" | ||
) | ||
return emptyEvent | ||
} | ||
return latestEvent | ||
} | ||
|
||
/// Create an instance of this view. | ||
/// - Parameters: | ||
/// - query: A query that limits which events will be returned when fetching. | ||
public init(query: OCKEventQuery) { | ||
self.query = query | ||
} | ||
} | ||
|
||
#Preview { | ||
var query: OCKEventQuery { | ||
var query = OCKEventQuery(for: Date()) | ||
query.taskIDs = [TaskID.doxylamine] | ||
|
||
return query | ||
} | ||
|
||
VStack { | ||
EventQueryView<SimpleTaskView>( | ||
query: query | ||
) | ||
Divider() | ||
EventQueryView<InstructionsTaskView>( | ||
query: query | ||
) | ||
#if !os(watchOS) | ||
Divider() | ||
EventQueryView<LabeledValueTaskView>( | ||
query: query | ||
) | ||
Divider() | ||
EventQueryView<NumericProgressTaskView>( | ||
query: query | ||
) | ||
#endif | ||
} | ||
.environment(\.careStore, Utility.createPreviewStore()) | ||
.accentColor(.red) | ||
.careKitStyle(OCKStyle()) | ||
.padding() | ||
} |
29 changes: 29 additions & 0 deletions
29
...KitEssentials/Cards/Shared/Extensions/InstructionsTaskView+CareStoreFetchedViewable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// InstructionsTaskView+CareStoreFetchedViewable.swift | ||
// CareKitEssentials | ||
// | ||
// Created by Corey Baker on 12/23/24. | ||
// Copyright © 2024 Network Reconnaissance Lab. All rights reserved. | ||
// | ||
|
||
import CareKit | ||
import CareKitStore | ||
import CareKitUI | ||
import Foundation | ||
import SwiftUI | ||
import os.log | ||
|
||
extension InstructionsTaskView: EventViewable where Header == InformationHeaderView { | ||
public init?( | ||
event: OCKAnyEvent, | ||
store: OCKAnyStoreProtocol | ||
) { | ||
self.init( | ||
event: event, | ||
store: store, | ||
onError: { error in | ||
Logger.instructionsTaskView.error("\(error)") | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.