add apple tv app

This commit is contained in:
Trey t
2024-06-18 12:03:56 -05:00
parent addeca4ead
commit 7d2b6b3e6e
134 changed files with 869 additions and 37 deletions

View File

@@ -0,0 +1,21 @@
//
// CurrentWorkoutElapsedTimeView.swift
// Werkout_ios
//
// Created by Trey Tartt on 7/7/23.
//
import SwiftUI
struct CurrentWorkoutElapsedTimeView: View {
@ObservedObject var bridgeModule = BridgeModule.shared
var body: some View {
if bridgeModule.currentWorkoutRunTimeInSeconds > -1 {
VStack {
Text("\(Double(bridgeModule.currentWorkoutRunTimeInSeconds).asString(style: .positional))")
.font(.title2)
}
}
}
}

View File

@@ -0,0 +1,160 @@
//
// ExerciseListView.swift
// Werkout_ios
//
// Created by Trey Tartt on 7/7/23.
//
import SwiftUI
import AVKit
struct ExerciseListView: View {
@AppStorage(Constants.phoneThotStyle) private var phoneThotStyle: ThotStyle = .never
@ObservedObject var bridgeModule = BridgeModule.shared
@State var avPlayer = AVPlayer(url: URL(string: "https://dev.werkout.fitness/media/exercise_videos/2_Dumbbell_Lateral_Lunges.mp4")!)
var workout: Workout
@Binding var showExecersizeInfo: Bool
@AppStorage(Constants.thotGenderOption) private var thotGenderOption: String = "female"
@State var videoExercise: Exercise? {
didSet {
if let videoURL = VideoURLCreator.videoURL(
thotStyle: phoneThotStyle,
gender: thotGenderOption,
defaultVideoURLStr: self.videoExercise?.videoURL,
exerciseName: self.videoExercise?.name,
workout: bridgeModule.currentExerciseInfo.workout) {
avPlayer = AVPlayer(url: videoURL)
avPlayer.play()
}
}
}
var body: some View {
if let supersets = workout.supersets {
ScrollViewReader { proxy in
List() {
ForEach(supersets.indices, id: \.self) { supersetIndex in
let superset = supersets[supersetIndex]
Section(content: {
ForEach(superset.exercises.indices, id: \.self) { exerciseIndex in
let supersetExecercise = superset.exercises[exerciseIndex]
VStack {
HStack {
if bridgeModule.isInWorkout &&
supersetIndex == bridgeModule.currentExerciseInfo.supersetIndex &&
exerciseIndex == bridgeModule.currentExerciseInfo.exerciseIndex {
Image(systemName: "figure.run")
.foregroundColor(Color("appColor"))
}
Text(supersetExecercise.exercise.extName)
Spacer()
if let reps = supersetExecercise.reps,
reps > 0 {
HStack {
Image(systemName: "number")
.foregroundColor(.white)
.frame(width: 20, alignment: .leading)
Text("\(reps)")
.foregroundColor(.white)
.frame(width: 30, alignment: .trailing)
}
.padding([.top, .bottom], 5)
.padding([.leading], 10)
.padding([.trailing], 15)
.background(.blue)
.cornerRadius(5, corners: [.topLeft, .bottomLeft])
.frame(alignment: .trailing)
}
if let duration = supersetExecercise.duration,
duration > 0 {
HStack {
Image(systemName: "stopwatch")
.foregroundColor(.white)
.frame(width: 20, alignment: .leading)
Text("\(duration)")
.foregroundColor(.white)
.frame(width: 30, alignment: .trailing)
}
.padding([.top, .bottom], 5)
.padding([.leading], 10)
.padding([.trailing], 15)
.background(.green)
.cornerRadius(5, corners: [.topLeft, .bottomLeft])
}
}
.padding(.trailing, -20)
.contentShape(Rectangle())
.onTapGesture {
if bridgeModule.isInWorkout {
bridgeModule.goToExerciseAt(section: supersetIndex, row: exerciseIndex)
} else {
videoExercise = supersetExecercise.exercise
}
}
if bridgeModule.isInWorkout &&
supersetIndex == bridgeModule.currentExerciseInfo.supersetIndex &&
exerciseIndex == bridgeModule.currentExerciseInfo.exerciseIndex &&
showExecersizeInfo {
detailView(forExercise: supersetExecercise)
}
}.id(supersetExecercise.id)
}
}, header: {
HStack {
Text(superset.name ?? "--")
.foregroundColor(Color("appColor"))
.bold()
Spacer()
Text("\(superset.rounds) rounds")
.foregroundColor(Color("appColor"))
.bold()
if let estimatedTime = superset.estimatedTime {
Text("@ " + estimatedTime.asString(style: .abbreviated))
.foregroundColor(Color("appColor"))
.bold()
}
}
})
}
}
.onChange(of: bridgeModule.currentExerciseInfo.allSupersetExecerciseIndex, perform: { newValue in
if let newCurrentExercise = bridgeModule.currentExerciseInfo.currentExercise {
withAnimation {
proxy.scrollTo(newCurrentExercise.id, anchor: .top)
}
}
})
.sheet(item: $videoExercise) { exercise in
PlayerView(player: $avPlayer)
.onAppear{
avPlayer.play()
}
}
}
}
}
func detailView(forExercise supersetExecercise: SupersetExercise) -> some View {
VStack {
Text(supersetExecercise.exercise.description)
.frame(alignment: .leading)
Divider()
Text(supersetExecercise.exercise.muscles.map({ $0.name }).joined(separator: ", "))
.frame(alignment: .leading)
}
}
}
//struct ExerciseListView_Previews: PreviewProvider {
// static var previews: some View {
// ExerciseListView(workout: PreviewData.workout(), showExecersizeInfo: )
// }
//}

