// // ExternalView.swift // Werkout_ios // // Created by Trey Tartt on 6/13/23. // import SwiftUI import AVKit struct ExternalWorkoutDetailView: View { @StateObject var bridgeModule = BridgeModule.shared @State var avPlayer = AVPlayer(url: URL(string: "https://dev.werkout.fitness/media/exercise_videos/2_Dumbbell_Lateral_Lunges.mp4") ?? URL(fileURLWithPath: "/dev/null")) @State var smallAVPlayer = 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 private var currentSmallVideoURL: URL? @AppStorage(Constants.extThotStyle) private var extThotStyle: ThotStyle = .never @AppStorage(Constants.extShowNextVideo) private var extShowNextVideo: Bool = false @AppStorage(Constants.thotGenderOption) private var thotGenderOption: String = "female" var body: some View { ZStack { if let workout = bridgeModule.currentWorkoutInfo.workout { GeometryReader { metrics in VStack { HStack { if extThotStyle != .off { PlayerView(player: $avPlayer) .frame(width: metrics.size.width * 0.5, height: metrics.size.height * 0.8) .onAppear{ avPlayer.isMuted = true avPlayer.play() } } VStack { ExtExerciseList(workout: workout, allSupersetExecerciseIndex: bridgeModule.currentWorkoutInfo.allSupersetExecerciseIndex) } .frame(width: metrics.size.width * 0.4, height: metrics.size.height * 0.8) .padding([.top, .bottom], 20) } HStack { ExtCountdownView() .padding(.leading, 50) .padding(.bottom, 20) if extShowNextVideo && extThotStyle != .off { PlayerView(player: $smallAVPlayer) .frame(width: metrics.size.width * 0.2, height: metrics.size.height * 0.2) .onAppear{ smallAVPlayer.isMuted = true smallAVPlayer.play() } } } .background(Color(uiColor: .tertiarySystemGroupedBackground)) } } } else { Image("icon") .resizable() .edgesIgnoringSafeArea(.all) .scaledToFill() } VStack { HStack { if bridgeModule.currentWorkoutRunTimeInSeconds > -1 { Text(" \(Double(bridgeModule.currentWorkoutRunTimeInSeconds).asString(style: .positional)) ") .font(Font.system(size: 120)) .minimumScaleFactor(0.01) .lineLimit(1) .padding() .bold() .foregroundColor(.white) .background( Capsule() .strokeBorder(Color.black, lineWidth: 0.8) .background(Color(uiColor: UIColor(red: 148/255, green: 0, blue: 211/255, alpha: 0.5))) .clipped() ) .clipShape(Capsule()) } Spacer() } .padding(.leading, 50) Spacer() } } .onChange(of: bridgeModule.isInWorkout, perform: { _ in playVideos() }) .onChange(of: bridgeModule.currentWorkoutInfo.allSupersetExecerciseIndex, perform: { _ in playVideos() }) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(bridgeModule.currentWorkoutInfo.workout == nil ? Color(red: 157/255, green: 138/255, blue: 255/255) : Color(uiColor: .systemBackground)) .onReceive(NotificationCenter.default.publisher( for: UIScene.willEnterForegroundNotification)) { _ in avPlayer.play() smallAVPlayer.play() } .onDisappear { avPlayer.pause() smallAVPlayer.pause() } } func playVideos() { if let currentExtercise = bridgeModule.currentWorkoutInfo.currentExercise { if let videoURL = VideoURLCreator.videoURL( thotStyle: extThotStyle, gender: thotGenderOption, defaultVideoURLStr: currentExtercise.exercise.videoURL, exerciseName: currentExtercise.exercise.name, workout: bridgeModule.currentWorkoutInfo.workout) { updateMainPlayer(for: videoURL) } if let smallVideoURL = VideoURLCreator.videoURL( thotStyle: .never, gender: thotGenderOption, defaultVideoURLStr: BridgeModule.shared.currentWorkoutInfo.nextExerciseInfo?.exercise.videoURL, exerciseName: BridgeModule.shared.currentWorkoutInfo.nextExerciseInfo?.exercise.name, workout: bridgeModule.currentWorkoutInfo.workout), extShowNextVideo { updateSmallPlayer(for: smallVideoURL) } } } private func updateMainPlayer(for url: URL) { if currentVideoURL == url { avPlayer.seek(to: .zero) avPlayer.play() return } currentVideoURL = url avPlayer = AVPlayer(url: url) avPlayer.play() } private func updateSmallPlayer(for url: URL) { if currentSmallVideoURL == url { smallAVPlayer.seek(to: .zero) smallAVPlayer.play() return } currentSmallVideoURL = url smallAVPlayer = AVPlayer(url: url) smallAVPlayer.play() } } //struct ExternalWorkoutDetailView_Previews: PreviewProvider { // static var bridge = BridgeModule.shared // // static var previews: some View { // ExternalWorkoutDetailView().environmentObject({ () -> BridgeModule in // let envObj = BridgeModule.shared // envObj.currentWorkout = nil //PreviewData.workout() // bridge.currentExercise = PreviewData.workout().exercisesSortedByCreated_at.first! // return envObj // }() ) // } //}