Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions CoffeeTracker/CoffeeTracker/Beans/BeanModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct BeanModel: Identifiable, Hashable {
notes = note
}

func addBeansToData(context: NSManagedObjectContext) {
mutating func addBeansToData(context: NSManagedObjectContext) {
let newBean = Bean(context: context)
newBean.id = id
newBean.name = name
Expand All @@ -27,6 +27,8 @@ struct BeanModel: Identifiable, Hashable {
newBean.boughtOn = boughtOn
newBean.notes = notes

self.objectID = newBean.objectID

do {
try context.save()
print("Bean saved correctly")
Expand All @@ -41,18 +43,26 @@ struct BeanModel: Identifiable, Hashable {
return
}

let fetchRequest: NSFetchRequest<Bean>
fetchRequest = Bean.fetchRequest()

fetchRequest.predicate = NSPredicate(format: "%K == %@", "id", id as CVarArg)

do {
let object: Bean = try context.existingObject(with: objectID) as! Bean
object.name = name
object.style = style
object.buyAgain = buyAgain
object.roaster = roaster
object.roastedOn = roastedOn
object.boughtOn = boughtOn
object.notes = notes
try context.save()
let objects = try context.fetch(fetchRequest)
print(objects.count)
for object in objects {
object.name = name
object.style = style
object.buyAgain = buyAgain
object.roaster = roaster
object.roastedOn = roastedOn
object.boughtOn = boughtOn
object.notes = notes
try context.save()
}
} catch {
print("❌ Error")
print("\(error)")
}
}
}
14 changes: 14 additions & 0 deletions CoffeeTracker/CoffeeTracker/Beans/NewBeansView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import SwiftUI

struct NewBeansView: View {
enum Field {
case name
case style
case roaster
case notes
}
@Environment(\.managedObjectContext) private var viewContext
@Binding var showForm: Bool

Expand All @@ -18,6 +24,7 @@ struct NewBeansView: View {
roastedOn: Date(),
boughtOn: Date(),
notes: "")
@FocusState private var focusedField: Field?

var isEdit = false

Expand All @@ -27,15 +34,18 @@ struct NewBeansView: View {
Section("Bean Information") {
TextField("Name", text: $beans.name)
.textFieldStyle(.roundedBorder)
.focused($focusedField, equals: .name)
TextField("Style", text: $beans.style)
.textFieldStyle(.roundedBorder)
.focused($focusedField, equals: .style)
}
Section("Roaster Information") {
DatePicker("Roasted On",
selection: $beans.roastedOn,
displayedComponents: .date)
TextField("Roaster", text: $beans.roaster)
.textFieldStyle(.roundedBorder)
.focused($focusedField, equals: .roaster)
}
Section("Purchase Information") {
DatePicker("Purchased on",
Expand Down Expand Up @@ -75,6 +85,7 @@ struct NewBeansView: View {
boughtOn: Date(),
notes: "")
}
focusedField = nil
showForm = false
} label: {
Text("Save").padding(.horizontal, 30)
Expand All @@ -87,6 +98,9 @@ struct NewBeansView: View {

}.padding()
}.background(.ultraThickMaterial)
.onDisappear {
focusedField = nil
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions CoffeeTracker/CoffeeTracker/CoffeeTrackerMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ struct CoffeeTrackerMain: View {
var body: some View {
ZStack(alignment: .bottomTrailing) {
BeansCollectionView()
NewBeansView(showForm: $showButtons)
.opacity(showButtons ? 1 : 0)
if showButtons {
NewBeansView(showForm: $showButtons)
.opacity(showButtons ? 1 : 0)
}
Button(action: {showButtons.toggle()}) {
Image(systemName: SFSymbols.plus)
.padding(12)
Expand Down