Day 26, 27, 28of #100DaysOfSwiftUI

Start 100DaysOfSwiftUI from 2020.Mar.18th.

Day 26, 27, 28: Project 4, part one, two three
done with 72min.

New findings: followings are new findings

  • just wapping DatePicker with Form made nice UI ! super cool !
  • CoreML is really interesting. actually I have never had any interest in ML, but I might start to study it from

Here is the code after challenges

//
//  ContentView.swift
//  BetterRest
//
//  Created by Tomoaki Yagishita on 2020/03/31.
//  Copyright © 2020 SmallDeskSoftware. All rights reserved.
//

import SwiftUI

struct ContentView: View {

    @State private var wakeUp = defaultWakeTime
    @State private var sleepAmount = 8.0
    @State private var coffeeAmount = 0
    
    @State private var alertTitle = ""
    @State private var alertMessage = ""
    @State private var showingAlert = false
    
    static var defaultWakeTime: Date {
        var components = DateComponents()
        components.hour = 7
        components.minute = 0
        return Calendar.current.date(from: components) ?? Date()
    }
    
    @State private var bettime = ""
    
    var body: some View {
        NavigationView {
            Form {
                Section {
                    BedTimeView(wakeUp: $wakeUp, sleepAmount: $sleepAmount,
                                coffeeAmount: $coffeeAmount)
                }
                Text("When do you want to wake up?")
                    .font(.headline)
                DatePicker("Please enter a time", selection: $wakeUp,
                           displayedComponents: .hourAndMinute)
                .labelsHidden()
//                    .datePickerStyle(WheelDatePickerStyle())
                
                    //                VStack(alignment: .leading, spacing: 0) {
                Section {
                    Text("Desired amount of sleep")
                        .font(.headline)
                    Stepper(value: $sleepAmount, in: 4...12, step: 0.25) {
                        Text("\(sleepAmount, specifier: "%g") hours")
                    }
                }
                
                //VStack(alignment: .leading, spacing: 0) {
                Section {
                    Text("Daily coffee intake")
                        .font(.headline)
//                    Stepper(value: $coffeeAmount, in: 1...20) {
//                        if coffeeAmount == 1 {
//                            Text("1 cup")
//                        } else {
//                            Text("\(coffeeAmount) cups")
//                        }
//                    }
                    Picker(selection: $coffeeAmount, label: Text("Daily coffee intake")) {
                        ForEach(1 ..< 21) { num in
                            if num == 1 {
                                Text("1 cup")
                            } else {
                                Text("\(num) cups")
                            }
                        }
                    }
                }
                
            }
            .navigationBarTitle("BetterRest")
//            .navigationBarItems(trailing: Button(action: calculateBedtime) {
//                Text("Calculate")
//                })
                .alert(isPresented: $showingAlert) {
                    Alert(title: Text(alertTitle),
                          message: Text(alertMessage),
                          dismissButton: .default(Text("OK")))
            }
        }
    }
    
    func calculateBedtime() {
        let model = SleepCalculator()

        let component = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)
        let hour = (component.hour ?? 0) * 60 * 60 // hour -> sec
        let minute = (component.minute ?? 0) * 60 // min -> sec
        
        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
            alertTitle = "Your ideal bedtime is..."
            alertMessage = formatter.string(from: sleepTime)
        }  catch {
            alertTitle = "Error"
            alertMessage = "Sorry, there was a problem calculating your bedtime."
        }
        showingAlert = true
    }
}

struct BedTimeView: View {
    @Binding var wakeUp:Date
    @Binding var sleepAmount:Double
    @Binding var coffeeAmount:Int
    
    var body: some View {
        HStack {
            Text("Your ideal bedtime is ")
            Text(calculatedTime())
        }
    }
    
    func calculatedTime() -> String{
        var message = ""
        let model = SleepCalculator()

        let component = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)
        let hour = (component.hour ?? 0) * 60 * 60 // hour -> sec
        let minute = (component.minute ?? 0) * 60 // min -> sec
        
        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
            message = formatter.string(from: sleepTime)
        }  catch {
            message = "Sorry, there was a problem calculating your bedtime."
        }
        return message
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です