Skip to content

Commit

Permalink
feat: Add an OCKAnyOutcome sorting extension (#12)
Browse files Browse the repository at this point in the history
* feat: Add an OCKOutcome sorting extension

* Update protocol extenstion to OCKAnyOutcome

* exclude some tests
  • Loading branch information
cbaker6 authored Jul 8, 2024
1 parent 7cc843d commit d810768
Show file tree
Hide file tree
Showing 13 changed files with 691 additions and 12 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ jobs:
runs-on: macos-14
strategy:
matrix:
destination: ['platform=iOS\ Simulator,name=iPhone\ 14\ Pro\ Max', 'platform=watchOS\ Simulator,name=Apple\ Watch\ Series\ 5\ \(40mm\)', 'platform=macOS CODE_SIGN_IDENTITY=""', 'platform=visionOS\ Simulator,OS=1.2,name=Apple\ Vision\ Pro CODE_SIGN_IDENTITY=""']
action: ['test']
destination: ['platform=iOS\ Simulator,name=iPhone\ 15\ Pro\ Max', 'platform=watchOS\ Simulator,name=Apple\ Watch\ Series\ 9\ \(41mm\)', 'platform=macOS CODE_SIGN_IDENTITY=""', 'platform=visionOS\ Simulator,OS=1.2,name=Apple\ Vision\ Pro CODE_SIGN_IDENTITY=""']
action: ['test', 'build']
exclude:
- destination: 'platform=iOS\ Simulator,OS=17.5,name=iPhone\ 15\ Pro\ Max'
action: 'build'
- destination: 'platform=macOS CODE_SIGN_IDENTITY=""'
action: 'build'
- destination: 'platform=visionOS\ Simulator,OS=1.2,name=Apple\ Vision\ Pro CODE_SIGN_IDENTITY=""'
action: 'test'
- destination: 'platform=watchOS\ Simulator,name=Apple\ Watch\ Series\ 9\ \(41mm\)'
action: 'test'
steps:
- uses: actions/checkout@v4
- name: Install Swiftlint
Expand Down
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/OCKAnyOutcome+Sequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// OCKAnyOutcome+Sequence.swift
// CareKitEssentials
//
// Created by Corey Baker on 7/7/24.
//

import CareKitStore
import Foundation

public extension Sequence where Element: OCKAnyOutcome {

/**
Returns the `OCKAnyOutcome` 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 `OCKAnyOutcome`'s by.
- parameter lessThanEqualTo: The value that the `keyPath` of all `OCKAnyOutcome`'s should
be less than or equal to. If this value is `nil`, the
- returns: Returns an array of `OCKAnyOutcome` sorted from newest to oldest with respect to `keyPath`.
- throws: An error when the `keyPath` cannot be unwrapped for any of the `OCKAnyOutcome` 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 -> Element? 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 `OCKAnyOutcome` 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 `OCKAnyOutcome`'s by.
- parameter lessThanEqualTo: The value that the `keyPath` of all `OCKAnyOutcome`'s should
be less than or equal to. If this value is `nil`, the
- returns: Returns an array of `OCKAnyOutcome` 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 -> Element? 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 d810768

Please sign in to comment.