Skip to content

Commit dba228d

Browse files
committed
Completed challenges for BetterRest app, updated related README.md
1 parent 39ee758 commit dba228d

File tree

7 files changed

+43
-39
lines changed

7 files changed

+43
-39
lines changed

Assets/Mockup_BetterRest.png

130 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
uuid = "6D270DBC-53A7-474D-AAC0-312FFE3E6DF1"
4+
type = "1"
5+
version = "2.0">
6+
</Bucket>

BetterRest/BetterRest/ContentView.swift

+33-31
Original file line numberDiff line numberDiff line change
@@ -18,55 +18,58 @@ struct ContentView: View {
1818
@State private var showingAlert = false
1919

2020
var body: some View {
21-
NavigationView {
21+
NavigationView {
2222
Form {
2323

2424
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+
3026
DatePicker("Please enter a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
3127
.labelsHidden()
3228
.datePickerStyle(WheelDatePickerStyle())
3329
}
3430

3531
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+
4133
Stepper(value: $sleepAmount, in: 4 ... 12, step: 0.25) {
4234
Text("\(sleepAmount, specifier: "%g") hours")
4335
}
4436
}
4537

4638
Section (header: Text("Daily coffe intake")) {
47-
/*
48-
VStack(alignment: .leading, spacing: 0) {
49-
Text("Daily coffe intake")
50-
.font(.headline)
51-
*/
39+
5240
Stepper(value: $coffeeAmount, in: 1 ... 20) {
5341
if coffeeAmount == 1{
5442
Text("1 cup")
5543
} else {
5644
Text("\(coffeeAmount) cups")
5745
}
5846
}
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+
*/
5962
}
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@*/)
6569
}
66-
)
67-
.alert(isPresented: $showingAlert) {
68-
Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("OK")))
6970
}
71+
.preferredColorScheme(/*@START_MENU_TOKEN@*/.dark/*@END_MENU_TOKEN@*/)
72+
.navigationBarTitle(Text("BetterRest"))
7073
}
7174
}
7275

@@ -77,26 +80,25 @@ struct ContentView: View {
7780
return Calendar.current.date(from: components) ?? Date()
7881
}
7982

80-
func calculateBedtime() {
83+
func calculateBedtime() -> String {
8184
let model = SleepCalculator()
8285
let components = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)
8386
let hour = (components.hour ?? 0) * 60 * 60
8487
let minute = (components.minute ?? 0) * 60
85-
88+
var bedtimePrediction: String = ""
89+
8690
do {
8791
let prediction = try model.prediction(wake: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeeAmount))
8892
let sleepTime = wakeUp - prediction.actualSleep
8993

9094
let formatter = DateFormatter()
9195
formatter.timeStyle = .short
9296

93-
alertMessage = formatter.string(from: sleepTime)
94-
alertTitle = "Your ideal bedtime is..."
97+
bedtimePrediction = formatter.string(from: sleepTime)
9598
} catch {
96-
alertTitle = "Error"
97-
alertMessage = "Sorry, there was a problem calculating your bedtime."
99+
bedtimePrediction = "Oops! I encountered an error 🤭"
98100
}
99-
showingAlert = true
101+
return bedtimePrediction
100102
}
101103
}
102104

BetterRest/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Create ML, Core ML, DatePicker, Stepper, navigationBarItems(), Date, DateCompone
1010
> * Replace the “Number of cups” stepper with a Picker showing the same range of values.
1111
> * Change the user interface so that it always shows their recommended bedtime using a nice and large font. You should be able to remove the “Calculate” button entirely.
1212
13+
Second challenge was completed and is contained as a comment inside the project. A stepper implementation seemed like a better design in this case.
14+
1315
## 📸 Screenshots
14-
<!---
1516
<div align ="center">
1617
<img src="/Assets/Mockup_BetterRest.png" width=800>
17-
</div>
18-
--->
18+
</div>

RockPaperScissors/RockPaperScissors/ContentView.swift

+1-5
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,13 @@ struct ContentView: View {
4545
}
4646
Spacer()
4747
.frame(height: 33)
48-
HStack{
48+
HStack (spacing: 38) {
4949
ForEach(0 ..< 3) { number in
5050
Button(action: {
5151
self.buttonTapped(number)
5252
}) {
53-
Spacer()
54-
.frame(width:19)
5553
Text("\(self.choices[number])")
5654
.font(.system(size: 65))
57-
Spacer()
58-
.frame(width:19)
5955
}
6056
}
6157
}

0 commit comments

Comments
 (0)