// // StockList.swift // HomeStock // // Created by Karl-Peter Zons on 05.01.21. // import SwiftUI import CoreData import Foundation struct StockList: View { @Environment(\.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Stock.bezeichnung, ascending: true)], animation: .default) private var waren: FetchedResults<Stock> var body: some View { NavigationView { List { ForEach(waren) { ware in VStack(alignment: .leading) { Text("\(ware.bezeichnung!) in \(ware.mengenEinheit!)") HStack { Text("Datum zu: ") Text(dFormatter.string(from: ware.zugangDatum!)) Text("ab:") Text(dFormatter.string(from: ware.verbrauchDatum!)) } VStack { Text("Menge ist: \(ware.istMenge) min: \(ware.minMenge)") } } .frame(height: 60) .font(.footnote) } .onDelete(perform: deleteItems) } .navigationBarTitle(Text("Gesamt - Bestand"), displayMode: .inline) } } private func deleteItems(offsets: IndexSet) { withAnimation { offsets.map { waren[$0] }.forEach(viewContext.delete) 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)") } } } } private let dFormatter: DateFormatter = { let dFormatter = DateFormatter() dFormatter.locale = Locale(identifier: "fr_FR") dFormatter.dateStyle = .short dFormatter.timeStyle = .none return dFormatter }() struct StockList_Previews: PreviewProvider { static var previews: some View { StockList().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } }