-
-
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 an OCKOutcome sorting extension
- Loading branch information
Showing
12 changed files
with
680 additions
and
10 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
CareKitEssentials.xcodeproj/xcshareddata/xcschemes/TestHost.xcscheme
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,78 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1540" | ||
version = "1.7"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES" | ||
buildArchitectures = "Automatic"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "70C422CE2C3B6C7600E6DC51" | ||
BuildableName = "TestHost.app" | ||
BlueprintName = "TestHost" | ||
ReferencedContainer = "container:CareKitEssentials.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
shouldAutocreateTestPlan = "YES"> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
<BuildableProductRunnable | ||
runnableDebuggingMode = "0"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "70C422CE2C3B6C7600E6DC51" | ||
BuildableName = "TestHost.app" | ||
BlueprintName = "TestHost" | ||
ReferencedContainer = "container:CareKitEssentials.xcodeproj"> | ||
</BuildableReference> | ||
</BuildableProductRunnable> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<BuildableProductRunnable | ||
runnableDebuggingMode = "0"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "70C422CE2C3B6C7600E6DC51" | ||
BuildableName = "TestHost.app" | ||
BlueprintName = "TestHost" | ||
ReferencedContainer = "container:CareKitEssentials.xcodeproj"> | ||
</BuildableReference> | ||
</BuildableProductRunnable> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
88 changes: 88 additions & 0 deletions
88
Sources/CareKitEssentials/Extensions/OCKOutcome+Sequence.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,88 @@ | ||
// | ||
// OCKOutcome+Sequence.swift | ||
// CareKitEssentials | ||
// | ||
// Created by Corey Baker on 7/7/24. | ||
// | ||
|
||
import CareKitStore | ||
import Foundation | ||
|
||
public extension Sequence where Element == OCKOutcome { | ||
|
||
/** | ||
Returns the `OCKOutcome` sorted in order from newest to oldest with respect to a given `KeyPath`. | ||
When necessary, can specify a cutoff value for the respective `KeyPath` to be less than or equal to. | ||
- parameter keyPath: An optional `Comparable` `KeyPath` to sort the `OCKOutcome`'s by. | ||
- parameter lessThanEqualTo: The value that the `keyPath` of all `OCKOutcome`'s should | ||
be less than or equal to. If this value is `nil`, the | ||
- returns: Returns an array of `OCKOutcome` sorted from newest to oldest with respect to `keyPath`. | ||
- throws: An error when the `keyPath` cannot be unwrapped for any of the `OCKOutcome` values | ||
in the array. | ||
*/ | ||
func sortedNewestToOldest<V>( | ||
_ keyPath: KeyPath<Element, V?>, | ||
lessThanEqualTo value: V? = nil | ||
) throws -> [Element] where V: Comparable { | ||
let outcomes = try compactMap { outcome -> OCKOutcome? in | ||
guard let outcomeKeyValue = outcome[keyPath: keyPath] else { | ||
throw CareKitEssentialsError.couldntUnwrapRequiredField | ||
} | ||
// If there's a value to compare to, check that the element | ||
// is less than or equal to this value. | ||
if let value = value { | ||
guard outcomeKeyValue <= value else { | ||
return nil | ||
} | ||
return outcome | ||
} | ||
return outcome | ||
} | ||
let sortedOutcomes = try outcomes.sorted(by: { | ||
guard let firstKeyValue = $0[keyPath: keyPath], | ||
let secondKeyValue = $1[keyPath: keyPath] else { | ||
// Should never occur due to compactMap above | ||
throw CareKitEssentialsError.couldntUnwrapRequiredField | ||
} | ||
return firstKeyValue > secondKeyValue | ||
}) | ||
|
||
return sortedOutcomes | ||
} | ||
|
||
/** | ||
Returns the `OCKOutcome` sorted in order from newest to oldest with respect to a given `KeyPath`. | ||
When necessary, can specify a cutoff value for the respective `KeyPath` to be less than or equal to. | ||
- parameter keyPath: A `Comparable` `KeyPath` to sort the `OCKOutcome`'s by. | ||
- parameter lessThanEqualTo: The value that the `keyPath` of all `OCKOutcome`'s should | ||
be less than or equal to. If this value is `nil`, the | ||
- returns: Returns an array of `OCKOutcome` sorted from newest to oldest with respect to `keyPath`. | ||
*/ | ||
func sortedNewestToOldest<V>( | ||
_ keyPath: KeyPath<Element, V>, | ||
lessThanEqualTo value: V? = nil | ||
) -> [Element] where V: Comparable { | ||
let outcomes = compactMap { outcome -> OCKOutcome? in | ||
let outcomeKeyValue = outcome[keyPath: keyPath] | ||
|
||
// If there's a value to compare to, check that the element | ||
// is less than or equal to this value. | ||
if let value = value { | ||
guard outcomeKeyValue <= value else { | ||
return nil | ||
} | ||
return outcome | ||
} | ||
return outcome | ||
} | ||
let sortedOutcomes = outcomes.sorted(by: { | ||
let firstKeyValue = $0[keyPath: keyPath] | ||
let secondKeyValue = $1[keyPath: keyPath] | ||
return firstKeyValue > secondKeyValue | ||
}) | ||
|
||
return sortedOutcomes | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
TestHost/Assets.xcassets/AccentColor.colorset/Contents.json
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,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
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,63 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "1x", | ||
"size" : "16x16" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "2x", | ||
"size" : "16x16" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "1x", | ||
"size" : "32x32" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "2x", | ||
"size" : "32x32" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "1x", | ||
"size" : "128x128" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "2x", | ||
"size" : "128x128" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "1x", | ||
"size" : "256x256" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "2x", | ||
"size" : "256x256" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "1x", | ||
"size" : "512x512" | ||
}, | ||
{ | ||
"idiom" : "mac", | ||
"scale" : "2x", | ||
"size" : "512x512" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
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,24 @@ | ||
// | ||
// ContentView.swift | ||
// TestHost | ||
// | ||
// Created by Corey Baker on 7/7/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ContentView: View { | ||
var body: some View { | ||
VStack { | ||
Image(systemName: "globe") | ||
.imageScale(.large) | ||
.foregroundStyle(.tint) | ||
Text("Hello, world!") | ||
} | ||
.padding() | ||
} | ||
} | ||
|
||
#Preview { | ||
ContentView() | ||
} |
6 changes: 6 additions & 0 deletions
6
TestHost/Preview Content/Preview Assets.xcassets/Contents.json
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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
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,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>com.apple.security.app-sandbox</key> | ||
<true/> | ||
<key>com.apple.security.files.user-selected.read-only</key> | ||
<true/> | ||
</dict> | ||
</plist> |
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,17 @@ | ||
// | ||
// TestHostApp.swift | ||
// TestHost | ||
// | ||
// Created by Corey Baker on 7/7/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@main | ||
struct TestHostApp: App { | ||
var body: some Scene { | ||
WindowGroup { | ||
ContentView() | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.