initial pre-viable version of watch app

This commit is contained in:
2025-07-20 19:44:53 -04:00
parent 33b88cb8f0
commit 68d90160c6
35 changed files with 2108 additions and 179 deletions

View File

@ -0,0 +1,27 @@
import SwiftUI
import SwiftData
/// A protocol for entities that can be managed in a generic list view.
protocol EditableEntity: PersistentModel, Identifiable, Hashable {
/// The name of the entity to be displayed in the list.
var name: String { get set }
/// A view for adding or editing the entity.
associatedtype FormView: View
@ViewBuilder static func formView(for model: Self) -> FormView
/// Creates a new, empty instance of the entity.
static func createNew() -> Self
/// The title for the navigation bar in the list view.
static var navigationTitle: String { get }
/// An optional property to specify a count to be displayed in the list item.
var count: Int? { get }
}
extension EditableEntity {
var count: Int? {
return nil
}
}

View File

@ -0,0 +1,48 @@
import SwiftUI
import SwiftData
struct EntityAddEditView<T: EditableEntity, Content: View>: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.modelContext) private var modelContext
@State var model: T
private let content: (Binding<T>) -> Content
init(model: T, @ViewBuilder content: @escaping (Binding<T>) -> Content) {
_model = State(initialValue: model)
self.content = content
}
var body: some View {
NavigationStack {
Form {
content($model)
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
dismiss()
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("Save") {
// If the model is not in a context, it's a new one.
if model.modelContext == nil {
modelContext.insert(model)
}
// The save is in a do-catch block to handle potential errors,
// such as constraint violations.
do {
try modelContext.save()
dismiss()
} catch {
// In a real app, you'd want to present this error to the user.
print("Failed to save model: \(error.localizedDescription)")
}
}
}
}
}
}
}

View File

@ -0,0 +1,96 @@
import SwiftUI
import SwiftData
struct EntityListView<T: EditableEntity>: View {
@Environment(\.modelContext) private var modelContext
@Query var items: [T]
@State private var showingAddSheet = false
@State private var itemToEdit: T? = nil
@State private var itemToDelete: T? = nil
private var sortDescriptors: [SortDescriptor<T>]
init(sort: [SortDescriptor<T>] = [], searchString: String = "") {
self.sortDescriptors = sort
_items = Query(filter: #Predicate { item in
if searchString.isEmpty {
return true
} else {
return item.name.localizedStandardContains(searchString)
}
}, sort: sort)
}
@State private var isInsideNavigationStack: Bool = false
var body: some View {
let content = Form {
List {
ForEach(items) { item in
ListItem(text: item.name, count: item.count)
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button(role: .destructive) {
itemToDelete = item
} label: {
Label("Delete", systemImage: "trash")
}
Button {
itemToEdit = item
} label: {
Label("Edit", systemImage: "pencil")
}
.tint(.indigo)
}
}
}
}
.navigationTitle(T.navigationTitle)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: { showingAddSheet.toggle() }) {
Image(systemName: "plus")
}
}
}
.sheet(isPresented: $showingAddSheet) {
T.formView(for: T.createNew())
}
.sheet(item: $itemToEdit) { item in
T.formView(for: item)
}
.confirmationDialog(
"Delete?",
isPresented: Binding<Bool>(
get: { itemToDelete != nil },
set: { if !$0 { itemToDelete = nil } }
),
titleVisibility: .visible
) {
Button("Delete", role: .destructive) {
if let item = itemToDelete {
modelContext.delete(item)
try? modelContext.save()
itemToDelete = nil
}
}
Button("Cancel", role: .cancel) {
itemToDelete = nil
}
} message: {
Text("Are you sure you want to delete \(itemToDelete?.name ?? "this item")?")
}
.background(
NavigationStackChecker(isInside: $isInsideNavigationStack)
)
if isInsideNavigationStack {
content
} else {
NavigationStack {
content
}
}
}
}

View File

