comparison 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
comparison
equal deleted inserted replaced
180:41f284db0c3d 181:5a9430fd6b4d
1 // 1 //
2 // ContentView.swift 2 // ContentView.swift
3 // Simoleon 3 // simoleon
4 // 4 //
5 // Created by Dennis Concepción Martín on 26/10/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
9 10
10 struct ContentView: View { 11 struct ContentView: View {
11 @State private var tab: Tab = .convert 12 @Environment(\.managedObjectContext) private var viewContext
12 13
13 private enum Tab { 14 @FetchRequest(
14 case convert, favorites, settings 15 sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
16 animation: .default)
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 }
28 }
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 }
15 } 43 }
16
17 @ViewBuilder var adjustedView: some View {
18 if UIDevice.current.userInterfaceIdiom == .pad {
19 NavigationView {
20 Sidebar()
21 ConversionView()
22 }
23 } else {
24 TabView(selection: $tab) {
25 ConversionView()
26 .tabItem {
27 Label("Convert", systemImage: "arrow.counterclockwise.circle")
28 }
29 .tag(Tab.convert)
30
31 FavoritesView()
32 .tabItem {
33 Label("Favorites", systemImage: "star")
34 }
35 .tag(Tab.favorites)
36 44
37 AboutView() 45 private func addItem() {
38 .tabItem { 46 withAnimation {
39 Label("About", systemImage: "info.circle") 47 let newItem = Item(context: viewContext)
40 } 48 newItem.timestamp = Date()
41 .tag(Tab.settings) 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)")
42 } 57 }
43 } 58 }
44 } 59 }
45 60
46 var body: some View { 61 private func deleteItems(offsets: IndexSet) {
47 adjustedView 62 withAnimation {
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 }
48 } 74 }
49 } 75 }
50 76
77 private let itemFormatter: DateFormatter = {
78 let formatter = DateFormatter()
79 formatter.dateStyle = .short
80 formatter.timeStyle = .medium
81 return formatter
82 }()
83
51 struct ContentView_Previews: PreviewProvider { 84 struct ContentView_Previews: PreviewProvider {
52 static var previews: some View { 85 static var previews: some View {
53 ContentView() 86 ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
54 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
55 } 87 }
56 } 88 }