Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sw 146 good form #107

Merged
merged 16 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions SmartWeights/SmartWeights.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
6BEA78612BBDC82900239AB0 /* UserFeedbackDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BEA78602BBDC82900239AB0 /* UserFeedbackDataManager.swift */; };
6BEA78632BBDC88200239AB0 /* UserFitnessDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BEA78622BBDC88200239AB0 /* UserFitnessDataManager.swift */; };
6BEA78652BBDC94300239AB0 /* UserFitnessPlanManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BEA78642BBDC94300239AB0 /* UserFitnessPlanManager.swift */; };
6BEA786B2BC0470C00239AB0 /* PetPageViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BEA786A2BC0470C00239AB0 /* PetPageViewModel.swift */; };
6BEA786F2BC08E4700239AB0 /* WorkoutPageViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BEA786E2BC08E4700239AB0 /* WorkoutPageViewModel.swift */; };
91065F7D2B7A7E4700BC62E6 /* SmartWeightsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91065F7C2B7A7E4700BC62E6 /* SmartWeightsApp.swift */; };
91065F7F2B7A7E4700BC62E6 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91065F7E2B7A7E4700BC62E6 /* ContentView.swift */; };
91065F812B7A7E4900BC62E6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 91065F802B7A7E4900BC62E6 /* Assets.xcassets */; };
Expand All @@ -47,6 +49,7 @@
912241712B90EA9F00D03638 /* WorkoutUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 912241702B90EA9F00D03638 /* WorkoutUITests.swift */; };
918037962BA9026700802D01 /* central.swift in Sources */ = {isa = PBXBuildFile; fileRef = 918037952BA9026700802D01 /* central.swift */; };
918037982BB4647A00802D01 /* central_v2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 918037972BB4647A00802D01 /* central_v2.swift */; };
9180379A2BBF719B00802D01 /* FormCriteria.swift in Sources */ = {isa = PBXBuildFile; fileRef = 918037992BBF719B00802D01 /* FormCriteria.swift */; };
B90B6FDA2B7FAD1C009866FD /* PetStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = B90B6FD92B7FAD1C009866FD /* PetStore.swift */; };
B9A4764C2B8F86DA0062CBB6 /* SelectFirstPet.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9A4764B2B8F86DA0062CBB6 /* SelectFirstPet.swift */; };
E56BD7D82BAFA2C300863D4D /* TabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = E56BD7D72BAFA2C300863D4D /* TabBar.swift */; };
Expand Down Expand Up @@ -97,6 +100,8 @@
6BEA78602BBDC82900239AB0 /* UserFeedbackDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserFeedbackDataManager.swift; sourceTree = "<group>"; };
6BEA78622BBDC88200239AB0 /* UserFitnessDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserFitnessDataManager.swift; sourceTree = "<group>"; };
6BEA78642BBDC94300239AB0 /* UserFitnessPlanManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserFitnessPlanManager.swift; sourceTree = "<group>"; };
6BEA786A2BC0470C00239AB0 /* PetPageViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PetPageViewModel.swift; sourceTree = "<group>"; };
6BEA786E2BC08E4700239AB0 /* WorkoutPageViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkoutPageViewModel.swift; sourceTree = "<group>"; };
91065F792B7A7E4700BC62E6 /* SmartWeights.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SmartWeights.app; sourceTree = BUILT_PRODUCTS_DIR; };
91065F7C2B7A7E4700BC62E6 /* SmartWeightsApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmartWeightsApp.swift; sourceTree = "<group>"; };
91065F7E2B7A7E4700BC62E6 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand All @@ -117,6 +122,7 @@
912241702B90EA9F00D03638 /* WorkoutUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkoutUITests.swift; sourceTree = "<group>"; };
918037952BA9026700802D01 /* central.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = central.swift; sourceTree = "<group>"; };
918037972BB4647A00802D01 /* central_v2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = central_v2.swift; sourceTree = "<group>"; };
918037992BBF719B00802D01 /* FormCriteria.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormCriteria.swift; sourceTree = "<group>"; };
B90B6FD92B7FAD1C009866FD /* PetStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PetStore.swift; sourceTree = "<group>"; };
B9A4764B2B8F86DA0062CBB6 /* SelectFirstPet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectFirstPet.swift; sourceTree = "<group>"; };
E56BD7D72BAFA2C300863D4D /* TabBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBar.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,6 +177,7 @@
children = (
3202761D2B892D2C001A86B8 /* CustomProgressView.swift */,
320276212B892D55001A86B8 /* Pet_page.swift */,
6BEA786A2BC0470C00239AB0 /* PetPageViewModel.swift */,
327C4B012BA9C38800E160EE /* Inventory_Customize_page.swift */,
);
path = "pet-customize-page";
Expand Down Expand Up @@ -287,6 +294,8 @@
911899392B8CE1300071232A /* StartWorkout.swift */,
9118993B2B8CE1400071232A /* WorkoutFeedback.swift */,
9118993D2B8CE1570071232A /* WorkoutMainPage.swift */,
918037992BBF719B00802D01 /* FormCriteria.swift */,
6BEA786E2BC08E4700239AB0 /* WorkoutPageViewModel.swift */,
);
path = Workout;
sourceTree = "<group>";
Expand Down Expand Up @@ -470,6 +479,7 @@
B9A4764C2B8F86DA0062CBB6 /* SelectFirstPet.swift in Sources */,
6BEA78632BBDC88200239AB0 /* UserFitnessDataManager.swift in Sources */,
6BC8E5722BB097CA00A9CB8C /* VoiceRecognitionViewModel.swift in Sources */,
6BEA786B2BC0470C00239AB0 /* PetPageViewModel.swift in Sources */,
B90B6FDA2B7FAD1C009866FD /* PetStore.swift in Sources */,
01A6B0BE2B88F16500D84920 /* MorePageView.swift in Sources */,
91065F862B7A7E4900BC62E6 /* Persistence.swift in Sources */,
Expand All @@ -491,9 +501,12 @@
6BEA78552BBCE5E300239AB0 /* Inventory.swift in Sources */,
91065F892B7A7E4900BC62E6 /* SmartWeights.xcdatamodeld in Sources */,
9118993E2B8CE1570071232A /* WorkoutMainPage.swift in Sources */,
9180379A2BBF719B00802D01 /* FormCriteria.swift in Sources */,
6B98A7242BAA223F00856271 /* TestDB.swift in Sources */,
01B8E3DA2B7FAE5C004375ED /* ChallengesTab.swift in Sources */,
320276222B892D55001A86B8 /* Pet_page.swift in Sources */,
327C4B022BA9C38800E160EE /* Inventory_Customize_page.swift in Sources */,
6BEA786F2BC08E4700239AB0 /* WorkoutPageViewModel.swift in Sources */,
9118993A2B8CE1300071232A /* StartWorkout.swift in Sources */,
6BEA785F2BBDC6A700239AB0 /* UserAchievementManager.swift in Sources */,
6BEA78652BBDC94300239AB0 /* UserFitnessPlanManager.swift in Sources */,
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,21 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "F3434B03-D0CB-43A9-86C7-6AEC0275C3D1"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SmartWeights/Workout/WorkoutMainPage.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "210"
endingLineNumber = "210"
landmarkName = "StartWorkoutView"
landmarkType = "24">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
37 changes: 35 additions & 2 deletions SmartWeights/SmartWeights/DBAccess/PetManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,42 @@ class PetDBManager: ObservableObject {
let petImage = record[PetRecordKeys.petImage.rawValue] as? CKAsset
let totalXP = record[PetRecordKeys.totalXP.rawValue] as? Int64 ?? 0

let pet = PetModel(recordId: record.recordID, health: health, level: level, petImage: petImage, totalXP: totalXP)
completion(pet, nil)
self.pet = PetModel(recordId: record.recordID, health: health, level: level, petImage: petImage, totalXP: totalXP)
completion(self.pet, nil)
self.petExists = true
}
}
func getXP(completion: @escaping (Int64?, Error?) -> Void){
if let pet = pet {
completion(pet.totalXP, nil)
} else {
fetchPet { pet, error in
if let error = error {
completion(nil, error)
return
}
guard let pet = pet else {
completion(nil, nil)
return
}
completion(self.pet?.totalXP, nil)
}
}
}
func updateUserXP(newXP: Int64, completion: @escaping (Error?) -> Void) {
CKManager.fetchPrivateRecord(recordType: "Pet") { records, error in
guard let record = records?.first else {
print("Error fetching user: \(error?.localizedDescription ?? "Unknown error")")
completion(error)
return
}

let currentXP = NSNumber(value: newXP)
record[PetRecordKeys.totalXP.rawValue] = currentXP as CKRecordValue
self.CKManager.savePrivateItem(record: record)
completion(nil)
}
}


}
4 changes: 2 additions & 2 deletions SmartWeights/SmartWeights/SmartWeightsApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ struct SmartWeightsApp: App {
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
LoginView()
WorkoutMainPage()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
// PetStore()

}
}
}
143 changes: 143 additions & 0 deletions SmartWeights/SmartWeights/Workout/FormCriteria.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//
// FormCriteria.swift
// SmartWeights
//
// Created by Tu Ha on 4/4/24.
//

