diff --git a/Werkout_ios.xcodeproj/project.pbxproj b/Werkout_ios.xcodeproj/project.pbxproj index f863a26..b1b8566 100644 --- a/Werkout_ios.xcodeproj/project.pbxproj +++ b/Werkout_ios.xcodeproj/project.pbxproj @@ -44,6 +44,9 @@ 1CC092F12C1FAD1E0004E1E6 /* CompletedWorkoutsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC092F02C1FAD1E0004E1E6 /* CompletedWorkoutsView.swift */; }; 1CC092F32C1FADDA0004E1E6 /* ThotPreferenceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC092F22C1FADDA0004E1E6 /* ThotPreferenceView.swift */; }; 1CC092F52C1FAE7B0004E1E6 /* ShowNextUpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC092F42C1FAE7B0004E1E6 /* ShowNextUpView.swift */; }; + 1CC092F72C1FAFD50004E1E6 /* AllMusclesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC092F62C1FAFD50004E1E6 /* AllMusclesView.swift */; }; + 1CC092F92C1FB1420004E1E6 /* AllExerciseView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC092F82C1FB1420004E1E6 /* AllExerciseView.swift */; }; + 1CC092FB2C1FB3320004E1E6 /* AllEquipmentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CC092FA2C1FB3320004E1E6 /* AllEquipmentView.swift */; }; 1CD0C6632A5AF62900970E52 /* WorkoutOverviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD0C6622A5AF62900970E52 /* WorkoutOverviewView.swift */; }; 1CD0C6672A5CA19600970E52 /* BaseURLs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD0C6662A5CA19600970E52 /* BaseURLs.swift */; }; 1CD0C6682A5CA1A200970E52 /* BaseURLs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CD0C6662A5CA19600970E52 /* BaseURLs.swift */; }; @@ -168,6 +171,9 @@ 1CC092F02C1FAD1E0004E1E6 /* CompletedWorkoutsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompletedWorkoutsView.swift; sourceTree = ""; }; 1CC092F22C1FADDA0004E1E6 /* ThotPreferenceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThotPreferenceView.swift; sourceTree = ""; }; 1CC092F42C1FAE7B0004E1E6 /* ShowNextUpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowNextUpView.swift; sourceTree = ""; }; + 1CC092F62C1FAFD50004E1E6 /* AllMusclesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllMusclesView.swift; sourceTree = ""; }; + 1CC092F82C1FB1420004E1E6 /* AllExerciseView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllExerciseView.swift; sourceTree = ""; }; + 1CC092FA2C1FB3320004E1E6 /* AllEquipmentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllEquipmentView.swift; sourceTree = ""; }; 1CD0C6622A5AF62900970E52 /* WorkoutOverviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkoutOverviewView.swift; sourceTree = ""; }; 1CD0C6662A5CA19600970E52 /* BaseURLs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseURLs.swift; sourceTree = ""; }; 1CD0C66B2A5E4EA100970E52 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = ""; }; @@ -263,11 +269,14 @@ 1CC092EB2C1FAC2A0004E1E6 /* subview */ = { isa = PBXGroup; children = ( + 1CC092F62C1FAFD50004E1E6 /* AllMusclesView.swift */, 1CC092EC2C1FAC730004E1E6 /* Logoutview.swift */, 1CC092EE2C1FACFC0004E1E6 /* NameView.swift */, 1CC092F02C1FAD1E0004E1E6 /* CompletedWorkoutsView.swift */, 1CC092F22C1FADDA0004E1E6 /* ThotPreferenceView.swift */, 1CC092F42C1FAE7B0004E1E6 /* ShowNextUpView.swift */, + 1CC092F82C1FB1420004E1E6 /* AllExerciseView.swift */, + 1CC092FA2C1FB3320004E1E6 /* AllEquipmentView.swift */, ); path = subview; sourceTree = ""; @@ -617,6 +626,7 @@ 1CF65A692A3C018F0042FFBD /* AccountView.swift in Sources */, 1CC092EF2C1FACFC0004E1E6 /* NameView.swift in Sources */, 1CF65A4E2A39FF200042FFBD /* WorkoutDetailViewModel.swift in Sources */, + 1CC092FB2C1FB3320004E1E6 /* AllEquipmentView.swift in Sources */, 1CF65A472A39FB6C0042FFBD /* RegisteredUser.swift in Sources */, 1CF65AB32A452F360042FFBD /* WatchPackageModel.swift in Sources */, 1CD0C6672A5CA19600970E52 /* BaseURLs.swift in Sources */, @@ -633,6 +643,7 @@ 1CF65A592A3BF4B60042FFBD /* Muscle.swift in Sources */, 1CC092F32C1FADDA0004E1E6 /* ThotPreferenceView.swift in Sources */, 1C4AFF1E2A7579410027710B /* NSFWVideo.swift in Sources */, + 1CC092F72C1FAFD50004E1E6 /* AllMusclesView.swift in Sources */, 1C5190D02A589D5F00885849 /* AllWorkoutsListView.swift in Sources */, 1C5190CA2A589CEC00885849 /* ExerciseListView.swift in Sources */, 1C4AFF202A8800860027710B /* AudioQueue.swift in Sources */, @@ -658,6 +669,7 @@ 1CF65A5B2A3BF4BE0042FFBD /* Equipment.swift in Sources */, 1C4AFF152A60F25F0027710B /* ThotStyle.swift in Sources */, 1C5190C82A589CDA00885849 /* CurrentWorkoutElapsedTimeView.swift in Sources */, + 1CC092F92C1FB1420004E1E6 /* AllExerciseView.swift in Sources */, 1CF65A452A39FB550042FFBD /* Exercise.swift in Sources */, 1CF65A612A3BF6020042FFBD /* AddExerciseView.swift in Sources */, 1C485C872A4915C400A6F896 /* CreateWorkoutItemPickerView.swift in Sources */, diff --git a/Werkout_ios/APIModels/Equipment.swift b/Werkout_ios/APIModels/Equipment.swift index d8f9366..80aae65 100644 --- a/Werkout_ios/APIModels/Equipment.swift +++ b/Werkout_ios/APIModels/Equipment.swift @@ -7,7 +7,7 @@ import Foundation -struct Equipment: Codable { +struct Equipment: Codable, Identifiable, Equatable { let id: Int let name, createdAt, updatedAt: String let is_weight: Bool? diff --git a/Werkout_ios/APIModels/Exercise.swift b/Werkout_ios/APIModels/Exercise.swift index f1af8b0..4bca2a5 100644 --- a/Werkout_ios/APIModels/Exercise.swift +++ b/Werkout_ios/APIModels/Exercise.swift @@ -37,19 +37,24 @@ struct SupersetExercise: Identifiable, Codable, Equatable, Hashable { } } -struct Exercise: Identifiable, Codable, Equatable { +struct Exercise: Identifiable, Codable, Equatable, Hashable { static func == (lhs: Exercise, rhs: Exercise) -> Bool { lhs.id == rhs.id } + public func hash(into hasher: inout Hasher) { + return hasher.combine(id) + } + let id: Int let equipment: [Equipment] let muscles: [Muscle] let audioURL, videoURL, createdAt, updatedAt: String - let name, description, side: String + let name, description: String + let side, jointsUsed, movementPatterns: String? let isTwoDumbbells, isTrackableDistance, isAlternating, isWeight: Bool let isDistance, isDuration, isReps: Bool - let jointsUsed, movementPatterns, equipmentRequired, muscleGroups: String + let equipmentRequired, muscleGroups: String? let synonyms: String? enum CodingKeys: String, CodingKey { @@ -74,8 +79,8 @@ struct Exercise: Identifiable, Codable, Equatable { } var extName: String { - if side.count > 0 { - var returnString = name + " - " + side + if side != nil && side!.count > 0 { + var returnString = name + " - " + side! returnString = returnString.replacingOccurrences(of: "_", with: " ") return returnString.capitalized } @@ -83,10 +88,10 @@ struct Exercise: Identifiable, Codable, Equatable { } var spacedMuscleGroups: String { - return muscleGroups.replacingOccurrences(of: ",", with: ", ") + return muscleGroups?.replacingOccurrences(of: ",", with: ", ") ?? "" } var spacedEquipmentRequired: String { - return equipmentRequired.replacingOccurrences(of: ",", with: ", ") + return equipmentRequired?.replacingOccurrences(of: ",", with: ", ") ?? "" } } diff --git a/Werkout_ios/APIModels/Muscle.swift b/Werkout_ios/APIModels/Muscle.swift index a9d3378..50159d2 100644 --- a/Werkout_ios/APIModels/Muscle.swift +++ b/Werkout_ios/APIModels/Muscle.swift @@ -7,7 +7,7 @@ import Foundation -struct Muscle: Codable { +struct Muscle: Codable, Identifiable, Equatable { let id: Int let name, createdAt, updatedAt: String diff --git a/Werkout_ios/Views/AddExercise/AddExerciseView.swift b/Werkout_ios/Views/AddExercise/AddExerciseView.swift index 874be59..c9aa86e 100644 --- a/Werkout_ios/Views/AddExercise/AddExerciseView.swift +++ b/Werkout_ios/Views/AddExercise/AddExerciseView.swift @@ -8,7 +8,6 @@ import Foundation import SwiftUI import Combine -import AVKit struct AddExerciseView: View { enum CreateWorkoutItemPickerViewType { @@ -19,117 +18,46 @@ struct AddExerciseView: View { @State var selectedMuscles = [Muscle]() @State var selectedEquipment = [Equipment]() @State var filteredExercises = [Exercise]() - + @StateObject var bridgeModule = BridgeModule.shared - @Environment(\.dismiss) var dismiss - var selectedExercise: ((Exercise) -> Void) - @State var createWorkoutItemPickerViewModel: CreateWorkoutItemPickerViewModel? - @State var createWorkoutItemPickerViewType: CreateWorkoutItemPickerViewType? - @State var searchString: String = "" - @State var videoExercise: Exercise? { - didSet { - if let viddd = self.videoExercise?.videoURL, - let url = URL(string: BaseURLs.currentBaseURL + viddd) { - self.avPlayer = AVPlayer(url: url) - } - } - } - @State var avPlayer = AVPlayer(url: URL(string: "https://dev.werkout.fitness/media/exercise_videos/2_Dumbbell_Lateral_Lunges.mp4")!) + let selectedExercise: ((Exercise) -> Void) var body: some View { VStack { - AllExerciseList() - .padding(.top) + AllExerciseView(filteredExercises: $filteredExercises, + selectedExercise: { excercise in + selectedExercise(excercise) + }) + .padding(.top) - TextField("Filter", text: $searchString) - .padding() - HStack { - muscleView() + AllMusclesView(selectedMuscles: $selectedMuscles) .frame(maxWidth: .infinity) Divider() - equipmentView() + AllEquipmentView(selectedEquipment: $selectedEquipment) .frame(maxWidth: .infinity) } .padding(.top) .frame(height: 44) } - .onAppear{ - if #function.hasPrefix("__preview") { - DataStore.shared.setupFakeData() - } - guard let exercises = DataStore.shared.allExercise, - let muscles = DataStore.shared.allMuscles, - let equipment = DataStore.shared.allEquipment else { - return - } - selectedMuscles = muscles - selectedEquipment = equipment - filteredExercises = exercises - } - .sheet(item: $videoExercise) { exercise in - PlayerView(player: $avPlayer) - .onAppear{ - avPlayer.play() - } - } - .sheet(item: $createWorkoutItemPickerViewModel) { item in - CreateWorkoutItemPickerView(viewModel: item, completed: { selectedids in - if let viewType = createWorkoutItemPickerViewType { - switch viewType { - case .muscles: - if let muscles = DataStore.shared.allMuscles { - selectedMuscles.removeAll() - for id in selectedids { - if let muscle = muscles.first(where: { - $0.id == id - }) { - selectedMuscles.append(muscle) - } - } - } - case .equipment: - if let equipment = DataStore.shared.allEquipment { - selectedEquipment.removeAll() - for id in selectedids { - if let equipment = equipment.first(where: { - $0.id == id - }) { - selectedEquipment.append(equipment) - } - } - } - } - } - - filterExercises() - }) - } + .onChange(of: selectedMuscles, perform: { _ in + filterExercises() + }) .onChange(of: selectedEquipment, perform: { _ in + filterExercises() + }) } func filterExercises() { - if selectedMuscles.count == 0 { - filteredExercises = [Exercise]() - return - } - - if selectedEquipment.count == 0 { - filteredExercises = [Exercise]() + guard let exercises = DataStore.shared.allExercise else { + filteredExercises = [] return } - guard let exercises = DataStore.shared.allExercise, - let muscles = DataStore.shared.allMuscles, - let equipment = DataStore.shared.allEquipment else { - filteredExercises = [Exercise]() - return - } - - filteredExercises = exercises.filter({ exercise in + let filtered = exercises.filter({ exercise in var hasCorrectMuscles = false - if selectedMuscles.count == muscles.count { + if selectedMuscles.count == 0 { hasCorrectMuscles = true } else { let exerciseMuscleIds = exercise.muscles.map({ $0.id }) @@ -137,13 +65,12 @@ struct AddExerciseView: View { // if one items match if exerciseMuscleIds.contains(where: selctedMuscleIds.contains) { // if all items match -// if exerciseMuscleIds.contains(selctedMuscleIds) { hasCorrectMuscles = true } } var hasCorrectEquipment = false - if selectedEquipment.count == equipment.count { + if selectedEquipment.count == 0 { hasCorrectEquipment = true } else { let exerciseEquipmentIds = exercise.equipment.map({ $0.id }) @@ -151,121 +78,18 @@ struct AddExerciseView: View { // if one items match if exerciseEquipmentIds.contains(where: selctedEquipmentIds.contains) { // if all items match -// if exerciseEquipmentIds.contains(selctedEquipmentIds) { hasCorrectEquipment = true } } - return hasCorrectMuscles && hasCorrectEquipment }) - } - func muscleView() -> some View { - VStack { - if let _ = DataStore.shared.allMuscles { - Text("Select Muscles") - .foregroundColor(.cyan) - Text("\(selectedMuscles.count) Selected") - } - } - .onTapGesture { - if let muscles = DataStore.shared.allMuscles { - var createWorkoutItemPickerModels = [CreateWorkoutItemPickerModel]() - muscles.forEach({ - let model = CreateWorkoutItemPickerModel(id: $0.id, name: $0.name.lowercased()) - createWorkoutItemPickerModels.append(model) - }) - createWorkoutItemPickerModels = createWorkoutItemPickerModels.sorted(by: { - $0.name < $1.name - }) - let selectedIds = selectedMuscles.map { $0.id } - createWorkoutItemPickerViewModel = CreateWorkoutItemPickerViewModel(allValues: createWorkoutItemPickerModels, selectedIds: selectedIds) - createWorkoutItemPickerViewType = .muscles - } - } - } - - func equipmentView() -> some View { - VStack { - if let _ = DataStore.shared.allEquipment { - Text("Select Equipment") - .foregroundColor(.cyan) - Text("\(selectedEquipment.count) Selected") - } - } - .onTapGesture { - if let equipment = DataStore.shared.allEquipment { - var createWorkoutItemPickerModels = [CreateWorkoutItemPickerModel]() - equipment.forEach({ - let model = CreateWorkoutItemPickerModel(id: $0.id, - name: $0.name.lowercased()) - createWorkoutItemPickerModels.append(model) - }) - createWorkoutItemPickerModels = createWorkoutItemPickerModels.sorted(by: { - $0.name < $1.name - }) - let selectedIds = selectedEquipment.map { $0.id } - createWorkoutItemPickerViewModel = CreateWorkoutItemPickerViewModel(allValues: createWorkoutItemPickerModels, selectedIds: selectedIds) - createWorkoutItemPickerViewType = .equipment - } - } - } - - func AllExerciseList() -> some View { - List() { - ForEach(filteredExercises.indices, id: \.self) { i in - let exercise = filteredExercises[i] - if searchString.isEmpty || (exercise.name.lowercased().contains(searchString.lowercased()) || exercise.muscleGroups.lowercased().contains(searchString.lowercased())) { - HStack { - VStack { - Text(exercise.name) - .frame(maxWidth: .infinity, alignment: .leading) - - if !exercise.side.isEmpty { - Text(exercise.side) - .font(.footnote) - .frame(maxWidth: .infinity, alignment: .leading) - } - - if !exercise.equipmentRequired.isEmpty { - Text(exercise.spacedEquipmentRequired) - .font(.footnote) - .frame(maxWidth: .infinity, alignment: .leading) - } - - if !exercise.muscleGroups.isEmpty { - Text(exercise.spacedMuscleGroups) - .font(.footnote) - .frame(maxWidth: .infinity, alignment: .leading) - } - } - .contentShape(Rectangle()) - .onTapGesture { - selectedExercise(exercise) - dismiss() - } - Button(action: { - videoExercise = exercise - }) { - ZStack { - Circle() - .fill(.blue) - .frame(width: 33, height: 33) - Image(systemName: "video.fill") - .frame(width: 33, height: 33) - .foregroundColor(.white ) - } - } - .frame(width: 33, height: 33) - } - } - } - } + filteredExercises = filtered } } -struct AddExerciseView_Previews: PreviewProvider { - static var previews: some View { - AddExerciseView(selectedExercise: { _ in }) - } -} +//struct AddExerciseView_Previews: PreviewProvider { +// static var previews: some View { +// AddExerciseView(selectedExercise: { _ in }) +// } +//} diff --git a/Werkout_ios/Views/CreateWorkout/CreateWorkoutMainView.swift b/Werkout_ios/Views/CreateWorkout/CreateWorkoutMainView.swift index 9d98434..1d7d2ee 100644 --- a/Werkout_ios/Views/CreateWorkout/CreateWorkoutMainView.swift +++ b/Werkout_ios/Views/CreateWorkout/CreateWorkoutMainView.swift @@ -34,8 +34,8 @@ struct CreateWorkoutMainView: View { Text(exercise.wrappedValue.exercise.name) .font(.title2) .frame(maxWidth: .infinity) - if exercise.wrappedValue.exercise.side.count > 0 { - Text(exercise.wrappedValue.exercise.side) + if exercise.wrappedValue.exercise.side != nil && exercise.wrappedValue.exercise.side!.count > 0 { + Text(exercise.wrappedValue.exercise.side!) .font(.title3) .frame(maxWidth: .infinity, alignment: .center) } @@ -128,7 +128,7 @@ struct CreateWorkoutMainView: View { // if left or right auto add the other side // with a recover in between b/c its // eaiser to delete a recover than add one - if exercise.side.count > 0 { + if exercise.side != nil && exercise.side!.count > 0 { let exercises = DataStore.shared.allExercise?.filter({ $0.name == exercise.name }) diff --git a/Werkout_ios/subview/AllEquipmentView.swift b/Werkout_ios/subview/AllEquipmentView.swift new file mode 100644 index 0000000..09371e6 --- /dev/null +++ b/Werkout_ios/subview/AllEquipmentView.swift @@ -0,0 +1,56 @@ +// +// AllEquipmentview.swift +// Werkout_ios +// +// Created by Trey Tartt on 6/16/24. +// + +import SwiftUI + +struct AllEquipmentView: View { + @Binding var selectedEquipment: [Equipment] + @State var createWorkoutItemPickerViewModel: CreateWorkoutItemPickerViewModel? + + var body: some View { + VStack { + if let _ = DataStore.shared.allEquipment { + Text("Select Equipment") + .foregroundColor(.cyan) + Text("\(selectedEquipment.count) Selected") + } + } + .onTapGesture { + if let equipment = DataStore.shared.allEquipment { + var createWorkoutItemPickerModels = [CreateWorkoutItemPickerModel]() + equipment.forEach({ + let model = CreateWorkoutItemPickerModel(id: $0.id, + name: $0.name.lowercased()) + createWorkoutItemPickerModels.append(model) + }) + createWorkoutItemPickerModels = createWorkoutItemPickerModels.sorted(by: { + $0.name < $1.name + }) + let selectedIds = selectedEquipment.map { $0.id } + createWorkoutItemPickerViewModel = CreateWorkoutItemPickerViewModel(allValues: createWorkoutItemPickerModels, selectedIds: selectedIds) + } + } + .sheet(item: $createWorkoutItemPickerViewModel) { item in + CreateWorkoutItemPickerView(viewModel: item, completed: { selectedids in + if let equipment = DataStore.shared.allEquipment { + selectedEquipment.removeAll() + for id in selectedids { + if let equipment = equipment.first(where: { + $0.id == id + }) { + selectedEquipment.append(equipment) + } + } + } + }) + } + } +} + +//#Preview { +// AllEquipmentview() +//} diff --git a/Werkout_ios/subview/AllExerciseView.swift b/Werkout_ios/subview/AllExerciseView.swift new file mode 100644 index 0000000..d2ed9e2 --- /dev/null +++ b/Werkout_ios/subview/AllExerciseView.swift @@ -0,0 +1,92 @@ +// +// AllExerciseView.swift +// Werkout_ios +// +// Created by Trey Tartt on 6/16/24. +// + +import SwiftUI +import AVKit + +struct AllExerciseView: View { + @Environment(\.dismiss) var dismiss + @State var searchString: String = "" + @Binding var filteredExercises: [Exercise] + var selectedExercise: ((Exercise) -> Void) + + @State var avPlayer = AVPlayer(url: URL(string: "https://dev.werkout.fitness/media/exercise_videos/2_Dumbbell_Lateral_Lunges.mp4")!) + @State var videoExercise: Exercise? { + didSet { + if let viddd = self.videoExercise?.videoURL, + let url = URL(string: BaseURLs.currentBaseURL + viddd) { + self.avPlayer = AVPlayer(url: url) + } + } + } + + var body: some View { + VStack { + TextField("Filter", text: $searchString) + .padding() + + List() { + ForEach(filteredExercises, id: \.self) { exercise in + if searchString.isEmpty || (exercise.name.lowercased().contains(searchString.lowercased()) || (exercise.muscleGroups ?? "").lowercased().contains(searchString.lowercased())) { + HStack { + VStack { + Text(exercise.name) + .frame(maxWidth: .infinity, alignment: .leading) + + if exercise.side != nil && !exercise.side!.isEmpty { + Text(exercise.side!) + .font(.footnote) + .frame(maxWidth: .infinity, alignment: .leading) + } + + if exercise.equipmentRequired != nil && !exercise.equipmentRequired!.isEmpty { + Text(exercise.spacedEquipmentRequired) + .font(.footnote) + .frame(maxWidth: .infinity, alignment: .leading) + } + + if exercise.muscleGroups != nil && !exercise.muscleGroups!.isEmpty { + Text(exercise.spacedMuscleGroups) + .font(.footnote) + .frame(maxWidth: .infinity, alignment: .leading) + } + } + .contentShape(Rectangle()) + .onTapGesture { + selectedExercise(exercise) + dismiss() + } + Button(action: { + videoExercise = exercise + }) { + ZStack { + Circle() + .fill(.blue) + .frame(width: 33, height: 33) + Image(systemName: "video.fill") + .frame(width: 33, height: 33) + .foregroundColor(.white ) + } + } + .frame(width: 33, height: 33) + } + } + } + } + } + .sheet(item: $videoExercise) { exercise in + PlayerView(player: $avPlayer) + .onAppear{ + avPlayer.play() + } + } + } +} +// +//#Preview { +// AllExerciseView() +//} diff --git a/Werkout_ios/subview/AllMusclesView.swift b/Werkout_ios/subview/AllMusclesView.swift new file mode 100644 index 0000000..bc23185 --- /dev/null +++ b/Werkout_ios/subview/AllMusclesView.swift @@ -0,0 +1,66 @@ +// +// AllMusclesView.swift +// Werkout_ios +// +// Created by Trey Tartt on 6/16/24. +// + +import SwiftUI + +struct AllMusclesView: View { + @Binding var selectedMuscles: [Muscle] + @State var createWorkoutItemPickerViewModel: CreateWorkoutItemPickerViewModel? + + var body: some View { + VStack { + if let _ = DataStore.shared.allMuscles { + Text("Select Muscles") + .foregroundColor(.cyan) + Text("\(selectedMuscles.count) Selected") + } + } + .onTapGesture { + if let muscles = DataStore.shared.allMuscles { + var createWorkoutItemPickerModels = [CreateWorkoutItemPickerModel]() + muscles.forEach({ + let model = CreateWorkoutItemPickerModel(id: $0.id, name: $0.name.lowercased()) + createWorkoutItemPickerModels.append(model) + }) + createWorkoutItemPickerModels = createWorkoutItemPickerModels.sorted(by: { + $0.name < $1.name + }) + let selectedIds = selectedMuscles.map { $0.id } + createWorkoutItemPickerViewModel = CreateWorkoutItemPickerViewModel(allValues: createWorkoutItemPickerModels, selectedIds: selectedIds) + } + } + .onAppear{ + if #function.hasPrefix("__preview") { + DataStore.shared.setupFakeData() + } + guard let _ = DataStore.shared.allExercise, + let muscles = DataStore.shared.allMuscles, + let _ = DataStore.shared.allEquipment else { + return + } + selectedMuscles = muscles + } + .sheet(item: $createWorkoutItemPickerViewModel) { item in + CreateWorkoutItemPickerView(viewModel: item, completed: { selectedids in + if let muscles = DataStore.shared.allMuscles { + selectedMuscles.removeAll() + for id in selectedids { + if let muscle = muscles.first(where: { + $0.id == id + }) { + selectedMuscles.append(muscle) + } + } + } + }) + } + } +} + +//#Preview { +// AllMusclesView() +//}