Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ public class EquipmentSwapManager {

// MARK: - Workout Swaps

/// Suggest catalogue alternatives with the ID of each eligible workout entry.
/// Results preserve workout order and keep repeated exercises addressable.
/// Uses `findAlternatives(for:from:)` without changing the workout.
public func suggestEntrySwaps(
for workout: Workout,
from catalogue: [Exercise]
) -> [WorkoutSwapSuggestion] {
workout.exercises.compactMap { entry in
let alternatives = findAlternatives(for: entry.exercise, from: catalogue)
guard !alternatives.isEmpty else { return nil }
return WorkoutSwapSuggestion(
workoutExerciseId: entry.id, original: entry.exercise, alternatives: alternatives)
}
}

/// Suggest real catalogue alternatives for each workout entry, in workout order.
/// Entries with available equipment or no eligible alternatives are omitted.
/// Repeated workout entries remain separate suggestions. The workout is unchanged.
Expand All @@ -74,9 +89,8 @@ public class EquipmentSwapManager {
for workout: Workout,
from catalogue: [Exercise]
) -> [(original: Exercise, alternatives: [Exercise])] {
workout.exercises.compactMap { entry in
let alternatives = findAlternatives(for: entry.exercise, from: catalogue)
return alternatives.isEmpty ? nil : (original: entry.exercise, alternatives: alternatives)
suggestEntrySwaps(for: workout, from: catalogue).map { suggestion in
(original: suggestion.original, alternatives: suggestion.alternatives)
}
}

Expand Down
15 changes: 15 additions & 0 deletions Sources/BetterFit/Models/WorkoutSwapSuggestion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation

/// Catalogue alternatives for one entry in a workout.
/// Use `workoutExerciseId` with `applySwap(workout:workoutExerciseId:newExercise:)`.
public struct WorkoutSwapSuggestion: Equatable {
public let workoutExerciseId: UUID
public let original: Exercise
public let alternatives: [Exercise]

public init(workoutExerciseId: UUID, original: Exercise, alternatives: [Exercise]) {
self.workoutExerciseId = workoutExerciseId
self.original = original
self.alternatives = alternatives
}
}
124 changes: 124 additions & 0 deletions Tests/BetterFitTests/WorkoutSwapSuggestionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import Foundation
import Testing
@testable import BetterFit

struct WorkoutSwapSuggestionTests {
private let press = Exercise(
name: "Barbell Bench Press", equipmentRequired: .barbell, muscleGroups: [.chest, .triceps])
private let dumbbellPress = Exercise(
name: "Dumbbell Bench Press", equipmentRequired: .dumbbell,
muscleGroups: [.chest, .triceps], imageURL: "dumbbell-press.png")

// MARK: - Suggestions

@Test func identifiesRepeatedEntriesWithoutChangingWorkout() {
let manager = EquipmentSwapManager(availableEquipment: [.dumbbell])
let warmUp = WorkoutExercise(exercise: press, sets: [ExerciseSet(reps: 12)], notes: "Warm up")
let working = WorkoutExercise(exercise: press, sets: [ExerciseSet(reps: 6)], notes: "Working sets")
let workout = Workout(name: "Push", exercises: [warmUp, working])
let snapshot = workout

let suggestions = manager.suggestEntrySwaps(for: workout, from: [dumbbellPress])

#expect(suggestions == [warmUp, working].map { entry in
WorkoutSwapSuggestion(workoutExerciseId: entry.id, original: press, alternatives: [dumbbellPress])
})
#expect(workout == snapshot)
}

@Test func omitsIneligibleEntriesAndPreservesWorkoutAndCatalogueOrder() {
let manager = EquipmentSwapManager(availableEquipment: [.dumbbell, .machine])
let machinePress = Exercise(
name: "Machine Press", equipmentRequired: .machine,
muscleGroups: [.triceps, .chest], imageURL: "machine-press.png")
let row = Exercise(name: "Barbell Row", equipmentRequired: .barbell, muscleGroups: [.back])
let dumbbellRow = Exercise(name: "Dumbbell Row", equipmentRequired: .dumbbell, muscleGroups: [.back])
let squat = Exercise(name: "Barbell Squat", equipmentRequired: .barbell, muscleGroups: [.quads])
let entries = [row, dumbbellPress, squat, press].map { WorkoutExercise(exercise: $0) }
let workout = Workout(name: "Full Body", exercises: entries)
let catalogue = [machinePress, dumbbellRow, dumbbellPress, machinePress]

let suggestions = manager.suggestEntrySwaps(for: workout, from: catalogue)

#expect(suggestions.map(\.workoutExerciseId) == [entries[0].id, entries[3].id])
#expect(suggestions.map(\.original) == [row, press])
#expect(suggestions.map(\.alternatives) == [[dumbbellRow], [machinePress, dumbbellPress]])
}

@Test func returnsNoEntrySuggestionsForEmptyInputs() {
let manager = EquipmentSwapManager(availableEquipment: [.dumbbell])
let workout = Workout(name: "Push", exercises: [WorkoutExercise(exercise: press)])

#expect(manager.suggestEntrySwaps(for: Workout(name: "Empty"), from: [dumbbellPress]).isEmpty)
#expect(manager.suggestEntrySwaps(for: workout, from: []).isEmpty)
}

@Test func reflectsUpdatedEquipmentAvailability() {
let manager = EquipmentSwapManager(availableEquipment: [.barbell, .dumbbell])
let entry = WorkoutExercise(exercise: press)
let workout = Workout(name: "Push", exercises: [entry])

#expect(manager.suggestEntrySwaps(for: workout, from: [dumbbellPress]).isEmpty)
manager.setAvailableEquipment([.dumbbell])
#expect(manager.suggestEntrySwaps(for: workout, from: [dumbbellPress]).map(\.workoutExerciseId) == [entry.id])
manager.setAvailableEquipment([])
#expect(manager.suggestEntrySwaps(for: workout, from: [dumbbellPress]).isEmpty)
}

// MARK: - Application and Compatibility

@Test func appliesSecondRepeatedEntrySuggestionAndPreservesWorkoutData() throws {
let manager = EquipmentSwapManager(availableEquipment: [.dumbbell])
let timestamp = Date(timeIntervalSince1970: 1_700_000_000)
let warmUp = WorkoutExercise(exercise: press, sets: [ExerciseSet(reps: 12)], notes: "Warm up")
let working = WorkoutExercise(exercise: press, sets: [
ExerciseSet(reps: 8, weight: 20, isCompleted: true, timestamp: timestamp, autoTracked: true),
ExerciseSet(reps: 6, weight: 22)
], notes: "Controlled tempo")
var workout = Workout(
name: "Push", exercises: [warmUp, working], date: timestamp,
duration: 600, isCompleted: true, templateId: UUID())
let snapshot = workout
let suggestion = try #require(manager.suggestEntrySwaps(for: workout, from: [dumbbellPress]).last)
let selected = try #require(suggestion.alternatives.first)

#expect(manager.applySwap(
workout: &workout, workoutExerciseId: suggestion.workoutExerciseId, newExercise: selected))

