This commit is contained in:
2025-07-25 17:30:11 -04:00
parent 310c120ca3
commit 3fd6887ce7
55 changed files with 1062 additions and 649 deletions

View File

@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,14 @@
{
"images" : [
{
"filename" : "DumbBellIcon-light.png",
"idiom" : "universal",
"platform" : "watchos",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

View File

@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,70 @@
//
// ContentView.swift
// Workouts
//
// Created by rzen on 7/15/25 at 7:09 PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@State var activeWorkouts: [Workout] = []
var body: some View {
NavigationStack {
if activeWorkouts.isEmpty {
NoActiveWorkoutView()
} else {
ActiveWorkoutListView(workouts: activeWorkouts)
}
}
.onAppear {
loadActiveWorkouts()
}
}
func loadActiveWorkouts () {
let completedStatus = WorkoutStatus.completed.rawValue
do {
self.activeWorkouts = try modelContext.fetch(FetchDescriptor<Workout>(
predicate: #Predicate<Workout> { workout in
workout.status != completedStatus
},
sortBy: [
SortDescriptor(\Workout.start, order: .reverse)
]
))
} catch {
print("ERROR: failed to load active workouts \(error)")
}
}
}
struct NoActiveWorkoutView: View {
var body: some View {
VStack(spacing: 16) {
Image(systemName: "dumbbell.fill")
.font(.system(size: 40))
.foregroundStyle(.gray)
Text("No Active Workout")
.font(.headline)
Text("Start a workout in the main app")
.font(.caption)
.foregroundStyle(.gray)
.multilineTextAlignment(.center)
}
.padding()
}
}
#Preview {
ContentView()
.modelContainer(AppContainer.preview)
}

View File

