Hi @wctjerry,
it was a long time that I did this day so I had too look back what the challenge was 😛.
If I remember correctly the original project shown by Paul uses a button and when you press it, it will show the recommended bed time right?
Well if you want to automate this you just have to create a computed propterty with the name calculatedBedtime and the type String. A Computed property is always updated so you can just show that property inside a text block:
Section(header: Text("Recommended Bedtime")) {
Text("\(calculatedBedtime)")
.font(.largeTitle)
}
The computed property I used is shown below but you might have to adjust it to your needs.
var calculatedBedtime: String {
let model = SleepCalculator()
let components = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)
let hour = (components.hour ?? 0) * 60 * 60
let minute = (components.minute ?? 0) * 60
var bedTime = "Please set all input values"
do {
let prediction = try model.prediction(wake: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeeAmount))
let sleepTime = wakeUp - prediction.actualSleep
let formatter = DateFormatter()
formatter.timeStyle = .short
bedTime = formatter.string(from: sleepTime)
} catch {
alertTitle = "Error"
alertMessage = "Sorry, there was a problem calculating your bedtime."
showingAlert = true
}
return bedTime
}
I hope this helps you further?