@@ -18,55 +18,58 @@ struct ContentView: View {
18
18
@State private var showingAlert = false
19
19
20
20
var body : some View {
21
- NavigationView {
21
+ NavigationView {
22
22
Form {
23
23
24
24
Section ( header: Text ( " When do you want to wake up? " ) ) {
25
- /*
26
- VStack(alignment: .leading, spacing: 0) {
27
- Text("When do you want to wake up?")
28
- .font(.headline)
29
- */
25
+
30
26
DatePicker ( " Please enter a time " , selection: $wakeUp, displayedComponents: . hourAndMinute)
31
27
. labelsHidden ( )
32
28
. datePickerStyle ( WheelDatePickerStyle ( ) )
33
29
}
34
30
35
31
Section ( header: Text ( " Desired amount of sleep " ) ) {
36
- /*
37
- VStack(alignment: .leading, spacing: 0) {
38
- Text("Desired amount of sleep")
39
- .font(.headline)
40
- */
32
+
41
33
Stepper ( value: $sleepAmount, in: 4 ... 12 , step: 0.25 ) {
42
34
Text ( " \( sleepAmount, specifier: " %g " ) hours " )
43
35
}
44
36
}
45
37
46
38
Section ( header: Text ( " Daily coffe intake " ) ) {
47
- /*
48
- VStack(alignment: .leading, spacing: 0) {
49
- Text("Daily coffe intake")
50
- .font(.headline)
51
- */
39
+
52
40
Stepper ( value: $coffeeAmount, in: 1 ... 20 ) {
53
41
if coffeeAmount == 1 {
54
42
Text ( " 1 cup " )
55
43
} else {
56
44
Text ( " \( coffeeAmount) cups " )
57
45
}
58
46
}
47
+
48
+ // Commented code represents the solution to challenge 2, but a Stepper was a better design
49
+
50
+ /*
51
+ Picker("Daily coffee intake", selection: $coffeeAmount) {
52
+ ForEach(1 ... 20, id: \.self) {
53
+ if $0 == 1 {
54
+ Text("\($0) cup")
55
+ } else {
56
+ Text("\($0) cups")
57
+ }
58
+ }
59
+ }
60
+ .pickerStyle(DefaultPickerStyle())
61
+ */
59
62
}
60
- }
61
- . navigationBarTitle ( " BetterRest " )
62
- . navigationBarItems ( trailing:
63
- Button ( action: calculateBedtime) {
64
- Text ( " Calculate " )
63
+
64
+ Section ( header: Text ( " Your ideal bedtime is " ) ) {
65
+ Text ( " \( calculateBedtime ( ) ) " )
66
+ . foregroundColor ( . yellow)
67
+ . font ( . largeTitle)
68
+ . fontWeight ( /*@START_MENU_TOKEN@*/. bold/*@END_MENU_TOKEN@*/)
65
69
}
66
- )
67
- . alert ( isPresented: $showingAlert) {
68
- Alert ( title: Text ( alertTitle) , message: Text ( alertMessage) , dismissButton: . default( Text ( " OK " ) ) )
69
70
}
71
+ . preferredColorScheme ( /*@START_MENU_TOKEN@*/. dark/*@END_MENU_TOKEN@*/)
72
+ . navigationBarTitle ( Text ( " BetterRest " ) )
70
73
}
71
74
}
72
75
@@ -77,26 +80,25 @@ struct ContentView: View {
77
80
return Calendar . current. date ( from: components) ?? Date ( )
78
81
}
79
82
80
- func calculateBedtime( ) {
83
+ func calculateBedtime( ) -> String {
81
84
let model = SleepCalculator ( )
82
85
let components = Calendar . current. dateComponents ( [ . hour, . minute] , from: wakeUp)
83
86
let hour = ( components. hour ?? 0 ) * 60 * 60
84
87
let minute = ( components. minute ?? 0 ) * 60
85
-
88
+ var bedtimePrediction : String = " "
89
+
86
90
do {
87
91
let prediction = try model. prediction ( wake: Double ( hour + minute) , estimatedSleep: sleepAmount, coffee: Double ( coffeeAmount) )
88
92
let sleepTime = wakeUp - prediction. actualSleep
89
93
90
94
let formatter = DateFormatter ( )
91
95
formatter. timeStyle = . short
92
96
93
- alertMessage = formatter. string ( from: sleepTime)
94
- alertTitle = " Your ideal bedtime is... "
97
+ bedtimePrediction = formatter. string ( from: sleepTime)
95
98
} catch {
96
- alertTitle = " Error "
97
- alertMessage = " Sorry, there was a problem calculating your bedtime. "
99
+ bedtimePrediction = " Oops! I encountered an error 🤭 "
98
100
}
99
- showingAlert = true
101
+ return bedtimePrediction
100
102
}
101
103
}
102
104
0 commit comments