@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@ -0,0 +1,205 @@
import Foundation
import SwiftData
final class AppContainer {
static let logger = AppLogger(
subsystem: Bundle.main.bundleIdentifier ?? "dev.rzen.indie.Workouts.watchkitapp",
category: "AppContainer"
)
static func create() -> ModelContainer {
// Using the current models directly without migration plan to avoid reference errors
let schema = Schema(SchemaVersion.models)
#if targetEnvironment(simulator) && os(watchOS)
// Use local-only storage for watchOS simulator
let configuration = ModelConfiguration(isStoredInMemoryOnly: false)
logger.info("Creating local-only database for watchOS simulator")
do {
let container = try ModelContainer(for: schema, configurations: configuration)
// Populate with test data if needed
Task { @MainActor in
await populateSimulatorData(container: container)
}
return container
} catch {
logger.error("Failed to create simulator ModelContainer: \(error.localizedDescription)")
fatalError("Failed to create simulator ModelContainer: \(error.localizedDescription)")
}
#else
// Use CloudKit for real devices
let configuration = ModelConfiguration(cloudKitDatabase: .automatic)
logger.info("Creating CloudKit database for real device")
let container = try! ModelContainer(for: schema, configurations: configuration)
return container
#endif
}
@MainActor
static var preview: ModelContainer {
let configuration = ModelConfiguration(isStoredInMemoryOnly: true)
do {
let schema = Schema(SchemaVersion.models)
let container = try ModelContainer(for: schema, configurations: configuration)
return container
} catch {
fatalError("Failed to create preview ModelContainer: \(error.localizedDescription)")
}
}
@MainActor
private static func populateSimulatorData(container: ModelContainer) async {
let context = container.mainContext
// Check if data already exists
let fetchDescriptor = FetchDescriptor<Split>()
guard (try? context.fetch(fetchDescriptor))?.isEmpty ?? true else {
logger.info("Simulator database already has data, skipping population")
return // Data already exists
}
logger.info("Populating simulator database with test data from pf-starter-exercises.yaml")
// Create splits
let upperBodySplit = Split(name: "Upper Body", color: "blue", systemImage: "figure.strengthtraining.traditional", order: 0)
let lowerBodySplit = Split(name: "Lower Body", color: "green", systemImage: "figure.run", order: 1)
let fullBodySplit = Split(name: "Full Body", color: "purple", systemImage: "figure.mixed.cardio", order: 2)
let coreSplit = Split(name: "Core", color: "red", systemImage: "figure.core.training", order: 3)
context.insert(upperBodySplit)
context.insert(lowerBodySplit)
context.insert(fullBodySplit)
context.insert(coreSplit)
// Create exercises based on pf-starter-exercises.yaml
// Upper Body Exercises
let latPullDown = Exercise(split: upperBodySplit, exerciseName: "Lat Pull Down", order: 0, sets: 3, reps: 12, weight: 120)
let seatedRow = Exercise(split: upperBodySplit, exerciseName: "Seated Row", order: 1, sets: 3, reps: 12, weight: 110)
let shoulderPress = Exercise(split: upperBodySplit, exerciseName: "Shoulder Press", order: 2, sets: 3, reps: 10, weight: 90)
let chestPress = Exercise(split: upperBodySplit, exerciseName: "Chest Press", order: 3, sets: 3, reps: 10, weight: 130)
let tricepPress = Exercise(split: upperBodySplit, exerciseName: "Tricep Press", order: 4, sets: 3, reps: 12, weight: 70)
let armCurl = Exercise(split: upperBodySplit, exerciseName: "Arm Curl", order: 5, sets: 3, reps: 12, weight: 60)
context.insert(latPullDown)
context.insert(seatedRow)
context.insert(shoulderPress)
context.insert(chestPress)
context.insert(tricepPress)
context.insert(armCurl)
// Core Exercises
let abdominal = Exercise(split: coreSplit, exerciseName: "Abdominal", order: 0, sets: 3, reps: 15, weight: 80)
let rotary = Exercise(split: coreSplit, exerciseName: "Rotary", order: 1, sets: 3, reps: 15, weight: 70)
let plank = Exercise(split: coreSplit, exerciseName: "Plank", order: 2, sets: 3, reps: 1, weight: 0) // Reps as time in minutes
let russianTwists = Exercise(split: coreSplit, exerciseName: "Russian Twists", order: 3, sets: 3, reps: 20, weight: 25)
context.insert(abdominal)
context.insert(rotary)
context.insert(plank)
context.insert(russianTwists)
// Lower Body Exercises
let legPress = Exercise(split: lowerBodySplit, exerciseName: "Leg Press", order: 0, sets: 3, reps: 12, weight: 200)
let legExtension = Exercise(split: lowerBodySplit, exerciseName: "Leg Extension", order: 1, sets: 3, reps: 12, weight: 110)
let legCurl = Exercise(split: lowerBodySplit, exerciseName: "Leg Curl", order: 2, sets: 3, reps: 12, weight: 90)
let adductor = Exercise(split: lowerBodySplit, exerciseName: "Adductor", order: 3, sets: 3, reps: 15, weight: 100)
let abductor = Exercise(split: lowerBodySplit, exerciseName: "Abductor", order: 4, sets: 3, reps: 15, weight: 90)
let calfs = Exercise(split: lowerBodySplit, exerciseName: "Calfs", order: 5, sets: 3, reps: 15, weight: 120)
context.insert(legPress)
context.insert(legExtension)
context.insert(legCurl)
context.insert(adductor)
context.insert(abductor)
context.insert(calfs)
// Full Body Exercises (selected from both upper and lower)
let fullBodyChestPress = Exercise(split: fullBodySplit, exerciseName: "Chest Press", order: 0, sets: 3, reps: 10, weight: 130)
let fullBodyLatPullDown = Exercise(split: fullBodySplit, exerciseName: "Lat Pull Down", order: 1, sets: 3, reps: 12, weight: 120)
let fullBodyLegPress = Exercise(split: fullBodySplit, exerciseName: "Leg Press", order: 2, sets: 3, reps: 12, weight: 200)
let fullBodyAbdominal = Exercise(split: fullBodySplit, exerciseName: "Abdominal", order: 3, sets: 3, reps: 15, weight: 80)
context.insert(fullBodyChestPress)
context.insert(fullBodyLatPullDown)
context.insert(fullBodyLegPress)
context.insert(fullBodyAbdominal)
// Create workouts
let now = Date()
// Upper Body Workout (in progress)
let upperBodyWorkout = Workout(start: now, end: now, split: upperBodySplit)
upperBodyWorkout.status = 2
// upperBodyWorkout.status = .inProgress
upperBodyWorkout.end = nil
context.insert(upperBodyWorkout)
// Lower Body Workout (scheduled for tomorrow)
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: now) ?? now
let lowerBodyWorkout = Workout(start: tomorrow, end: tomorrow, split: lowerBodySplit)
lowerBodyWorkout.status = 1
// lowerBodyWorkout.status = .notStarted
context.insert(lowerBodyWorkout)
// Full Body Workout (completed yesterday)
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now) ?? now
let fullBodyWorkout = Workout(start: yesterday, end: yesterday, split: fullBodySplit)
fullBodyWorkout.status = 3
// fullBodyWorkout.status = .completed
context.insert(fullBodyWorkout)
// Create workout logs for Upper Body workout (in progress)
let chestPressLog = WorkoutLog(workout: upperBodyWorkout, exerciseName: chestPress.name, date: now, order: 0, sets: chestPress.sets, reps: chestPress.reps, weight: chestPress.weight, status: .completed, completed: true)
let shoulderPressLog = WorkoutLog(workout: upperBodyWorkout, exerciseName: shoulderPress.name, date: now, order: 1, sets: shoulderPress.sets, reps: shoulderPress.reps, weight: shoulderPress.weight, status: .completed, completed: true)
let latPullDownLog = WorkoutLog(workout: upperBodyWorkout, exerciseName: latPullDown.name, date: now, order: 2, sets: latPullDown.sets, reps: latPullDown.reps, weight: latPullDown.weight, status: .inProgress, completed: false)
let seatedRowLog = WorkoutLog(workout: upperBodyWorkout, exerciseName: seatedRow.name, date: now, order: 3, sets: seatedRow.sets, reps: seatedRow.reps, weight: seatedRow.weight, status: .notStarted, completed: false)
let tricepPressLog = WorkoutLog(workout: upperBodyWorkout, exerciseName: tricepPress.name, date: now, order: 4, sets: tricepPress.sets, reps: tricepPress.reps, weight: tricepPress.weight, status: .notStarted, completed: false)
let armCurlLog = WorkoutLog(workout: upperBodyWorkout, exerciseName: armCurl.name, date: now, order: 5, sets: armCurl.sets, reps: armCurl.reps, weight: armCurl.weight, status: .notStarted, completed: false)
context.insert(chestPressLog)
context.insert(shoulderPressLog)
context.insert(latPullDownLog)
context.insert(seatedRowLog)
context.insert(tricepPressLog)
context.insert(armCurlLog)
// Create workout logs for Lower Body workout (scheduled)
let legPressLog = WorkoutLog(workout: lowerBodyWorkout, exerciseName: legPress.name, date: tomorrow, order: 0, sets: legPress.sets, reps: legPress.reps, weight: legPress.weight, status: .notStarted, completed: false)
let legExtensionLog = WorkoutLog(workout: lowerBodyWorkout, exerciseName: legExtension.name, date: tomorrow, order: 1, sets: legExtension.sets, reps: legExtension.reps, weight: legExtension.weight, status: .notStarted, completed: false)
let legCurlLog = WorkoutLog(workout: lowerBodyWorkout, exerciseName: legCurl.name, date: tomorrow, order: 2, sets: legCurl.sets, reps: legCurl.reps, weight: legCurl.weight, status: .notStarted, completed: false)
let adductorLog = WorkoutLog(workout: lowerBodyWorkout, exerciseName: adductor.name, date: tomorrow, order: 3, sets: adductor.sets, reps: adductor.reps, weight: adductor.weight, status: .notStarted, completed: false)
let abductorLog = WorkoutLog(workout: lowerBodyWorkout, exerciseName: abductor.name, date: tomorrow, order: 4, sets: abductor.sets, reps: abductor.reps, weight: abductor.weight, status: .notStarted, completed: false)
let calfsLog = WorkoutLog(workout: lowerBodyWorkout, exerciseName: calfs.name, date: tomorrow, order: 5, sets: calfs.sets, reps: calfs.reps, weight: calfs.weight, status: .notStarted, completed: false)
context.insert(legPressLog)
context.insert(legExtensionLog)
context.insert(legCurlLog)
context.insert(adductorLog)
context.insert(abductorLog)
context.insert(calfsLog)
// Create workout logs for Full Body workout (completed)
let fullBodyChestPressLog = WorkoutLog(workout: fullBodyWorkout, exerciseName: fullBodyChestPress.name, date: yesterday, order: 0, sets: fullBodyChestPress.sets, reps: fullBodyChestPress.reps, weight: fullBodyChestPress.weight, status: .completed, completed: true)
let fullBodyLatPullDownLog = WorkoutLog(workout: fullBodyWorkout, exerciseName: fullBodyLatPullDown.name, date: yesterday, order: 1, sets: fullBodyLatPullDown.sets, reps: fullBodyLatPullDown.reps, weight: fullBodyLatPullDown.weight, status: .completed, completed: true)
let fullBodyLegPressLog = WorkoutLog(workout: fullBodyWorkout, exerciseName: fullBodyLegPress.name, date: yesterday, order: 2, sets: fullBodyLegPress.sets, reps: fullBodyLegPress.reps, weight: fullBodyLegPress.weight, status: .completed, completed: true)
let fullBodyAbdominalLog = WorkoutLog(workout: fullBodyWorkout, exerciseName: fullBodyAbdominal.name, date: yesterday, order: 3, sets: fullBodyAbdominal.sets, reps: fullBodyAbdominal.reps, weight: fullBodyAbdominal.weight, status: .completed, completed: true)
context.insert(fullBodyChestPressLog)
context.insert(fullBodyLatPullDownLog)
context.insert(fullBodyLegPressLog)
context.insert(fullBodyAbdominalLog)
do {
try context.save()
logger.info("Successfully populated simulator database with test data from pf-starter-exercises.yaml")
} catch {
logger.error("Failed to save test data: \(error.localizedDescription)")
}
}
}

