// // CreateViewRepsWeightView.swift // Werkout_ios // // Created by Trey Tartt on 6/18/23. // import SwiftUI import AVFoundation struct CreateExerciseActionsView: View { @ObservedObject var workoutExercise: CreateWorkoutExercise @ObservedObject var superset: CreateWorkoutSuperSet var viewModel: WorkoutViewModel @State var avPlayer = AVPlayer(url: URL(string: "https://dev.werkout.fitness/media/exercise_videos/2_Dumbbell_Lateral_Lunges.mp4") ?? URL(fileURLWithPath: "/dev/null")) @State private var currentVideoURL: URL? @State var videoExercise: Exercise? { didSet { if let viddd = self.videoExercise?.videoURL, let url = URL(string: BaseURLs.currentBaseURL + viddd) { updatePlayer(for: url) } } } var body: some View { VStack { VStack { HStack { Text("Reps: ") Text("\(workoutExercise.reps)") .foregroundColor(workoutExercise.reps == 0 && workoutExercise.duration == 0 ? .red : Color(uiColor: .label)) .bold() Stepper("", onIncrement: { workoutExercise.increaseReps() }, onDecrement: { workoutExercise.decreaseReps() }) .accessibilityLabel("Reps") } } HStack { Text("Weight: ") Text("\(workoutExercise.weight)") Stepper("", onIncrement: { workoutExercise.increaseWeight() }, onDecrement: { workoutExercise.decreaseWeight() }) .accessibilityLabel("Weight") } HStack { Text("Duration: ") Text("\(workoutExercise.duration)") .foregroundColor( workoutExercise.reps == 0 && workoutExercise.duration == 0 ? .red : Color( uiColor: .label ) ) .bold() Stepper("", onIncrement: { workoutExercise.increaseDuration() }, onDecrement: { workoutExercise.decreaseDuration() }) .accessibilityLabel("Duration") } HStack { Spacer() Button(action: { videoExercise = workoutExercise.exercise }) { Image(systemName: "video.fill") } .frame(width: 88, height: 44) .foregroundColor(.white) .background(.blue) .cornerRadius(Constants.buttonRadius) .buttonStyle(BorderlessButtonStyle()) .accessibilityLabel("Preview exercise video") .accessibilityHint("Opens a video preview for this exercise") Spacer() Spacer() Button(action: { superset .deleteExerciseForChosenSuperset(exercise: workoutExercise) viewModel.increaseRandomNumberForUpdating() }) { Image(systemName: "trash.fill") } .frame(width: 88, height: 44) .foregroundColor(.white) .background(.red) .cornerRadius(Constants.buttonRadius) .buttonStyle(BorderlessButtonStyle()) .accessibilityLabel("Delete exercise") .accessibilityHint("Removes this exercise from the superset") Spacer() } } .sheet(item: $videoExercise) { exercise in PlayerView(player: $avPlayer) .onAppear{ avPlayer.isMuted = true avPlayer.play() } } .onDisappear { avPlayer.pause() } } private func updatePlayer(for url: URL) { if currentVideoURL == url { avPlayer.seek(to: .zero) avPlayer.isMuted = true avPlayer.play() return } currentVideoURL = url avPlayer = AVPlayer(url: url) avPlayer.isMuted = true avPlayer.play() } } //struct CreateViewRepsWeightView_Previews: PreviewProvider { // static var previews: some View { // CreateViewRepsWeightView() // } //}