ivanscorral / ivanscorral/KoWorkout
[Feature] Refactor Workout Templates: Dedicated Models, Repository, and List UI
- Dominant language
- Swift
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
# Refactor Workout Templates: Dedicated Models, Repository, and List UI
---
## Summary
Introduce dedicated workout template models and a repository, and refactor the list view to manage templates (not sessions).
Add a conversion API on `Workout` to create session instances from templates.
Update previews and main tab wiring accordingly.
**Key files:**
- `KoWorkout/Models/WorkoutTemplate.swift:10`
- `KoWorkout/Models/WorkoutTemplateExercise.swift:10`
- `KoWorkout/Models/WorkoutTemplateSet.swift:10`
- `KoWorkout/Repositories/WorkoutTemplateRepository.swift:10`
- `KoWorkout/ViewModels/WorkoutTemplateListViewModel.swift:13`
- `KoWorkout/Views/WorkoutTemplateListView.swift:10`
- `KoWorkout/Views/AddWorkoutTemplateView.swift:11`
- `KoWorkout/Models/Workout.swift:10`
- `KoWorkout/Repositories/WorkoutRepository.swift:40`
- `KoWorkout/Previews/Workout+Previews.swift:12`
- `KoWorkout/Views/MainTabView.swift:11`
---
## Motivation
- Correct architecture: templates define reusable workout structures; sessions represent individual performed workouts.
- The previous list view incorrectly handled Workout sessions; it should manage templates.
- Dedicated models and a repository for templates enable clean separation of concerns, ordering, and future persistence.
- Provides a single point to convert a template into a session to avoid duplication and drift.
---
## Scope
**Included:**
- New template models and repository.
- Refactor list view to display and create templates.
- Add session conversion API on `Workout`.
- Adjust previews and main tab wiring.
**Excluded:**
- Template detail editor screens (viewing/editing exercises and sets within a template).
- Persisted storage/migration for templates (in-memory only).
- Converting templates to sessions via UI entry points.
---
## Implementation Strategy
- Define `WorkoutTemplate`, `WorkoutTemplateExercise`, `WorkoutTemplateSet` with consistent ordering semantics.
- Implement `WorkoutTemplateRepository` with an in-memory implementation for creation, listing, updates, and reordering.
- Refactor `WorkoutTemplateListView` to use `WorkoutTemplateListViewModel` + `WorkoutTemplateRepository`.
- Provide `Workout.apply(template:)` to materialize a session from a template, capturing the `templateId`.
- Trim inputs and guard against empty titles in the add-template flow.
- Update previews to demonstrate the new template flow.
---
## Proposed Coverage / Functionality
### **Models**
#### `WorkoutTemplate` (`KoWorkout/Models/WorkoutTemplate.swift:10`)
- Holds title, notes, `[WorkoutTemplateExercise]`.
- `addExercise(_:order:notes:)` maintains `orderIndex` across exercises.
#### `WorkoutTemplateExercise` (`KoWorkout/Models/WorkoutTemplateExercise.swift:10`)
- Stores `templateId`, `exerciseId`, display metadata, `orderIndex`, `[WorkoutTemplateSet]`.
- `addSet(...)` appends and assigns `orderIndex`.
#### `WorkoutTemplateSet` (`KoWorkout/Models/WorkoutTemplateSet.swift:10`)
- Prescribed targets with `orderIndex`.
---
### **Repository**
#### `WorkoutTemplateRepository` (`KoWorkout/Repositories/WorkoutTemplateRepository.swift:10`)
- In-memory new/get/list/update/delete.
- Add/move/delete exercises and sets with order maintenance.
---
### **Views & ViewModels**
#### `WorkoutTemplateListViewModel` (`KoWorkout/ViewModels/WorkoutTemplateListViewModel.swift:13`)
- Loads templates, sorts by title, toggles add sheet, creates templates with validation.
#### `WorkoutTemplateListView` (`KoWorkout/Views/WorkoutTemplateListView.swift:10`)
- Displays template cards with exercise/set counts, notes, and add template sheet.
#### `AddWorkoutTemplateView` (`KoWorkout/Views/AddWorkoutTemplateView.swift:11`)
- Simple form for title and notes, wired to the view model.
---
### **Workouts**
#### `Workout` (`KoWorkout/Models/Workout.swift:10`)
- Adds `templateId` and `apply(template:)` to generate session exercises/sets from a template in order.
---
### **Wiring & Previews**
- **MainTabView** (`KoWorkout/Views/MainTabView.swift:11`) wires `WorkoutTemplateListView` to `InMemoryWorkoutTemplateRepository`.
- **Previews** updated to seed example templates (`KoWorkout/Views/WorkoutTemplateListView.swift:146`).
- **Workout previews** updated for new initializer (`KoWorkout/Previews/Workout+Previews.swift:12`).
---
## Example Skeleton
```swift
// Create and save a template
let repo = InMemoryWorkoutTemplateRepository()
var template = repo.new(title: "Upper Body", notes: "Push focus")
let bench = Exercise.makeRepsWeight(name: "Bench", defaultReps: 5)
let added = repo.addExercise(to: template.id, exercise: bench, notes: "Pause reps", order: nil)
// Materialize a session from a template
var session = Workout(date: Date(), templateId: nil, title: nil, notes: nil, exercises: [])
if let fetched = repo.get(template.id) {
session.apply(template: fetched)
}
```
---
## Acceptance Criteria
- [ ] Template list shows template cards with counts and notes.
- [ ] Adding a template through the sheet creates and shows it sorted by title.
- [ ]`Workout.apply(template:)` produces ordered exercises/sets with `templateId` set.
- [ ] Existing workout detail flows remain unaffected.
- [ ] Project compiles in Xcode; previews render.
---
## Files To Touch
- `KoWorkout/Models/WorkoutTemplate.swift:10`
- `KoWorkout/Models/WorkoutTemplateExercise.swift:10`
- `KoWorkout/Models/WorkoutTemplateSet.swift:10`
- `KoWorkout/Repositories/WorkoutTemplateRepository.swift:10`
- `KoWorkout/ViewModels/WorkoutTemplateListViewModel.swift:13`
- `KoWorkout/Views/WorkoutTemplateListView.swift:10`
- `KoWorkout/Views/AddWorkoutTemplateView.swift:11`
- `KoWorkout/Models/Workout.swift:41`
- `KoWorkout/Repositories/WorkoutRepository.swift:40`
- `KoWorkout/Previews/Workout+Previews.swift:12`
- `KoWorkout/Views/MainTabView.swift:11`
---
## Notes
**Follow-up tasks:**
- Add a template detail editor (manage template exercises/sets).
- Provide a “Start Workout” flow that creates a `Workout` from a template and navigates into session detail.
- Consider persistence (e.g., Core Data/SQLite/JSON) and data migration for templates.
- Update or add tests for template repository behaviors and `apply(template:)`.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.