View File

@ -0,0 +1,10 @@
import SwiftData
enum SchemaVersion {
static var models: [any PersistentModel.Type] = [
// Split.self,
// Exercise.self,
Workout.self,
WorkoutLog.self
]
}

View File

@ -0,0 +1,22 @@
import Foundation
import OSLog
struct AppLogger {
private let logger: Logger
init(subsystem: String, category: String) {
self.logger = Logger(subsystem: subsystem, category: category)
}
func debug(_ message: String) {
logger.debug("\(message)")
}
func info(_ message: String) {
logger.info("\(message)")
}
func error(_ message: String) {
logger.error("\(message)")
}
}

View File

@ -0,0 +1,21 @@
import SwiftUI
extension Color {
static func color(from name: String) -> Color {
switch name {
case "red": return .red
case "orange": return .orange
case "yellow": return .yellow
case "green": return .green
case "mint": return .mint
case "teal": return .teal
case "cyan": return .cyan
case "blue": return .blue
case "indigo": return .indigo
case "purple": return .purple
case "pink": return .pink
case "brown": return .brown
default: return .indigo
}
}
}

View File

@ -0,0 +1,9 @@
import Foundation
extension Date {
func formatDate() -> String {
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter.string(from: self)
}
}

View File

@ -0,0 +1,14 @@
import Foundation
extension Date {
func formatDateET(format: String = "MMMM, d yyyy @ h:mm a z") -> String {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(identifier: "America/New_York")
formatter.dateFormat = format
return formatter.string(from: self)
}
static var ISO8601: String {
"yyyy-MM-dd'T'HH:mm:ssZ"
}
}

View File

@ -0,0 +1,9 @@
import Foundation
extension Date {
func formattedDate() -> String {
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter.string(from: self)
}
}

View File

@ -0,0 +1,33 @@
import Foundation
import WatchKit
struct HapticFeedback {
static func success() {
WKInterfaceDevice.current().play(.success)
}
static func notification() {
WKInterfaceDevice.current().play(.notification)
}
static func click() {
WKInterfaceDevice.current().play(.click)
}
static func doubleTap() {
WKInterfaceDevice.current().play(.click)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
WKInterfaceDevice.current().play(.click)
}
}
static func tripleTap() {
WKInterfaceDevice.current().play(.notification)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
WKInterfaceDevice.current().play(.notification)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
WKInterfaceDevice.current().play(.notification)
}
}
}

View File

@ -0,0 +1,18 @@
//
// TimeInterval+minutesSecons.swift
// Workouts
//
// Created by rzen on 7/23/25 at 4:22PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import Foundation
extension Int {
var secondsFormatted: String {
let minutes = self / 60
let seconds = self % 60
return String(format: "%02d:%02d", minutes, seconds)
}
}

View File

@ -0,0 +1,36 @@
//
// ExerciseDoneCard.swift
// Workouts
//
// Created by rzen on 7/23/25 at 4:29PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct ExerciseDoneCard: View {
let elapsedSeconds: Int
let onComplete: () -> Void
var body: some View {
VStack(spacing: 20) {
Button(action: onComplete) {
Text("Done in \(10 - elapsedSeconds)s")
.font(.headline)
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(.green)
.padding(.horizontal)
}
.padding()
}
private var timeFormatted: String {
let minutes = elapsedSeconds / 60
let seconds = elapsedSeconds % 60
return String(format: "%02d:%02d", minutes, seconds)
}
}

View File

@ -0,0 +1,55 @@
//
// ExerciseIntroView.swift
// Workouts
//
// Created by rzen on 7/23/25 at 4:19PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct ExerciseIntroCard: View {
let log: WorkoutLog
var body: some View {
VStack(alignment: .center, spacing: 16) {
Text(log.exerciseName)
.font(.title)
.lineLimit(1)
.minimumScaleFactor(0.5)
.layoutPriority(1)
HStack(alignment: .bottom) {
Text("\(log.weight)")
Text("lbs")
.fontWeight(.light)
.padding([.trailing], 10)
Text("\(log.sets)")
Text("×")
.fontWeight(.light)
Text("\(log.reps)")
}
.font(.title3)
.lineLimit(1)
.minimumScaleFactor(0.5)
.layoutPriority(1)
Text(log.status?.name ?? "Not Started")
.foregroundStyle(Color.accentColor)
}
.padding()
// VStack(spacing: 20) {
// Text(title)
// .font(.title)
//
// Text(elapsedSeconds.secondsFormatted)
// .font(.system(size: 48, weight: .semibold, design: .monospaced))
// .foregroundStyle(Color.accentColor)
// }
// .padding()
}
}

View File

@ -0,0 +1,30 @@
//
// ExerciseRestCard.swift
// Workouts
//
// Created by rzen on 7/23/25 at 4:28PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct ExerciseRestCard: View {
let elapsedSeconds: Int
var body: some View {
VStack(spacing: 20) {
Text("Resting for")
.font(.title)
.lineLimit(1)
.minimumScaleFactor(0.5)
.layoutPriority(1)
Text(elapsedSeconds.secondsFormatted)
.font(.system(size: 48, weight: .semibold, design: .monospaced))
.foregroundStyle(Color.green)
}
.padding()
}
}

View File

@ -0,0 +1,28 @@
//
// ExerciseSetCard.swift
// Workouts
//
// Created by rzen on 7/23/25 at 4:26PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct ExerciseSetCard: View {
let set: Int
let elapsedSeconds: Int
var body: some View {
VStack(spacing: 20) {
Text("Set \(set)")
.font(.title)
Text(elapsedSeconds.secondsFormatted)
.font(.system(size: 48, weight: .semibold, design: .monospaced))
.foregroundStyle(Color.accentColor)
}
.padding()
}
}

View File