import SwiftUI


class FormCriteria: ObservableObject{

/*
read from the arrays that store the data

calculate the form for one set (speed)

*/

//read the Zaxis rotation
//make a function that reads from array
func averageUpDownAcceleration(array: [[Int]]) -> Double{

/*
good acceleration for going up and down -180°/s, 180°/s
bad - less than -180 or greater than 180
*/
var count = 0
var good = 0
var fast = 0

array.forEach { (data) in
if data[2] < 10 && data[2] > -10{ //the user isnt making a rep, could be between reps

}
else if data[2] > -180 && data[2] < 180{
good += 1
}
else if data[2] < 0{
fast += 1
}
if data[2] > 10 || data[2] < -10{
count += 1
}

}

return Double(good)/Double(count)

}
//tell the user if they going going up too fast or going down too fast
func UpDownAcceleration(array: [[Int]]) -> (Double,Double){

var tooFastUp = 0
var tooFastDown = 0

var upCount = 0
var downCount = 0

array.forEach{ (data) in

if data[2] < 10 && data[2] > -10{ //the user isnt making a rep, could be between reps (resting)
}

else if data[2] > 180{ //moving up faster than 180 degrees per second
tooFastUp += 1
}
else if data[2] < -180{ //moving down faster than 180 degrees per second
tooFastDown += 1
}

if data[2] > 10{ //only counting when they are moving up
upCount += 1
}
else if data[2] < -10{ //only counting when they are moving down
downCount += 1
}

}

return(Double(tooFastUp)/Double(upCount), Double(tooFastDown)/Double(downCount))

}

func giveFeedback(array: [[Int]]) -> (String,String,String,String){

let averageAcceleration = self.averageUpDownAcceleration(array: array)
let upDownAcceleration = self.UpDownAcceleration(array: array)

let overallAccel = String(format: "Overall acceleration: %.f%% good", averageAcceleration * 100)
let upSpeed = String(format: "%.0f%% of your reps are too fast going up", upDownAcceleration.0 * 100)
let downSpeed = String(format: "%.0f%% of your reps are too fast going down", upDownAcceleration.1 * 100)
var customTextFeedback = ""

if self.averageUpDownAcceleration(array: array) < 0.5{
customTextFeedback = "whoa slow down!"

}
else{
customTextFeedback = "Keep up the good work"
}



return (overallAccel,upSpeed,downSpeed,customTextFeedback)

}

}


struct FormCriteriaView:View {
@ObservedObject var ble = BLEcentral()
@ObservedObject var form = FormCriteria()

@State var z = 0.0
@State var up = 0.0
@State var down = 0.0

var body: some View {
bleView(ble: ble)


Button(action: {
z = form.averageUpDownAcceleration(array: ble.MPU6050_1Gyros)
up = form.UpDownAcceleration(array: ble.MPU6050_1Gyros).0
down = form.UpDownAcceleration(array: ble.MPU6050_1Gyros).1
}){
Text("get feedback")
}
Text("Overall acceleration going up and down is \(z*100)%")
Text("\(up * 100)% of your reps are too fast going up")
Text("\(down * 100)% of your reps are too fast going down")




}
}

#Preview{
FormCriteriaView()
}
2 changes: 1 addition & 1 deletion SmartWeights/SmartWeights/Workout/StartWorkout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,5 @@ class WorkoutViewModel: ObservableObject {


#Preview{
WorkoutMainPage(viewModel: WorkoutViewModel(), bleManager: BLEManager())
WorkoutMainPage(viewModel: WorkoutViewModel())
}
Loading