Mercurial > public > simoleon
diff Simoleon/ContentView.swift @ 183:d2398f02e1ce
implement unit currency selector
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Mon, 20 Dec 2021 12:28:16 +0100 |
parents | ba3ebe8cefe5 |
children | 1ebd1c5dd302 |
line wrap: on
line diff
--- a/Simoleon/ContentView.swift Wed Dec 08 10:58:15 2021 +0100 +++ b/Simoleon/ContentView.swift Mon Dec 20 12:28:16 2021 +0100 @@ -6,83 +6,50 @@ // import SwiftUI -import CoreData struct ContentView: View { - @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) + @State private var tab: Tab = .convert + + private enum Tab { + case convert, favorites, settings + } + + @ViewBuilder var adjustedView: some View { + if UIDevice.current.userInterfaceIdiom == .pad { + NavigationView { + } - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - EditButton() - } - ToolbarItem { - Button(action: addItem) { - Label("Add Item", systemImage: "plus") + } else { + TabView(selection: $tab) { + ConversionView() + .tabItem { + Label("Convert", systemImage: "arrow.counterclockwise.circle") } - } - } - Text("Select an item") - } - } + .tag(Tab.convert) + + Text("Favorites View") + .tabItem { + Label("Favorites", systemImage: "star") + } + .tag(Tab.favorites) - 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)") + Text("About View") + .tabItem { + Label("About", systemImage: "info.circle") + } + .tag(Tab.settings) } } } - 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)") - } - } + var body: some View { + adjustedView } } -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() } }