Skip to content

Commit

Permalink
feat: Add an OCKOutcome sorting extension
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Jul 8, 2024
1 parent 7cc843d commit 4a896dc
Show file tree
Hide file tree
Showing 12 changed files with 680 additions and 10 deletions.
282 changes: 278 additions & 4 deletions CareKitEssentials.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

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 Sources/CareKitEssentials/Extensions/OCKOutcome+Sequence.swift
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 TestHost/Assets.xcassets/AccentColor.colorset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
63 changes: 63 additions & 0 deletions TestHost/Assets.xcassets/AppIcon.appiconset/Contents.json
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
}
}
6 changes: 6 additions & 0 deletions TestHost/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
24 changes: 24 additions & 0 deletions TestHost/ContentView.swift
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()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
10 changes: 10 additions & 0 deletions TestHost/TestHost.entitlements
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>
17 changes: 17 additions & 0 deletions TestHost/TestHostApp.swift
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()
}
}
}
6 changes: 0 additions & 6 deletions Tests/CareKitEssentialsTests/CareKitEssentialsTests.swift

This file was deleted.

Loading

0 comments on commit 4a896dc

Please sign in to comment.