Skip to content

Commit fd5b589

Browse files
Validation for Weight and Age inputs
1 parent 3f5d3e8 commit fd5b589

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

Sources/SprenUI/UI/Screens/Flows/BodyComp/Components/InputElements/AgeInput.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ struct AgeInput: View {
1919
var strokeColor: Color = .sprenUISecondaryColor
2020

2121
var body: some View {
22-
TextField("Enter your age", value: $age, formatter: numberFormatter)
22+
let binding = Binding<Int?>(get: {
23+
return self.age
24+
}, set: {
25+
if let age = $0 {
26+
self.age = age
27+
}else{
28+
self.age = 0
29+
}
30+
})
31+
32+
TextField("Enter your age", value: binding, formatter: numberFormatter)
2333
.font(.sprenInput)
2434
.keyboardType(.numberPad)
2535
.padding()

Sources/SprenUI/UI/Screens/Flows/BodyComp/Components/InputElements/WeightInput.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,21 @@ struct WeightInput: View {
2020
var strokeColor: Color = .sprenUISecondaryColor
2121

2222
var body: some View {
23+
24+
let binding = Binding<Double?>(get: {
25+
return self.weight
26+
}, set: {
27+
if let weight = $0 {
28+
self.weight = weight
29+
}else{
30+
self.weight = 0
31+
}
32+
})
33+
2334
VStack(alignment: .leading, spacing: 0) {
2435
let textUnit = weightMetric == 0 ? "lbs" : "kg"
2536
HStack {
26-
TextField("Enter your weight", value: $weight, formatter: numberFormatter)
37+
TextField("Enter your weight", value: binding, formatter: numberFormatter)
2738
.font(.sprenInput)
2839
.keyboardType(.decimalPad)
2940
Spacer()

Sources/SprenUI/UI/Screens/Flows/BodyComp/UserInputScreens/AgeScreen/AgeScreen.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,16 @@ struct AgeScreen: View {
4848
}
4949

5050
Spacer()
51-
NavigationLink(destination: GenderScreen()) {
51+
52+
if(age == 0){
5253
PurpleButton(text: "Next")
53-
}.simultaneousGesture(TapGesture().onEnded {
54-
UserData.default.saveAge(self.$age.wrappedValue)
55-
})
54+
}else{
55+
NavigationLink(destination: GenderScreen()) {
56+
PurpleButton(text: "Next")
57+
}.simultaneousGesture(TapGesture().onEnded {
58+
UserData.default.saveAge(self.$age.wrappedValue)
59+
})
60+
}
5661
}
5762
.navigationBarHidden(true)
5863
.padding(.all, Autoscale.convert(15))

Sources/SprenUI/UI/Screens/Flows/BodyComp/UserInputScreens/WeightScreen/WeightScreen.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ struct WeightScreen: View {
4646
.pickerStyle(.segmented)
4747
WeightInput(weight: $weight, weightMetric: weightMetric)
4848
Spacer()
49-
NavigationLink(destination: HeightScreen()) {
49+
50+
if(weight == 0){
5051
PurpleButton(text: "Next")
51-
}.simultaneousGesture(TapGesture().onEnded{
52-
UserData.default.saveWeight(self.$weight.wrappedValue, self.$weightMetric.wrappedValue)
53-
})
52+
}else{
53+
NavigationLink(destination: HeightScreen()) {
54+
PurpleButton(text: "Next")
55+
}.simultaneousGesture(TapGesture().onEnded{
56+
UserData.default.saveWeight(self.$weight.wrappedValue, self.$weightMetric.wrappedValue)
57+
})
58+
}
5459
}
5560
.navigationBarHidden(true)
5661
.padding(.all, Autoscale.convert(15))

0 commit comments

Comments
 (0)