Mercurial > public > simoleon
diff Simoleon/ContentView.swift @ 181:5a9430fd6b4d
restart project to new version
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Wed, 08 Dec 2021 10:52:11 +0100 |
parents | 7c4a789e51ba |
children | ba3ebe8cefe5 |
line wrap: on
line diff
--- a/Simoleon/ContentView.swift Wed Dec 08 10:47:02 2021 +0100 +++ b/Simoleon/ContentView.swift Wed Dec 08 10:52:11 2021 +0100 @@ -1,56 +1,88 @@ // // ContentView.swift -// Simoleon +// simoleon // -// Created by Dennis Concepción Martín on 26/10/21. +// Created by Dennis Concepción Martín on 8/12/21. // import SwiftUI +import CoreData struct ContentView: View { - @State private var tab: Tab = .convert - - private enum Tab { - case convert, favorites, settings - } - - @ViewBuilder var adjustedView: some View { - if UIDevice.current.userInterfaceIdiom == .pad { - NavigationView { - Sidebar() - ConversionView() + @Environment(\.managedObjectContext) private var viewContext + + @FetchRequest( + sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], + animation: .default) + private var items: FetchedResults<Item> + + var body: some View { + NavigationView { + List { + ForEach(items) { item in + NavigationLink { + Text("Item at \(item.timestamp!, formatter: itemFormatter)") + } label: { + Text(item.timestamp!, formatter: itemFormatter) + } + } + .onDelete(perform: deleteItems) } - } else { - TabView(selection: $tab) { - ConversionView() - .tabItem { - Label("Convert", systemImage: "arrow.counterclockwise.circle") + .toolbar { + ToolbarItem(placement: .navigationBarTrailing) { + EditButton() + } + ToolbarItem { + Button(action: addItem) { + Label("Add Item", systemImage: "plus") } - .tag(Tab.convert) - - FavoritesView() - .tabItem { - Label("Favorites", systemImage: "star") - } - .tag(Tab.favorites) + } + } + Text("Select an item") + } + } - AboutView() - .tabItem { - Label("About", systemImage: "info.circle") - } - .tag(Tab.settings) + private func addItem() { + withAnimation { + let newItem = Item(context: viewContext) + newItem.timestamp = 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)") } } } - - var body: some View { - adjustedView + + private func deleteItems(offsets: IndexSet) { + withAnimation { + offsets.map { items[$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 itemFormatter: DateFormatter = { + let formatter = DateFormatter() + formatter.dateStyle = .short + formatter.timeStyle = .medium + return formatter +}() + struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView() - .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) + ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } }