Guest User

Untitled

a guest
Nov 1st, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.92 KB | Source Code | 0 0
  1. import CoreML
  2. import SwiftUI
  3.  
  4.  
  5. struct ContentView: View {
  6.     @State private var wakeUp = defaultWakeTime
  7.     @State private var sleepAmount = 8.0
  8.     @State private var coffeAmount = 1
  9.    
  10.     @State private var alertTitle = ""
  11.     @State private var alertMessage = ""
  12.     @State private var showingAlert = false
  13.    
  14.     static var defaultWakeTime: Date {
  15.         var components = DateComponents()
  16.         components.hour = 7
  17.         components.minute = 0
  18.         return Calendar.current.date(from: components) ?? Date.now
  19.     }
  20.    
  21.     var body: some View {
  22.        
  23.         NavigationView {
  24.             Form {
  25.                 VStack(alignment: .leading, spacing: 0){
  26.                     Text("When do you want to wake up?")
  27.                         .font(.headline)
  28.                     DatePicker("Please enter a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
  29.                         .labelsHidden()
  30.                 }
  31.                 VStack(alignment: .leading, spacing: 0){
  32.                     Text("Desired amount of sleep")
  33.                         .font(.headline)
  34.                     Stepper("\(sleepAmount.formatted()) hours", value: $sleepAmount, in: 4...12, step: 0.25)
  35.                 }
  36.                
  37.                 VStack(alignment: .leading, spacing: 0){
  38.                     Text("Daily coffe intake")
  39.                         .font(.headline)
  40.                     Stepper(coffeAmount == 1 ? "1 cup" : "\(coffeAmount) cups", value: $coffeAmount, in: 1...20)
  41.                 }
  42.             }
  43.             .navigationTitle("Better Rest")
  44.             .toolbar {
  45.                 Button("Calculate", action: calculateBedTime)
  46.             }
  47.             .alert(alertTitle, isPresented: $showingAlert) {
  48.                 Button("OK") { }
  49.             } message: {
  50.                 Text(alertMessage)
  51.             }
  52.         }
  53.     }
  54.    
  55.     func calculateBedTime() {
  56.         do {
  57.             let config = MLModelConfiguration()
  58.             let model = try SleepCalculator(configuration: config)
  59.            
  60.             let components = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)
  61.             let hour = (components.hour ?? 0) * 60 * 60
  62.             let minute = (components.minute ?? 0) * 60
  63.            
  64.             let prediction = try model.prediction(wake: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeAmount))
  65.            
  66.             let sleepTime = wakeUp - prediction.actualSleep
  67.             alertTitle = "Your ideal bed time is..."
  68.             alertMessage = sleepTime.formatted(date: .omitted, time: .shortened)
  69.            
  70.            
  71.         } catch {
  72.             alertTitle = "Error"
  73.             alertMessage = "Sorry, there was a problem calculationg your bedtime."
  74.         }
  75.             showingAlert = true
  76.     }
  77. }
  78.  
  79. struct ContentView_Previews: PreviewProvider {
  80.     static var previews: some View {
  81.         ContentView()
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment