add always sorted [WorkoutItem], ContentUnavailableView to searches, SampleData import, refactor WorkoutItem init
This commit is contained in:
@@ -5,23 +5,24 @@ struct ActiveWorkoutSession: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
|
||||
@Default(\.isWorkingOut) var isWorkingOut
|
||||
@State var isTimerRunning: Bool = true
|
||||
|
||||
@Query(sort: \WorkoutSession.name) private var workoutSessions: [WorkoutSession]
|
||||
@State private var activeWorkoutSession: WorkoutSession?
|
||||
@Query(sort: \WorkoutSession.name) var workoutSessions: [WorkoutSession]
|
||||
@State var activeWorkoutSession: WorkoutSession?
|
||||
@Default(\.activeWorkoutSessionId) var activeWorkoutSessionId
|
||||
|
||||
@Query(sort: \Workout.name) private var workouts: [Workout]
|
||||
@State private var activeWorkout: Workout?
|
||||
@Query(sort: \Workout.name) var workouts: [Workout]
|
||||
@State var activeWorkout: Workout?
|
||||
@Default(\.activeWorkoutId) var activeWorkoutId
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
List {
|
||||
Section(footer: Text(activeWorkoutSession?.creationDate.ISO8601Format() ?? "Unknown Date")) {
|
||||
Section(header: Text("Workout"), footer: Text(activeWorkoutSession?.creationDate.ISO8601Format() ?? "Unknown Date")) {
|
||||
NavigationLink(destination: {
|
||||
ItemPicker<Workout>(items: workouts, selectedItem: $activeWorkout)
|
||||
}) {
|
||||
Text(activeWorkout?.name ?? "Select your next Workout")
|
||||
Text(activeWorkout?.name ?? "Select Workout")
|
||||
}
|
||||
.onChange(of: activeWorkout) { _, newWorkout in
|
||||
if let workout = newWorkout {
|
||||
@@ -33,7 +34,7 @@ struct ActiveWorkoutSession: View {
|
||||
|
||||
if let activeWorkout = activeWorkout {
|
||||
Section(header: Text("Exercises")) {
|
||||
ForEach(activeWorkout.workoutItems.sorted(by: { $0.position < $1.position })) { workoutItem in
|
||||
ForEach(getActiveWorkoutItems(activeWorkout: activeWorkout)) { workoutItem in
|
||||
HStack {
|
||||
Text(String(workoutItem.reps))
|
||||
Text(workoutItem.name)
|
||||
@@ -47,65 +48,51 @@ struct ActiveWorkoutSession: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if true { // This condition should be more meaningful.
|
||||
VStack {
|
||||
HStack {
|
||||
Text(String(workoutSessions.count))
|
||||
Button(action: {
|
||||
// save(workoutSession: activeWorkoutSession)
|
||||
}) {
|
||||
Text("Save Session")
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
} else {
|
||||
ContentUnavailableView {
|
||||
Label("Select a Workout", systemImage: "arrow.up")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: -- Workout Controls
|
||||
Group {
|
||||
if activeWorkoutSession?.workout != nil {
|
||||
if isWorkingOut {
|
||||
// MARK: -- Stop Workout
|
||||
VStack {
|
||||
ProgressView("", value: 10, total: 100)
|
||||
TimerView(isActive: $isWorkingOut)
|
||||
.font(.title)
|
||||
.bold()
|
||||
Button(action: {
|
||||
isWorkingOut = false
|
||||
activeWorkoutSession?.startWorkoutSession()
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "stop.fill")
|
||||
Text("Stop Workout")
|
||||
}
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.bold()
|
||||
.tint(.red)
|
||||
}
|
||||
} else {
|
||||
// MARK: -- Start Workout
|
||||
Button(action: {
|
||||
isWorkingOut = true
|
||||
activeWorkoutSession?.stopWorkoutSession()
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "play.fill")
|
||||
Text("Start Workout")
|
||||
}
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.bold()
|
||||
.tint(.green)
|
||||
}
|
||||
if (isWorkingOut) {
|
||||
if activeWorkoutSession != nil {
|
||||
ActiveWorkoutSessionControls(
|
||||
session: Binding(
|
||||
get: { self.activeWorkoutSession! },
|
||||
set: { self.activeWorkoutSession = $0 }
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Workout Session")
|
||||
.navigationTitle("Session")
|
||||
.toolbar {
|
||||
if (isWorkingOut) {
|
||||
Button(action: {
|
||||
isWorkingOut = false
|
||||
activeWorkoutSession?.stop()
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "stop.fill")
|
||||
Text("Stop")
|
||||
}
|
||||
}
|
||||
.bold()
|
||||
.tint(.red)
|
||||
} else {
|
||||
Button(action: {
|
||||
isWorkingOut = true
|
||||
activeWorkoutSession?.start()
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "play.fill")
|
||||
Text("Start")
|
||||
}
|
||||
}
|
||||
.bold()
|
||||
.tint(.green)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
// Load the active workout session and workout onAppear
|
||||
if let activeWorkoutSession = getItem(from: workoutSessions, by: activeWorkoutSessionId) {
|
||||
@@ -128,15 +115,22 @@ struct ActiveWorkoutSession: View {
|
||||
self.activeWorkoutSession = newWorkoutSession
|
||||
}
|
||||
|
||||
private func getActiveWorkoutItems(activeWorkout: Workout?) -> [WorkoutItem] {
|
||||
guard let activeWorkout else { return [] }
|
||||
return activeWorkout.getWorkoutItems()
|
||||
}
|
||||
|
||||
// TODO: Put this somewhere general
|
||||
private func getItem<Item: Nameable>(from array: [Item], by id: String) -> Item? {
|
||||
let filteredItems = array.filter { $0.id == UUID(uuidString: id) }
|
||||
return filteredItems.count == 1 ? filteredItems.first : nil
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
#Preview("RR Selected") {
|
||||
@Previewable @State var activeWorkout = Workout.sampleData.first!
|
||||
NavigationStack {
|
||||
ActiveWorkoutSession()
|
||||
ActiveWorkoutSession(activeWorkout: activeWorkout)
|
||||
}
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user