/BetterRest

SwiftUI - Project 4

Primary LanguageSwift

BetterRest

A SwiftUI app leveraging a CreateML model to estimate bedtime based on coffee consumed during the day & wake up time.

gif.gif

Project 4 - 100DaysOfSwiftUI - Days 26-28

Day 26

  • Creating a ML model using training data & Create ML

Day 27

  • The name of the .MLmodel file is used for the class name of the model
  • Using the ML model is as simple as calling prediction() with the same type of data for the parameters as used in the training data itself
let model = SleepCalculator()
let prediction = try model.prediction(wake: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeeAmount))
  • And the output name will be a property with the same name as used in the model, and of the same type
  • To generate a Date by supplying only the time, DateComponents() can be used
static var defaultWakeTime: Date {
        var components = DateComponents()
        components.hour = 7
        components.minute = 0
        return Calendar.current.date(from: components) ?? Date()
}
  • And to extract a particular component from a Date variable (wakeUp below):
let components = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)
let hour = (components.hour ?? 0) * 60 * 60
let minute = (components.minute ?? 0) * 60

Day 28 - Challenges

  1. Replace each VStack in our form with a Section, where the text view is the title of the section. Do you prefer this layout or the VStack layout? It’s your app – you choose!
  2. Replace the “Number of cups” stepper with a Picker showing the same range of values.
  3. 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.
  • For challenge #3 I used a computed property instead:
var bedtime: 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
        
        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
            
            return formatter.string(from: sleepTime)
        } catch {
            return "Sorry there was a problem calculating your bedtime"
        }
}

Day 76 - Accessibility Challenge

Fix the steppers in BetterRest so that they read out useful information when the user adjusts their values.