@ -0,0 +1,140 @@
//
// ExerciseProgressControlView 2.swift
// Workouts
//
// Created by rzen on 7/23/25 at 9:15AM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct ExerciseProgressControlView: View {
let log: WorkoutLog
@Environment(\.dismiss) private var dismiss
@Environment(\.modelContext) private var modelContext
@State private var exerciseStates: [ExerciseState] = []
@State private var currentStateIndex: Int = 0
@State private var elapsedSeconds: Int = 0
@State private var timer: Timer? = nil
@State private var previousStateIndex: Int = 0
var body: some View {
TabView(selection: $currentStateIndex) {
ForEach(Array(exerciseStates.enumerated()), id: \.element.id) { index, state in
if state.isIntro {
ExerciseIntroCard(log: log)
.tag(index)
} else if state.isSet {
ExerciseSetCard(set: state.setNumber ?? 0, elapsedSeconds: elapsedSeconds)
.tag(index)
} else if state.isRest {
ExerciseRestCard(elapsedSeconds: elapsedSeconds)
.tag(index)
} else if state.isDone {
ExerciseDoneCard(elapsedSeconds: elapsedSeconds, onComplete: completeExercise)
.tag(index)
}
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
.onChange(of: currentStateIndex) { oldValue, newValue in
if oldValue != newValue {
elapsedSeconds = 0
moveToNextState()
}
}
.onAppear {
setupExerciseStates()
currentStateIndex = log.currentStateIndex ?? 0
startTimer()
}
.onDisappear {
stopTimer()
}
}
private func setupExerciseStates() {
var states: [ExerciseState] = []
states.append(.intro)
for i in 1...log.sets {
states.append(.set(number: i))
if i < log.sets {
states.append(.rest(afterSet: i))
}
}
states.append(.done)
exerciseStates = states
}
private func startTimer() {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
elapsedSeconds += 1
// Check if we need to provide haptic feedback during rest periods
if currentStateIndex >= 0 && currentStateIndex < exerciseStates.count {
let currentState = exerciseStates[currentStateIndex]
if currentState.isRest {
provideRestHapticFeedback()
} else if currentState.isDone && elapsedSeconds >= 10 {
// Auto-complete after 10 seconds on the DONE state
completeExercise()
}
}
}
}
private func stopTimer() {
timer?.invalidate()
timer = nil
}
private func moveToNextState() {
if currentStateIndex < exerciseStates.count - 1 {
elapsedSeconds = 0
withAnimation {
currentStateIndex += 1
log.currentStateIndex = currentStateIndex
log.elapsedSeconds = elapsedSeconds
log.status = .inProgress
try? modelContext.save()
}
} else {
// We've reached the end (DONE state)
completeExercise()
}
}
private func provideRestHapticFeedback() {
// Provide haptic feedback based on elapsed time
if elapsedSeconds % 60 == 0 && elapsedSeconds > 0 {
// Triple tap every 60 seconds
HapticFeedback.tripleTap()
} else if elapsedSeconds % 30 == 0 && elapsedSeconds > 0 {
// Double tap every 30 seconds
HapticFeedback.doubleTap()
} else if elapsedSeconds % 10 == 0 && elapsedSeconds > 0 {
// Single tap every 10 seconds
HapticFeedback.success()
}
}
private func completeExercise() {
// Update the workout log status to completed
log.status = .completed
// reset index in case we wish to re-run the exercise
log.currentStateIndex = 0
// Provide "tada" haptic feedback
HapticFeedback.tripleTap()
// Dismiss this view to return to WorkoutDetailView
dismiss()
}
}

View File

