Refactor UI: move Splits to Settings, redesign ExerciseView

Schema & Models:
- Add notes, loadType, duration fields to WorkoutLog
- Align Watch schema with iOS (use duration Date instead of separate mins/secs)
- Add duration helper properties to Exercise and WorkoutLog

UI Changes:
- Remove Splits and Settings tabs, single Workout Logs view
- Add gear button in nav bar to access Settings as sheet
- Move Splits section into Settings view with inline list
- Redesign ExerciseView with read-only Plan/Notes tiles and Edit buttons
- Add PlanEditView and NotesEditView with Cancel/Save buttons
- Auto-dismiss ExerciseView when completing last set
- Navigate to ExerciseView when adding new exercise

Data Flow:
- Plan edits sync to both WorkoutLog and corresponding Exercise
- Changes propagate up navigation chain via CoreData
This commit is contained in:
2026-01-19 16:10:37 -05:00
parent c65040e756
commit 8b6250e4d6
14 changed files with 592 additions and 106 deletions

View File

@@ -17,46 +17,19 @@ struct ExerciseView: View {
@ObservedObject var workoutLog: WorkoutLog
var allLogs: [WorkoutLog]
var currentIndex: Int = 0
@State private var progress: Int = 0
@State private var navigateTo: WorkoutLog? = nil
@State private var showingPlanEdit = false
@State private var showingNotesEdit = false
let notStartedColor = Color.white
let completedColor = Color.green
var body: some View {
Form {
Section(header: Text("Navigation")) {
HStack {
Button(action: navigateToPrevious) {
HStack {
Image(systemName: "chevron.left")
Text("Previous")
}
}
.disabled(currentIndex <= 0)
Spacer()
Text("\(currentIndex + 1) of \(allLogs.count)")
.foregroundColor(.secondary)
Spacer()
Button(action: navigateToNext) {
HStack {
Text("Next")
Image(systemName: "chevron.right")
}
}
.disabled(currentIndex >= allLogs.count - 1)
}
.padding(.vertical, 8)
}
// MARK: - Progress Section
Section(header: Text("Progress")) {
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: Int(workoutLog.sets)), spacing: 2) {
ForEach(1...Int(workoutLog.sets), id: \.self) { index in
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: Int(workoutLog.sets)), spacing: 4) {
ForEach(1...max(1, Int(workoutLog.sets)), id: \.self) { index in
ZStack {
let completed = index <= progress
let color = completed ? completedColor : notStartedColor
@@ -71,95 +44,181 @@ struct ExerciseView: View {
.aspectRatio(0.618, contentMode: .fit)
.shadow(radius: 2)
Text("\(index)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.primary)
.colorInvert()
}
.onTapGesture {
if progress == index {
progress = 0
} else {
progress = index
let totalSets = Int(workoutLog.sets)
let isLastTile = index == totalSets
let wasAlreadyAtThisProgress = progress == index
withAnimation(.easeInOut(duration: 0.2)) {
if wasAlreadyAtThisProgress {
progress = 0
} else {
progress = index
}
}
updateLogStatus()
// If tapping the last tile to complete, go back to list
if isLastTile && !wasAlreadyAtThisProgress {
dismiss()
}
}
}
}
}
Section(header: Text("Plan")) {
Stepper("\(workoutLog.sets) sets", value: Binding(
get: { Int(workoutLog.sets) },
set: { workoutLog.sets = Int32($0) }
), in: 1...10)
.font(.title)
Stepper("\(workoutLog.reps) reps", value: Binding(
get: { Int(workoutLog.reps) },
set: { workoutLog.reps = Int32($0) }
), in: 1...25)
.font(.title)
// MARK: - Plan Section (Read-only with Edit button)
Section {
PlanTilesView(workoutLog: workoutLog)
} header: {
HStack {
Text("\(workoutLog.weight) lbs")
VStack(alignment: .trailing) {
Stepper("", value: Binding(
get: { Int(workoutLog.weight) },
set: { workoutLog.weight = Int32($0) }
), in: 1...500)
Stepper("", value: Binding(
get: { Int(workoutLog.weight) },
set: { workoutLog.weight = Int32($0) }
), in: 1...500, step: 5)
Text("Plan")
Spacer()
Button("Edit") {
showingPlanEdit = true
}
.font(.subheadline)
.textCase(.none)
}
.font(.title)
}
// MARK: - Notes Section (Read-only with Edit button)
Section {
if let notes = workoutLog.notes, !notes.isEmpty {
Text(notes)
.foregroundColor(.primary)
} else {
Text("No notes")
.foregroundColor(.secondary)
.italic()
}
} header: {
HStack {
Text("Notes")
Spacer()
Button("Edit") {
showingNotesEdit = true
}
.font(.subheadline)
.textCase(.none)
}
}
// MARK: - Progress Tracking Chart
Section(header: Text("Progress Tracking")) {
WeightProgressionChartView(exerciseName: workoutLog.exerciseName)
}
}
.navigationTitle(workoutLog.exerciseName)
.navigationDestination(item: $navigateTo) { nextLog in
ExerciseView(
workoutLog: nextLog,
allLogs: allLogs,
currentIndex: allLogs.firstIndex(where: { $0.objectID == nextLog.objectID }) ?? 0
)
.sheet(isPresented: $showingPlanEdit) {
PlanEditView(workoutLog: workoutLog)
}
.sheet(isPresented: $showingNotesEdit) {
NotesEditView(workoutLog: workoutLog)
}
.onAppear {
progress = Int(workoutLog.currentStateIndex)
}
.onDisappear {
saveChanges()
}
}
private func updateLogStatus() {
workoutLog.currentStateIndex = Int32(progress)
if progress >= Int(workoutLog.sets) {
workoutLog.status = .completed
workoutLog.completed = true
} else if progress > 0 {
workoutLog.status = .inProgress
workoutLog.completed = false
} else {
workoutLog.status = .notStarted
workoutLog.completed = false
}
updateWorkoutStatus()
saveChanges()
}
private func updateWorkoutStatus() {
guard let workout = workoutLog.workout else { return }
let logs = workout.logsArray
let allCompleted = logs.allSatisfy { $0.status == .completed }
let anyInProgress = logs.contains { $0.status == .inProgress }
let allNotStarted = logs.allSatisfy { $0.status == .notStarted }
if allCompleted {
workout.status = .completed
workout.end = Date()
} else if anyInProgress || !allNotStarted {
workout.status = .inProgress
} else {
workout.status = .notStarted
}
}
private func saveChanges() {
try? viewContext.save()
}
}
private func navigateToPrevious() {
guard currentIndex > 0 else { return }
let previousIndex = currentIndex - 1
navigateTo = allLogs[previousIndex]
// MARK: - Plan Tiles View
struct PlanTilesView: View {
@ObservedObject var workoutLog: WorkoutLog
var body: some View {
if workoutLog.loadTypeEnum == .duration {
// Duration layout: Sets | Duration
HStack(spacing: 0) {
PlanTile(label: "Sets", value: "\(workoutLog.sets)")
PlanTile(label: "Duration", value: formattedDuration)
}
} else {
// Weight layout: Sets | Reps | Weight
HStack(spacing: 0) {
PlanTile(label: "Sets", value: "\(workoutLog.sets)")
PlanTile(label: "Reps", value: "\(workoutLog.reps)")
PlanTile(label: "Weight", value: "\(workoutLog.weight) lbs")
}
}
}
private func navigateToNext() {
guard currentIndex < allLogs.count - 1 else { return }
let nextIndex = currentIndex + 1
navigateTo = allLogs[nextIndex]
private var formattedDuration: String {
let mins = workoutLog.durationMinutes
let secs = workoutLog.durationSeconds
if mins > 0 && secs > 0 {
return "\(mins)m \(secs)s"
} else if mins > 0 {
return "\(mins) min"
} else if secs > 0 {
return "\(secs) sec"
} else {
return "0 sec"
}
}
}
struct PlanTile: View {
let label: String
let value: String
var body: some View {
VStack(spacing: 4) {
Text(label)
.font(.caption)
.foregroundColor(.secondary)
Text(value)
.font(.title2)
.fontWeight(.semibold)
.foregroundColor(.primary)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 12)
.background(Color(.systemGray6))
.cornerRadius(8)
}
}