var expected = snapshot
expected.exercises[1].exercise = dumbbellPress
#expect(workout == expected)
}

@Test func suggestionStillTargetsSameEntryAfterReordering() throws {
let manager = EquipmentSwapManager(availableEquipment: [.dumbbell])
let first = WorkoutExercise(exercise: press)
let second = WorkoutExercise(exercise: press)
var workout = Workout(name: "Push", exercises: [first, second])
let suggestion = try #require(manager.suggestEntrySwaps(for: workout, from: [dumbbellPress]).last)
let selected = try #require(suggestion.alternatives.first)
workout.exercises.reverse()

#expect(manager.applySwap(
workout: &workout, workoutExerciseId: suggestion.workoutExerciseId, newExercise: selected))

#expect(workout.exercises[0].id == second.id)
#expect(workout.exercises[0].exercise == dumbbellPress)
#expect(workout.exercises[1] == first)
#expect(manager.suggestEntrySwaps(for: workout, from: [dumbbellPress]).map(\.workoutExerciseId) == [first.id])
}

@Test func tupleSuggestionsRemainCompatibleWithEntrySuggestions() {
let manager = EquipmentSwapManager(availableEquipment: [.dumbbell])
let workout = Workout(name: "Push", exercises: [
WorkoutExercise(exercise: press), WorkoutExercise(exercise: press)
])

let entries = manager.suggestEntrySwaps(for: workout, from: [dumbbellPress])
let tuples = manager.suggestSwaps(for: workout, from: [dumbbellPress])

#expect(tuples.map(\.original) == entries.map(\.original))
#expect(tuples.map(\.alternatives) == entries.map(\.alternatives))
#expect(!manager.suggestSwaps(for: workout).isEmpty)
}
}
21 changes: 21 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ public class EquipmentSwapManager
- `findAlternatives(for:from:) -> [Exercise]` - Select real alternatives from a supplied exercise catalogue
- `suggestSwaps(for:) -> [(original: Exercise, alternatives: [Exercise])]`
- `suggestSwaps(for:from:) -> [(original: Exercise, alternatives: [Exercise])]` - Select catalogue alternatives for a workout
- `suggestEntrySwaps(for:from:) -> [WorkoutSwapSuggestion]` - Select catalogue alternatives with workout entry IDs
- `applySwap(workout:originalExerciseId:newExercise:) -> Bool`
- `applySwap(workout:workoutExerciseId:newExercise:) -> Bool` - Replace a specific workout entry

Expand All @@ -434,6 +435,26 @@ metadata. Entries with available equipment or no eligible alternative are omitte
Repeated workout entries remain separate suggestions. Neither the workout nor
its sets are changed until the caller applies a selected swap with `applySwap`.

`suggestEntrySwaps(for:from:)` returns a `WorkoutSwapSuggestion` for each eligible
entry, with `workoutExerciseId`, `original` and `alternatives`. It uses the same
catalogue matching rules and preserves workout order, including separate results
for repeated exercise definitions. Available equipment and entries without an
eligible alternative are omitted. The workout is unchanged.

Pass the suggestion's entry ID to apply a selected alternative to that occurrence.
The ID continues to target the same entry if the workout is reordered.

```swift
if let suggestion = swaps.suggestEntrySwaps(for: workout, from: exerciseCatalogue).last,
let replacement = suggestion.alternatives.first {
swaps.applySwap(
workout: &workout, workoutExerciseId: suggestion.workoutExerciseId, newExercise: replacement)
}
```

Suggestions are snapshots. Regenerate them after editing a workout or changing
available equipment; applying a swap does not revalidate a suggestion.

Use `applySwap(workout:workoutExerciseId:newExercise:)` with a `WorkoutExercise.id`
to replace one occurrence of a repeated exercise. It changes only that entry's
exercise, preserving its ID, sets and notes and all other workout data. It returns
Expand Down
Loading