@ -0,0 +1,317 @@
import SwiftUI
import SwiftData
import WatchKit
// Enum to track the current phase of the exercise
enum ExercisePhase {
case notStarted
case exercising(setNumber: Int)
case resting(setNumber: Int, elapsedSeconds: Int)
case completed
}
struct ExerciseProgressView: View {
@Environment(\.modelContext) private var modelContext
@Environment(\.dismiss) private var dismiss
let log: WorkoutLog
@State private var phase: ExercisePhase = .notStarted
@State private var hapticSeconds: Int = 0
@State private var restSeconds: Int = 0
@State private var hapticTimer: Timer? = nil
@State private var restTimer: Timer? = nil
var body: some View {
ScrollView {
VStack(spacing: 15) {
exerciseHeader
switch phase {
case .notStarted:
startPhaseView
case .exercising(let setNumber):
exercisingPhaseView(setNumber: setNumber)
case .resting(let setNumber, let elapsedSeconds):
restingPhaseView(setNumber: setNumber, elapsedSeconds: elapsedSeconds)
case .completed:
completedPhaseView
}
}
.padding()
}
.navigationTitle(log.exerciseName)
.navigationBarTitleDisplayMode(.inline)
.onDisappear {
stopTimers()
}
.gesture(
DragGesture(minimumDistance: 50)
.onEnded { gesture in
if gesture.translation.width < 0 {
// Swipe left - progress to next phase
handleSwipeLeft()
} else if gesture.translation.height < 0 && gesture.translation.height < -50 {
// Swipe up - cancel current set
handleSwipeUp()
}
}
)
}
// MARK: - View Components
private var exerciseHeader: some View {
VStack(alignment: .leading, spacing: 5) {
Text(log.exerciseName)
.font(.headline)
.foregroundColor(.primary)
Text("\(log.sets) sets × \(log.reps) reps")
.font(.subheadline)
.foregroundColor(.secondary)
Text("\(log.weight) lbs")
.font(.subheadline)
.foregroundColor(.secondary)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.bottom, 5)
}
private var startPhaseView: some View {
VStack(spacing: 20) {
Text("Ready to start?")
.font(.headline)
Button(action: startFirstSet) {
Text("Start First Set")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(.blue)
}
}
private func exercisingPhaseView(setNumber: Int) -> some View {
VStack(spacing: 20) {
Text("Set \(setNumber) of \(log.sets)")
.font(.headline)
Text("Exercising...")
.foregroundColor(.secondary)
HStack(spacing: 20) {
Button(action: completeSet) {
Text("Complete")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(.green)
Button(action: cancelSet) {
Text("Cancel")
.frame(maxWidth: .infinity)
}
.buttonStyle(.bordered)
.tint(.red)
}
Text("Or swipe left to complete")
.font(.caption)
.foregroundColor(.secondary)
Text("Swipe up to cancel")
.font(.caption)
.foregroundColor(.secondary)
}
}
private func restingPhaseView(setNumber: Int, elapsedSeconds: Int) -> some View {
VStack(spacing: 20) {
Text("Rest after Set \(setNumber)")
.font(.headline)
Text("Rest time: \(formatSeconds(elapsedSeconds))")
.foregroundColor(.secondary)
if setNumber < (log.sets) {
Button(action: { startNextSet(after: setNumber) }) {
Text("Start Set \(setNumber + 1)")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(.blue)
Text("Or swipe left to start next set")
.font(.caption)
.foregroundColor(.secondary)
} else {
Button(action: completeExercise) {
Text("Complete Exercise")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(.green)
Text("Or swipe left to complete")
.font(.caption)
.foregroundColor(.secondary)
}
}
}
private var completedPhaseView: some View {
VStack(spacing: 20) {
Text("Exercise Completed!")
.font(.headline)
.foregroundColor(.green)
Image(systemName: "checkmark.circle.fill")
.font(.system(size: 50))
.foregroundColor(.green)
Button(action: { dismiss() }) {
Text("Return to Workout")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(.blue)
}
}
// MARK: - Action Handlers
private func handleSwipeLeft() {
switch phase {
case .notStarted:
startFirstSet()
case .exercising:
completeSet()
case .resting(let setNumber, _):
if setNumber < (log.sets) {
startNextSet(after: setNumber)
} else {
completeExercise()
}
case .completed:
dismiss()
}
}
private func handleSwipeUp() {
if case .exercising = phase {
cancelSet()
}
}
private func startFirstSet() {
phase = .exercising(setNumber: 1)
startHapticTimer()
}
private func startNextSet(after completedSetNumber: Int) {
stopTimers()
let nextSetNumber = completedSetNumber + 1
phase = .exercising(setNumber: nextSetNumber)
startHapticTimer()
}
private func completeSet() {
stopTimers()
if case .exercising(let setNumber) = phase {
// Start rest timer
phase = .resting(setNumber: setNumber, elapsedSeconds: 0)
startRestTimer()
startHapticTimer()
// Play completion haptic
HapticFeedback.success()
}
}
private func cancelSet() {
// Just go back to the previous state
stopTimers()
phase = .notStarted
}
private func completeExercise() {
stopTimers()
// Update workout log
log.completed = true
log.status = .completed
try? modelContext.save()
// Show completion screen
phase = .completed
// Play completion haptic
HapticFeedback.success()
}
// MARK: - Timer Management
private func startHapticTimer() {
hapticSeconds = 0
hapticTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
hapticSeconds += 1
// Provide haptic feedback based on time intervals
if hapticSeconds % 60 == 0 {
// Triple tap every 60 seconds
HapticFeedback.tripleTap()
} else if hapticSeconds % 30 == 0 {
// Double tap every 30 seconds
HapticFeedback.doubleTap()
} else if hapticSeconds % 10 == 0 {
// Light tap every 10 seconds
HapticFeedback.click()
}
}
}
private func startRestTimer() {
restSeconds = 0
restTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
restSeconds += 1
if case .resting(let setNumber, _) = phase {
phase = .resting(setNumber: setNumber, elapsedSeconds: restSeconds)
}
}
}
private func stopTimers() {
hapticTimer?.invalidate()
hapticTimer = nil
restTimer?.invalidate()
restTimer = nil
}
// MARK: - Helper Functions
private func formatSeconds(_ seconds: Int) -> String {
let minutes = seconds / 60
let remainingSeconds = seconds % 60
return String(format: "%d:%02d", minutes, remainingSeconds)
}
}
//#Preview {
// let config = ModelConfiguration(isStoredInMemoryOnly: true)
// let container = try! ModelContainer(for: SchemaV1.models, configurations: config)
//
// // Create sample data
// let exercise = Exercise(name: "Bench Press", sets: 3, reps: 8, weight: 60.0)
// let workout = Workout(name: "Chest Day", date: Date())
// let log = WorkoutLog(exercise: exercise, workout: workout)
//
// NavigationStack {
// ExerciseProgressView(log: log)
// .modelContainer(container)
// }
//}

View File

@ -0,0 +1,72 @@
//
// ExerciseState.swift
// Workouts
//
// Created by rzen on 7/23/25 at 9:14AM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
enum ExerciseState: Identifiable {
case intro
case set(number: Int)
case rest(afterSet: Int)
case done
var id: String {
switch self {
case .intro:
return "detail"
case .set(let number):
return "set_\(number)"
case .rest(let afterSet):
return "rest_\(afterSet)"
case .done:
return "done"
}
}
var setNumber: Int? {
switch self {
case .intro, .rest, .done: return nil
case .set (let number): return number
}
}
var afterSet: Int? {
switch self {
case .intro, .set, .done: return nil
case .rest (let afterSet): return afterSet
}
}
var isIntro: Bool {
if case .intro = self {
return true
}
return false
}
var isSet: Bool {
if case .set = self {
return true
}
return false
}
var isRest: Bool {
if case .rest = self {
return true
}
return false
}
var isDone: Bool {
if case .done = self {
return true
}
return false
}
}

View File

@ -0,0 +1,47 @@
//
// ExerciseStateView 2.swift
// Workouts
//
// Created by rzen on 7/23/25 at 9:15AM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct ExerciseStateView: View {
let title: String
let isRest: Bool
let isDone: Bool
let elapsedSeconds: Int
let onComplete: () -> Void
var body: some View {
VStack(spacing: 20) {
Text(title)
.font(.title)
Text(timeFormatted)
.font(.system(size: 48, weight: .semibold, design: .monospaced))
.foregroundStyle(isRest ? .orange : .accentColor)
if isDone {
Button(action: onComplete) {
Text("Done in \(10 - elapsedSeconds)s")
.font(.headline)
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(.green)
.padding(.horizontal)
}
}
.padding()
}
private var timeFormatted: String {
let minutes = elapsedSeconds / 60
let seconds = elapsedSeconds % 60
return String(format: "%02d:%02d", minutes, seconds)
}
}

View File

@ -0,0 +1,62 @@
//
// ActiveWorkoutListView.swift
// Workouts
//
// Created by rzen on 7/20/25 at 6:35 PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
import SwiftData
struct ActiveWorkoutListView: View {
@Environment(\.modelContext) private var modelContext
let workouts: [Workout]
var body: some View {
List {
ForEach(workouts) { workout in
NavigationLink {
WorkoutDetailView(workout: workout)
} label: {
WorkoutCardView(workout: workout)
}
.listRowBackground(
RoundedRectangle(cornerRadius: 12)
.fill(Color.secondary.opacity(0.2))
.padding(
EdgeInsets(
top: 4,
leading: 8,
bottom: 4,
trailing: 8
)
)
)
// .swipeActions (edge: .trailing, allowsFullSwipe: false) {
// Button {
// //
// } label: {
// Label("Delete", systemImage: "trash")
// .frame(height: 40)
// }
// .tint(.red)
// }
}
}
.listStyle(.carousel)
}
}
#Preview {
let container = AppContainer.preview
let split = Split(name: "Upper Body", color: "blue", systemImage: "figure.strengthtraining.traditional")
let workout1 = Workout(start: Date(), end: Date(), split: split)
let split2 = Split(name: "Lower Body", color: "red", systemImage: "figure.run")
let workout2 = Workout(start: Date().addingTimeInterval(-3600), end: Date(), split: split2)
ActiveWorkoutListView(workouts: [workout1, workout2])
.modelContainer(container)
}

View File

@ -0,0 +1,36 @@
//
// WorkoutCardView.swift
// Workouts
//
// Created by rzen on 7/22/25 at 9:54PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct WorkoutCardView: View {
let workout: Workout
var body: some View {
VStack(alignment: .leading) {
if let split = workout.split {
Image(systemName: split.systemImage)
.font(.system(size: 48))
.foregroundStyle(split.getColor())
} else {
Image(systemName: "dumbbell.fill")
.font(.system(size: 24))
.foregroundStyle(.gray)
}
Text(workout.split?.name ?? "Workout")
.font(.headline)
.foregroundStyle(.white)
Text(workout.statusName)
.font(.caption)
.foregroundStyle(Color.accentColor)
}
}
}

View File

@ -0,0 +1,50 @@
//
// WorkoutDetailView.swift
// Workouts
//
// Created by rzen on 7/22/25 at 9:54PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct WorkoutDetailView: View {
let workout: Workout
var body: some View {
VStack(alignment: .center, spacing: 8) {
if let logs = workout.logs?.sorted(by: { $0.order < $1.order }), !logs.isEmpty {
List {
ForEach(logs) { log in
NavigationLink {
ExerciseProgressControlView(log: log)
} label: {
WorkoutLogCardView(log: log)
}
.listRowBackground(
RoundedRectangle(cornerRadius: 12)
.fill(Color.secondary.opacity(0.2))
.padding(
EdgeInsets(
top: 4,
leading: 8,
bottom: 4,
trailing: 8
)
)
)
}
}
.listStyle(.carousel)
} else {
Text("No exercises in this workout")
.font(.body)
.foregroundStyle(.secondary)
.padding()
Spacer()
}
}
}
}

