init commit - bring project over from Mood

This commit is contained in:
Trey t
2022-01-10 09:04:38 -06:00
parent ff5d9bea18
commit 58697bf965
30 changed files with 1840 additions and 121 deletions

View File

@@ -0,0 +1,79 @@
//
// 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)
}
}
}