View File

@@ -0,0 +1,229 @@
//
// MainView.swift
// Werkout_ios
//
// Created by Trey Tartt on 6/14/23.
//
import SwiftUI
import AVKit
struct WorkoutDetailView: View {
@StateObject var viewModel: WorkoutDetailViewModel
@State var avPlayer = AVPlayer(url: URL(string: "https://dev.werkout.fitness/media/exercise_videos/2_Dumbbell_Lateral_Lunges.mp4")!)
@StateObject var bridgeModule = BridgeModule.shared
@Environment(\.dismiss) var dismiss
@AppStorage(Constants.phoneThotStyle) private var phoneThotStyle: ThotStyle = .never
@AppStorage(Constants.thotGenderOption) private var thotGenderOption: String = "female"
enum Sheet: Identifiable {
case completedWorkout([String: Any])
var id: String { return UUID().uuidString }
}
@State var presentedSheet: Sheet?
@State var workoutToPlan: Workout?
@State var showExecersizeInfo: Bool = false
var body: some View {
ZStack {
switch viewModel.status {
case .loading:
Text("Loading")
case .showWorkout(let workout):
VStack(spacing: 0) {
if bridgeModule.isInWorkout {
HStack {
CountdownView()
}
.padding()
.frame(maxWidth: .infinity)
if phoneThotStyle != .off {
GeometryReader { metrics in
ZStack {
PlayerView(player: $avPlayer)
.frame(width: metrics.size.width * 1, height: metrics.size.height * 1)
.onAppear{
avPlayer.play()
}
Button(action: {
if let assetURL = ((avPlayer.currentItem?.asset) as? AVURLAsset)?.url,
let currentExtercise = bridgeModule.currentExerciseInfo.currentExercise,
let otherVideoURL = VideoURLCreator.videoURL(
thotStyle: VideoURLCreator.otherVideoType(forVideoURL: assetURL),
gender: thotGenderOption,
defaultVideoURLStr: currentExtercise.exercise.videoURL,
exerciseName: currentExtercise.exercise.name,
workout: bridgeModule.currentExerciseInfo.workout) {
avPlayer = AVPlayer(url: otherVideoURL)
avPlayer.play()
}
}, label: {
Image(systemName: "arrow.triangle.2.circlepath.camera.fill")
.frame(width: 44, height: 44)
.foregroundColor(Color("appColor"))
})
.foregroundColor(.blue)
.cornerRadius(4)
.frame(width: 160, height: 120)
.position(x: metrics.size.width - 22, y: metrics.size.height - 30)
Button(action: {
showExecersizeInfo.toggle()
}, label: {
Image(systemName: "info.circle.fill")
.frame(width: 44, height: 44)
.foregroundColor(Color("appColor"))
})
.foregroundColor(.blue)
.cornerRadius(4)
.frame(width: 120, height: 120)
.position(x: 22, y: metrics.size.height - 30)
}
}
.padding([.top, .bottom])
.background(Color(uiColor: .tertiarySystemBackground))
}
}
if !bridgeModule.isInWorkout {
InfoView(workout: workout)
.padding(.bottom)
}
if bridgeModule.isInWorkout {
Divider()
.background(Color(uiColor: .secondaryLabel))
HStack {
Text("\(bridgeModule.currentExerciseInfo.currentRound) of \(bridgeModule.currentExerciseInfo.numberOfRoundsInCurrentSuperSet)")
.font(.title3)
.bold()
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.leading, 10)
CurrentWorkoutElapsedTimeView()
.frame(maxWidth: .infinity, alignment: .center)
Text("\(bridgeModule.currentExerciseInfo.allSupersetExecerciseIndex+1)/\(bridgeModule.currentExerciseInfo.workout?.allSupersetExecercise?.count ?? -99)")
.font(.title3)
.bold()
.frame(maxWidth: .infinity, alignment: .trailing)
.padding(.trailing, 10)
}
.padding([.top, .bottom])
.background(Color(uiColor: .tertiarySystemBackground))
}
Divider()
.background(Color(uiColor: .secondaryLabel))
ExerciseListView(workout: workout, showExecersizeInfo: $showExecersizeInfo)
.padding([.top, .bottom], 10)
.background(Color(uiColor: .systemGroupedBackground))
ActionsView(completedWorkout: {
bridgeModule.completeWorkout()
}, planWorkout: { workout in
workoutToPlan = workout
}, workout: workout, showAddToCalendar: viewModel.isPreview, startWorkoutAction: {
startWorkout(workout: workout)
})
.frame(height: 44)
}
.sheet(item: $presentedSheet) { item in
switch item {
case .completedWorkout(let data):
CompletedWorkoutView(postData: data,
workout: workout,
completedWorkoutDismissed: { uploaded in
if uploaded {
dismiss()
}
})
}
}
.sheet(item: $workoutToPlan) { workout in
PlanWorkoutView(workout: workout, addedPlannedWorkout: {
dismiss()
})
}
}
}
.onChange(of: bridgeModule.currentExerciseInfo.allSupersetExecerciseIndex, perform: { _ in
playVideos()
})
.onChange(of: bridgeModule.isInWorkout, perform: { _ in
playVideos()
})
.onAppear{
if let currentExtercise = bridgeModule.currentExerciseInfo.currentExercise {
if let videoURL = VideoURLCreator.videoURL(
thotStyle: phoneThotStyle,
gender: thotGenderOption,
defaultVideoURLStr: currentExtercise.exercise.videoURL,
exerciseName: currentExtercise.exercise.name,
workout: bridgeModule.currentExerciseInfo.workout) {
avPlayer = AVPlayer(url: videoURL)
avPlayer.play()
}
}
}
.onReceive(NotificationCenter.default.publisher(
for: UIScene.willEnterForegroundNotification)) { _ in
avPlayer.play()
}
}
func playVideos() {
if let currentExtercise = bridgeModule.currentExerciseInfo.currentExercise {
if let videoURL = VideoURLCreator.videoURL(
thotStyle: phoneThotStyle,
gender: thotGenderOption,
defaultVideoURLStr: currentExtercise.exercise.videoURL,
exerciseName: currentExtercise.exercise.name,
workout: bridgeModule.currentExerciseInfo.workout) {
avPlayer = AVPlayer(url: videoURL)
avPlayer.play()
}
}
}
func startWorkout(workout: Workout) {
bridgeModule.completedWorkout = {
if let workoutData = createWorkoutData() {
presentedSheet = .completedWorkout(workoutData)
bridgeModule.resetCurrentWorkout()
}
}
bridgeModule.start(workout: workout)
}
func createWorkoutData() -> [String:Any]? {
guard let workoutid = bridgeModule.currentExerciseInfo.workout?.id,
let startTime = bridgeModule.workoutStartDate?.timeFormatForUpload,
let endTime = bridgeModule.workoutEndDate?.timeFormatForUpload else {
return nil
}
let postBody = [
"difficulty": 1,
"workout_start_time": startTime,
"workout_end_time": endTime,
"workout": workoutid,
"total_time": bridgeModule.currentWorkoutRunTimeInSeconds
] as [String : Any]
return postBody
}
}
struct WorkoutDetailView_Previews: PreviewProvider {
static let workoutDetail = PreviewData.workout()
static var previews: some View {
WorkoutDetailView(viewModel: WorkoutDetailViewModel(workout: WorkoutDetailView_Previews.workoutDetail, status: .showWorkout(WorkoutDetailView_Previews.workoutDetail), isPreview: true))
}
}

View File

@@ -0,0 +1,39 @@
//
// MainViewViewModel.swift
// Werkout_ios
//
// Created by Trey Tartt on 6/14/23.
//
import Combine
import Foundation
class WorkoutDetailViewModel: ObservableObject {
enum WorkoutDetailViewModelStatus {
case loading
case showWorkout(Workout)
}
@Published var status: WorkoutDetailViewModelStatus
let isPreview: Bool
init(workout: Workout, status: WorkoutDetailViewModelStatus? = nil, isPreview: Bool) {
self.status = .loading
self.isPreview = isPreview
if let passedStatus = status {
self.status = passedStatus
} else {
WorkoutDetailFetchable(workoutID: workout.id).fetch(completion: { result in
switch result {
case .success(let model):
DispatchQueue.main.async {
self.status = .showWorkout(model)
}
case .failure(let failure):
fatalError("failed \(failure.localizedDescription)")
}
})
}
}
}