View File

@ -0,0 +1,34 @@
//
// WorkoutLogCardView.swift
// Workouts
//
// Created by rzen on 7/22/25 at 9:56PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct WorkoutLogCardView: View {
let log: WorkoutLog
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(log.exerciseName)
.font(.headline)
.lineLimit(1)
Text(log.status?.name ?? "Not Started")
.font(.caption)
.foregroundStyle(Color.accentColor)
HStack(spacing: 12) {
Text("\(log.weight) lbs")
Spacer()
Text("\(log.sets) × \(log.reps)")
}
}
}
}

View File

@ -0,0 +1,69 @@
//
// WorkoutLogDetailView.swift
// Workouts
//
// Created by rzen on 7/22/25 at 9:57PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
struct WorkoutLogDetailView: View {
let log: WorkoutLog
var body: some View {
NavigationLink {
ExerciseProgressControlView(log: log)
} label: {
VStack(alignment: .center) {
Text(log.exerciseName)
.font(.title)
.lineLimit(1)
.minimumScaleFactor(0.5)
.layoutPriority(1)
HStack (alignment: .bottom) {
Text("\(log.weight)")
Text( "lbs")
.fontWeight(.light)
.padding([.trailing], 10)
Text("\(log.sets)")
Text("×")
.fontWeight(.light)
Text("\(log.reps)")
}
.font(.title3)
.lineLimit(1)
.minimumScaleFactor(0.5)
.layoutPriority(1)
Text(log.status?.name ?? "Not Started")
.foregroundStyle(Color.accentColor)
Text("Tap to start")
.foregroundStyle(Color.accentColor)
}
.padding()
}
.buttonStyle(.plain)
}
private func statusColor(for status: WorkoutStatus?) -> Color {
guard let status = status else { return .secondary }
switch status {
case .notStarted:
return .secondary
case .inProgress:
return .blue
case .completed:
return .green
case .skipped:
return .red
}
}
}

View File

@ -0,0 +1,117 @@
import SwiftUI
import SwiftData
struct WorkoutLogListView: View {
@Environment(\.modelContext) private var modelContext
let workout: Workout
@State private var selectedLogIndex: Int = 0
var body: some View {
VStack {
if let logs = workout.logs?.sorted(by: { $0.order < $1.order }), !logs.isEmpty {
TabView(selection: $selectedLogIndex) {
ForEach(Array(logs.enumerated()), id: \.element.id) { index, log in
WorkoutLogCard(log: log, index: index)
.tag(index)
}
}
.tabViewStyle(.page)
// .indexViewStyle(.page(backgroundDisplayMode: .always))
} else {
Text("No exercises in this workout")
.foregroundStyle(.secondary)
}
}
.navigationTitle(workout.split?.name ?? "Workout")
.navigationBarTitleDisplayMode(.inline)
}
}
struct WorkoutLogCard: View {
let log: WorkoutLog
let index: Int
var body: some View {
VStack(spacing: 12) {
Text(log.exerciseName)
.font(.headline)
.multilineTextAlignment(.center)
HStack {
VStack {
Text("\(log.sets)")
.font(.title2)
Text("Sets")
.font(.caption)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
VStack {
Text("\(log.reps)")
.font(.title2)
Text("Reps")
.font(.caption)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
VStack {
Text("\(log.weight)")
.font(.title2)
Text("Weight")
.font(.caption)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
}
NavigationLink {
ExerciseProgressView(log: log)
} label: {
Text("Start")
.font(.headline)
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 8)
.background(Color.blue)
.cornerRadius(8)
}
.buttonStyle(PlainButtonStyle())
Text(log.status?.name ?? "Not Started")
.font(.caption)
.foregroundStyle(statusColor(for: log.status))
}
.padding()
.background(Color.secondary.opacity(0.2))
.cornerRadius(12)
.padding(.horizontal)
}
private func statusColor(for status: WorkoutStatus?) -> Color {
guard let status = status else { return .secondary }
switch status {
case .notStarted:
return .secondary
case .inProgress:
return .blue
case .completed:
return .green
case .skipped:
return .red
}
}
}
#Preview {
let container = AppContainer.preview
let workout = Workout(start: Date(), end: Date(), split: nil)
let log1 = WorkoutLog(workout: workout, exerciseName: "Bench Press", date: Date(), sets: 3, reps: 10, weight: 135)
let log2 = WorkoutLog(workout: workout, exerciseName: "Squats", date: Date(), order: 1, sets: 3, reps: 8, weight: 225)
return WorkoutLogListView(workout: workout)
.modelContainer(container)
}

View File

@ -0,0 +1,16 @@
<?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>aps-environment</key>
<string>development</string>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.com.dev.rzen.indie.Workouts</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudKit</string>
</array>
</dict>
</plist>

View File

@ -0,0 +1,23 @@
//
// WorksoutsApp.swift
// Workouts
//
// Created by rzen on 7/15/25 at 7:09 PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
import SwiftData
@main
struct Workouts_Watch_AppApp: App {
let container = AppContainer.create()
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(container)
}
}
}

View File

@ -0,0 +1,45 @@
////
//// ExerciseDetailView.swift
//// Workouts
////
//// Created by rzen on 7/23/25 at 9:17AM.
////
//// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
////
//
//import SwiftUI
//
//struct ExerciseDetailView: View {
// let log: WorkoutLog
// let onStart: () -> Void
//
// var body: some View {
// VStack(alignment: .center, spacing: 16) {
// Text(log.exerciseName)
// .font(.title)
// .lineLimit(1)
// .minimumScaleFactor(0.5)
// .layoutPriority(1)
//
// HStack(alignment: .bottom) {
// Text("\(log.weight)")
// Text("lbs")
// .fontWeight(.light)
// .padding([.trailing], 10)
//
// Text("\(log.sets)")
// Text("×")
// .fontWeight(.light)
// Text("\(log.reps)")
// }
// .font(.title3)
// .lineLimit(1)
// .minimumScaleFactor(0.5)
// .layoutPriority(1)
//
// Text(log.status?.name ?? "Not Started")
// .foregroundStyle(Color.accentColor)
// }
// .padding()
// }
//}

View File

@ -0,0 +1,35 @@
////
//// ExerciseProgressControlView.swift
//// Workouts
////
//// Created by rzen on 7/20/25 at 7:19 PM.
////
//// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
////
//
//import SwiftUI
//import SwiftData
//
//
//
//
//
//
//// Detail view shown as the first item in the exercise progress carousel
//
//
//// Helper extension to safely access array elements
////extension Array {
//// subscript(safe index: Index) -> Element? {
//// return indices.contains(index) ? self[index] : nil
//// }
////}
//
////#Preview {
//// let container = AppContainer.preview
//// let workout = Workout(start: Date(), end: nil, split: nil)
//// let log = WorkoutLog(workout: workout, exerciseName: "Bench Press", date: Date(), sets: 3, reps: 10, weight: 135)
////
//// ExerciseProgressControlView(log: log)
//// .modelContainer(container)
////}