80 lines
2.8 KiB
Swift
80 lines
2.8 KiB
Swift
//
|
|
// AddMoodHeaderView.swift
|
|
// Feels
|
|
//
|
|
// Created by Trey Tartt on 1/5/22.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
import CoreData
|
|
|
|
struct AddMoodHeaderView: View {
|
|
@Environment(\.managedObjectContext) private var viewContext
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
Color(UIColor.secondarySystemBackground)
|
|
|
|
VStack {
|
|
Text("How are you feeling today?")
|
|
.font(.title)
|
|
.foregroundColor(Color(UIColor.label))
|
|
.padding()
|
|
HStack{
|
|
ForEach(Mood.allValues) { mood in
|
|
VStack {
|
|
Button(action: {
|
|
addItem(withMoodValue: mood.rawValue)
|
|
}, label: {
|
|
mood.icon
|
|
.font(.system(size: 50))
|
|
})
|
|
|
|
//Text(mood.strValue)
|
|
}.frame(minWidth: 0, maxWidth: .infinity)
|
|
}
|
|
}
|
|
.padding([.leading, .trailing, .bottom])
|
|
}
|
|
}
|
|
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
|
|
.frame(minHeight: 85, maxHeight: 140)
|
|
.frame(minWidth: 0, maxWidth: .infinity)
|
|
.padding()
|
|
}
|
|
|
|
private func addItem(withMoodValue moodValue: Int) {
|
|
withAnimation {
|
|
let newItem = MoodEntry(context: viewContext)
|
|
newItem.timestamp = Date()
|
|
newItem.moodValue = Int16(moodValue)
|
|
newItem.date = Date()
|
|
|
|
do {
|
|
try viewContext.save()
|
|
} catch {
|
|
// Replace this implementation with code to handle the error appropriately.
|
|
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
|
let nsError = error as NSError
|
|
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AddMoodHeaderView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
Group {
|
|
AddMoodHeaderView().environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
|
|
|
AddMoodHeaderView().preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
|
|
|
AddMoodHeaderView().environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
|
|
|
AddMoodHeaderView().preferredColorScheme(.dark).environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
|
|
}
|
|
}
|
|
}
|