-
Notifications
You must be signed in to change notification settings - Fork 13
Remove stones as a weight unit, keeping only pounds and kilograms #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-11
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4d414e7
Initial plan for issue
Copilot 2ab6ca2
Remove stones support from core models and view models
Copilot 8065abf
Complete stones removal with final verification tests
Copilot e077635
STEP #1 - Consolidated Models, ViewModels, and Views folders under Dr…
pierceboggan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,82 @@ | ||
|
|
||
| // AIWorkoutGenerator.swift | ||
| // Dropped | ||
| // | ||
| // Service for generating structured cycling workouts using the OpenAI API. | ||
| // | ||
| // - Handles prompt construction, API requests, and response parsing. | ||
| // - Designed for use by the WorkoutGeneratorViewModel. | ||
| // | ||
| // Edge Cases: Handles API/network errors and invalid responses. | ||
| // Limitations: Assumes OpenAI API key is available and valid. | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Error types for AIWorkoutGenerator | ||
| enum AIWorkoutGeneratorError: Error { | ||
| case networkError(Error) | ||
| case invalidResponse | ||
| case apiError(String) | ||
| } | ||
|
|
||
| /// Service responsible for generating workouts using OpenAI's API. | ||
| /// - Usage: Call `generateWorkout` with user FTP and selected WorkoutType. | ||
| final class AIWorkoutGenerator { | ||
| private let apiKey: String | ||
| private let endpoint = URL(string: "https://api.openai.com/v1/chat/completions")! | ||
| private let model = "gpt-3.5-turbo" | ||
|
|
||
| /// Initialize with OpenAI API key | ||
| init(apiKey: String) { | ||
| self.apiKey = apiKey | ||
| } | ||
|
|
||
| /// Generates a structured workout using OpenAI | ||
| /// - Parameters: | ||
| /// - ftp: User's Functional Threshold Power (watts) | ||
| /// - type: Selected WorkoutType | ||
| /// - completion: Callback with result (JSON string or error) | ||
| func generateWorkout(ftp: Int, type: WorkoutType, completion: @escaping (Result<String, AIWorkoutGeneratorError>) -> Void) { | ||
| let prompt = Self.makePrompt(ftp: ftp, type: type) | ||
| let requestBody: [String: Any] = [ | ||
| "model": model, | ||
| "messages": [ | ||
| ["role": "system", "content": "You are a cycling coach AI. Output only valid JSON."], | ||
| ["role": "user", "content": prompt] | ||
| ] | ||
| ] | ||
| guard let body = try? JSONSerialization.data(withJSONObject: requestBody) else { | ||
| completion(.failure(.invalidResponse)) | ||
| return | ||
| } | ||
| var request = URLRequest(url: endpoint) | ||
| request.httpMethod = "POST" | ||
| request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization") | ||
| request.setValue("application/json", forHTTPHeaderField: "Content-Type") | ||
| request.httpBody = body | ||
| let task = URLSession.shared.dataTask(with: request) { data, response, error in | ||
| if let error = error { | ||
| completion(.failure(.networkError(error))) | ||
| return | ||
| } | ||
| guard let data = data, | ||
| let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any], | ||
| let choices = json["choices"] as? [[String: Any]], | ||
| let message = choices.first?["message"] as? [String: Any], | ||
| let content = message["content"] as? String else { | ||
| completion(.failure(.invalidResponse)) | ||
| return | ||
| } | ||
| completion(.success(content)) | ||
| } | ||
| task.resume() | ||
| } | ||
|
|
||
| /// Constructs the AI prompt for workout generation | ||
| private static func makePrompt(ftp: Int, type: WorkoutType) -> String { | ||
| """ | ||
| Generate a structured cycling workout for a rider with FTP \(ftp) watts. Workout type: \(type.displayName). | ||
| Output JSON with fields: title, summary, intervals (array of {duration_minutes, target_watts, description}), and total_duration_minutes. | ||
| """ | ||
| } | ||
| } |
Empty file.
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -90,7 +90,6 @@ struct Workout: Identifiable, Codable, Equatable { | |||||
| enum WeightUnit: String, CaseIterable, Identifiable, Codable { | ||||||
| case pounds = "lb" | ||||||
| case kilograms = "kg" | ||||||
| case stones = "st" | ||||||
|
|
||||||
| var id: String { self.rawValue } | ||||||
|
|
||||||
|
|
@@ -103,8 +102,6 @@ enum WeightUnit: String, CaseIterable, Identifiable, Codable { | |||||
| valueInKg = value * 0.453592 | ||||||
| case .kilograms: | ||||||
| valueInKg = value | ||||||
| case .stones: | ||||||
| valueInKg = value * 6.35029 | ||||||
| } | ||||||
|
|
||||||
| // Convert from kg to target unit | ||||||
|
|
@@ -113,8 +110,6 @@ enum WeightUnit: String, CaseIterable, Identifiable, Codable { | |||||
| return valueInKg / 0.453592 | ||||||
| case .kilograms: | ||||||
| return valueInKg | ||||||
| case .stones: | ||||||
| return valueInKg / 6.35029 | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -199,6 +194,13 @@ class UserDataManager { | |||||
| } | ||||||
|
|
||||||
| func loadUserData() -> UserData { | ||||||
| // Migrate any existing stones users to pounds | ||||||
| if userData.weightUnit == "st" { | ||||||
| var migratedData = userData | ||||||
| migratedData.weightUnit = WeightUnit.pounds.rawValue | ||||||
| self.userData = migratedData | ||||||
|
||||||
| self.userData = migratedData | |
| saveUserData(migratedData) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Avoid using the hardcoded literal "st". Define a legacy constant or extension to represent the old stones unit, which improves readability and reduces magic strings.