change folders to the "feature" mindset
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
// This view can only be viewed, if there exists a WorkoutSession that can be considered active (a person working out).
|
||||
struct ActiveWorkoutSession: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
|
||||
@Default(\.isWorkingOut) var isWorkingOut
|
||||
@State var isTimerRunning: Bool = true
|
||||
|
||||
@Query(sort: \WorkoutSession.name) var workoutSessions: [WorkoutSession]
|
||||
@Binding var activeWorkoutSession: WorkoutSession?
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
List {
|
||||
Section(header: Text("Workout")) {
|
||||
Text(activeWorkoutSession!.name)
|
||||
}
|
||||
Section(header: Text("Exercises")) {
|
||||
ForEach(activeWorkoutSession!.workoutSessionItems) { workoutItem in
|
||||
ActiveWorkoutSessionListItem(workoutItem: workoutItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Session")
|
||||
.toolbar {
|
||||
Button(action: {
|
||||
isWorkingOut = false
|
||||
activeWorkoutSession?.stop()
|
||||
}) {
|
||||
HStack {
|
||||
Image(systemName: "stop.fill")
|
||||
Text("Stop")
|
||||
}
|
||||
}
|
||||
.bold()
|
||||
.fontDesign(.rounded)
|
||||
.tint(.red)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@Previewable @State var activeWorkoutSession: WorkoutSession?
|
||||
@Previewable @State var workout = Workout.sampleData.first!
|
||||
|
||||
NavigationStack {
|
||||
ActiveWorkoutSession(activeWorkoutSession: $activeWorkoutSession)
|
||||
}
|
||||
.onAppear {
|
||||
Defaults.shared.isWorkingOut = false
|
||||
}
|
||||
.modelContainer(SampleData.shared.modelContainer)
|
||||
}
|
||||
|
||||
//#Preview("Empty modelContainer") {
|
||||
// @Previewable @State var activeWorkoutSession: WorkoutSession?
|
||||
//
|
||||
// NavigationStack {
|
||||
// ActiveWorkoutSession(activeWorkoutSession: $activeWorkoutSession)
|
||||
// }
|
||||
// .onAppear {
|
||||
// Defaults.shared.isWorkingOut = false
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user