Mercurial > public > simoleon
comparison 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 |
comparison
equal
deleted
inserted
replaced
182:ba3ebe8cefe5 | 183:d2398f02e1ce |
---|---|
4 // | 4 // |
5 // Created by Dennis Concepción Martín on 8/12/21. | 5 // Created by Dennis Concepción Martín on 8/12/21. |
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 import CoreData | |
10 | 9 |
11 struct ContentView: View { | 10 struct ContentView: View { |
12 @Environment(\.managedObjectContext) private var viewContext | 11 @State private var tab: Tab = .convert |
12 | |
13 private enum Tab { | |
14 case convert, favorites, settings | |
15 } | |
16 | |
17 @ViewBuilder var adjustedView: some View { | |
18 if UIDevice.current.userInterfaceIdiom == .pad { | |
19 NavigationView { | |
20 | |
21 } | |
22 } else { | |
23 TabView(selection: $tab) { | |
24 ConversionView() | |
25 .tabItem { | |
26 Label("Convert", systemImage: "arrow.counterclockwise.circle") | |
27 } | |
28 .tag(Tab.convert) | |
29 | |
30 Text("Favorites View") | |
31 .tabItem { | |
32 Label("Favorites", systemImage: "star") | |
33 } | |
34 .tag(Tab.favorites) | |
13 | 35 |
14 @FetchRequest( | 36 Text("About View") |
15 sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], | 37 .tabItem { |
16 animation: .default) | 38 Label("About", systemImage: "info.circle") |
17 private var items: FetchedResults<Item> | |
18 | |
19 var body: some View { | |
20 NavigationView { | |
21 List { | |
22 ForEach(items) { item in | |
23 NavigationLink { | |
24 Text("Item at \(item.timestamp!, formatter: itemFormatter)") | |
25 } label: { | |
26 Text(item.timestamp!, formatter: itemFormatter) | |
27 } | 39 } |
28 } | 40 .tag(Tab.settings) |
29 .onDelete(perform: deleteItems) | |
30 } | |
31 .toolbar { | |
32 ToolbarItem(placement: .navigationBarTrailing) { | |
33 EditButton() | |
34 } | |
35 ToolbarItem { | |
36 Button(action: addItem) { | |
37 Label("Add Item", systemImage: "plus") | |
38 } | |
39 } | |
40 } | |
41 Text("Select an item") | |
42 } | |
43 } | |
44 | |
45 private func addItem() { | |
46 withAnimation { | |
47 let newItem = Item(context: viewContext) | |
48 newItem.timestamp = Date() | |
49 | |
50 do { | |
51 try viewContext.save() | |
52 } catch { | |
53 // Replace this implementation with code to handle the error appropriately. | |
54 // 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. | |
55 let nsError = error as NSError | |
56 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
57 } | 41 } |
58 } | 42 } |
59 } | 43 } |
60 | 44 |
61 private func deleteItems(offsets: IndexSet) { | 45 var body: some View { |
62 withAnimation { | 46 adjustedView |
63 offsets.map { items[$0] }.forEach(viewContext.delete) | |
64 | |
65 do { | |
66 try viewContext.save() | |
67 } catch { | |
68 // Replace this implementation with code to handle the error appropriately. | |
69 // 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. | |
70 let nsError = error as NSError | |
71 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
72 } | |
73 } | |
74 } | 47 } |
75 } | 48 } |
76 | 49 |
77 private let itemFormatter: DateFormatter = { | |
78 let formatter = DateFormatter() | |
79 formatter.dateStyle = .short | |
80 formatter.timeStyle = .medium | |
81 return formatter | |
82 }() | |
83 | 50 |
84 struct ContentView_Previews: PreviewProvider { | 51 struct ContentView_Previews: PreviewProvider { |
85 static var previews: some View { | 52 static var previews: some View { |
86 ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) | 53 ContentView() |
87 } | 54 } |
88 } | 55 } |