@ -0,0 +1,15 @@
import SwiftUI
struct NavigationStackChecker: UIViewControllerRepresentable {
@Binding var isInside: Bool
func makeUIViewController(context: Context) -> UIViewController {
let viewController = UIViewController()
DispatchQueue.main.async {
self.isInside = viewController.navigationController != nil
}
return viewController
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

View File

@ -0,0 +1,55 @@
//
// 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
//
// let completedStatus = WorkoutStatus.completed
//
// @Query(filter: #Predicate<Workout> { workout in
// workout.status?.rawValue != WorkoutStatus.completed.rawValue
// }, sort: \Workout.start, order: .reverse) var activeWorkouts: [Workout]
//
// var body: some View {
// NavigationStack {
// if activeWorkouts.isEmpty {
// NoActiveWorkoutView()
// } else if let currentWorkout = activeWorkouts.first {
// WorkoutLogListView(workout: currentWorkout)
// }
// }
// }
//}
//
//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,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 currentSetNumber: Int = 0
// @State private var restingSeconds: Int = 0
// @State private var timer: Timer?
// @State private var hapticTimer: Timer?
// @State private var hapticSeconds: Int = 0
//
// var body: some View {
// ScrollView {
// VStack(spacing: 16) {
// Text(log.exerciseName)
// .font(.headline)
// .multilineTextAlignment(.center)
//
// switch phase {
// case .notStarted:
// startView
// case .exercising(let setNumber):
// exercisingView(setNumber: setNumber)
// case .resting(let setNumber, let elapsedSeconds):
// restingView(setNumber: setNumber, elapsedSeconds: elapsedSeconds)
// case .completed:
// completedView
// }
// }
// .padding()
// }
// .navigationTitle("Progress")
// .navigationBarTitleDisplayMode(.inline)
// .onDisappear {
// stopTimers()
// }
// }
//
// private var startView: some View {
// VStack(spacing: 16) {
// Text("Ready to start")
// .font(.title3)
//
// Text("\(log.sets) sets × \(log.reps) reps × \(log.weight) lbs")
// .font(.subheadline)
// .foregroundStyle(.secondary)
//
// Button(action: startExercise) {
// Text("Start First Set")
// .font(.headline)
// .foregroundStyle(.white)
// .frame(maxWidth: .infinity)
// .padding(.vertical, 8)
// .background(Color.blue)
// .cornerRadius(8)
// }
// .buttonStyle(PlainButtonStyle())
// }
// }
//
// private func exercisingView(setNumber: Int) -> some View {
// VStack(spacing: 16) {
// Text("Set \(setNumber) of \(log.sets)")
// .font(.title3)
//
// Text("\(log.reps) reps × \(log.weight) lbs")
// .font(.subheadline)
// .foregroundStyle(.secondary)
//
// Text("In progress: \(hapticSeconds)s")
// .font(.body)
// .monospacedDigit()
//
// HStack {
// Button(action: completeSet) {
// Text("Complete")
// .font(.headline)
// .foregroundStyle(.white)
// .frame(maxWidth: .infinity)
// .padding(.vertical, 8)
// .background(Color.green)
// .cornerRadius(8)
// }
// .buttonStyle(PlainButtonStyle())
//
// Button(action: cancelSet) {
// Text("Cancel")
// .font(.headline)
// .foregroundStyle(.white)
// .frame(maxWidth: .infinity)
// .padding(.vertical, 8)
// .background(Color.red)
// .cornerRadius(8)
// }
// .buttonStyle(PlainButtonStyle())
// }
// }
// .gesture(
// DragGesture(minimumDistance: 20)
// .onEnded { gesture in
// if gesture.translation.width < 0 {
// // Swipe left to complete
// completeSet()
// } else if gesture.translation.width > 0 {
// // Swipe right to cancel
// cancelSet()
// }
// }
// )
// }
//
// private func restingView(setNumber: Int, elapsedSeconds: Int) -> some View {
// VStack(spacing: 16) {
// Text("Rest")
// .font(.title3)
//
// Text("After Set \(setNumber) of \(log.sets)")
// .font(.subheadline)
// .foregroundStyle(.secondary)
//
// Text("Resting: \(elapsedSeconds)s")
// .font(.body)
// .monospacedDigit()
//
// Button(action: {
// if setNumber < log.sets {
// startNextSet()
// } else {
// completeExercise()
// }
// }) {
// Text(setNumber < log.sets ? "Start Next Set" : "Complete Exercise")
// .font(.headline)
// .foregroundStyle(.white)
// .frame(maxWidth: .infinity)
// .padding(.vertical, 8)
// .background(Color.blue)
// .cornerRadius(8)
// }
// .buttonStyle(PlainButtonStyle())
// }
// .gesture(
// DragGesture(minimumDistance: 20)
// .onEnded { gesture in
// if gesture.translation.width < 0 {
// // Swipe left to start next set or complete
// if setNumber < log.sets {
// startNextSet()
// } else {
// completeExercise()
// }
// }
// }
// )
// }
//
// private var completedView: some View {
// VStack(spacing: 16) {
// Image(systemName: "checkmark.circle.fill")
// .font(.system(size: 50))
// .foregroundStyle(.green)
//
// Text("Exercise Completed!")
// .font(.title3)
//
// Button(action: {
// dismiss()
// }) {
// Text("Return to Workout")
// .font(.headline)
// .foregroundStyle(.white)
// .frame(maxWidth: .infinity)
// .padding(.vertical, 8)
// .background(Color.blue)
// .cornerRadius(8)
// }
// .buttonStyle(PlainButtonStyle())
// }
// }
//
// // MARK: - Actions
//
// private func startExercise() {
// currentSetNumber = 1
// phase = .exercising(setNumber: currentSetNumber)
//
// // Update workout log status
// log.status = .inProgress
// try? modelContext.save()
//
// // Start haptic timer
// startHapticTimer()
// }
//
// private func completeSet() {
// stopHapticTimer()
//
// // Start rest phase
// restingSeconds = 0
// phase = .resting(setNumber: currentSetNumber, elapsedSeconds: restingSeconds)
//
// // Start rest timer
// timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
// restingSeconds += 1
// phase = .resting(setNumber: currentSetNumber, elapsedSeconds: restingSeconds)
// }
//
// // Start haptic timer for rest phase
// startHapticTimer()
//
// // Play completion haptic
// WKInterfaceDevice.current().play(.success)
// }
//
// private func cancelSet() {
// // Just go back to the previous state
// if currentSetNumber > 1 {
// currentSetNumber -= 1
// phase = .resting(setNumber: currentSetNumber, elapsedSeconds: 0)
// } else {
// phase = .notStarted
// }
//
// stopHapticTimer()
// stopTimers()
// }
//
// private func startNextSet() {
// stopTimers()
//
// currentSetNumber += 1
// phase = .exercising(setNumber: currentSetNumber)
//
// // Start haptic timer for next set
// startHapticTimer()
// }
//
// private func completeExercise() {
// stopTimers()
//
// // Update workout log
// log.completed = true
// log.status = .completed
// try? modelContext.save()
//
// // Show completion screen
// phase = .completed
//
// // Play completion haptic
// WKInterfaceDevice.current().play(.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
// 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)
// }
// } else if hapticSeconds % 30 == 0 {
// // Double tap every 30 seconds
// WKInterfaceDevice.current().play(.click)
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
// WKInterfaceDevice.current().play(.click)
// }
// } else if hapticSeconds % 10 == 0 {
// // Light tap every 10 seconds
// WKInterfaceDevice.current().play(.click)
// }
// }
// }
//
// private func stopHapticTimer() {
// hapticTimer?.invalidate()
// hapticTimer = nil
// hapticSeconds = 0
// }
//
// private func stopTimers() {
// timer?.invalidate()
// timer = nil
// stopHapticTimer()
// }
//}
//
//#Preview {
// let container = AppContainer.preview
// let workout = Workout(start: Date(), end: Date(), split: nil)
// let log = WorkoutLog(workout: workout, exerciseName: "Bench Press", date: Date(), sets: 3, reps: 10, weight: 135)
//
// return ExerciseProgressView(log: log)
// .modelContainer(container)
//}

View File

@ -0,0 +1,81 @@
//
// SplitPickerView.swift
// Workouts
//
// Created by rzen on 7/13/25 at 7:17PM.
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
import SwiftData
struct SplitPickerView: View {
@Environment(\.modelContext) private var modelContext
@Environment(\.dismiss) private var dismiss
@Query(sort: [SortDescriptor(\Split.name)]) private var splits: [Split]
var onSplitSelected: (Split) -> Void
var body: some View {
NavigationStack {
ScrollView {
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 16) {
ForEach(splits) { split in
Button(action: {
onSplitSelected(split)
dismiss()
}) {
VStack {
ZStack(alignment: .bottom) {
// Golden ratio rectangle (1:1.618)
RoundedRectangle(cornerRadius: 12)
.fill(
LinearGradient(
gradient: Gradient(colors: [split.getColor(), split.getColor().darker(by: 0.2)]),
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
.aspectRatio(1.618, contentMode: .fit)
.shadow(radius: 2)
VStack {
// Icon in the center
Image(systemName: split.systemImage)
.font(.system(size: 40, weight: .medium))
.foregroundColor(.white)
.offset(y: -15)
// Name at the bottom inside the rectangle
Text(split.name)
.font(.headline)
.foregroundColor(.white)
.lineLimit(1)
.padding(.horizontal, 8)
.padding(.bottom, 8)
}
}
// Exercise count below the rectangle
Text("\(split.exercises?.count ?? 0) exercises")
.font(.caption)
.foregroundColor(.secondary)
}
}
.buttonStyle(PlainButtonStyle())
}
}
.padding()
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
dismiss()
}
}
}
}
}
}