comparison LazyBear/Watchlist.swift @ 465:6953d83060a4

New design
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sat, 17 Jul 2021 17:58:57 +0100
parents
children
comparison
equal deleted inserted replaced
464:04e430ef254a 465:6953d83060a4
1 //
2 // Watchlist.swift
3 // lazybear
4 //
5 // Created by Dennis Concepción Martín on 17/07/2021.
6 //
7
8 import SwiftUI
9 import CoreData
10
11 struct Watchlist: View {
12 @Environment(\.managedObjectContext) private var viewContext
13 @FetchRequest(
14 sortDescriptors: [NSSortDescriptor(keyPath: \Company.symbol, ascending: true)],
15 animation: .default)
16 private var companies: FetchedResults<Company>
17
18 var body: some View {
19 List {
20 ForEach(companies) { company in
21 NavigationLink(destination: CompanyView(symbol: company.symbol)) {
22 WatchlistRow(symbol: company.symbol)
23 }
24 }
25 .onDelete(perform: deleteCompanyFromWatchlist)
26 }
27 .navigationTitle("Companies")
28 .toolbar {
29 #if os(iOS)
30 EditButton()
31 #endif
32 }
33 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
34 NavigationView { content }
35 }
36 }
37
38 private func deleteCompanyFromWatchlist(offsets: IndexSet) {
39 withAnimation {
40 offsets.map { companies[$0] }.forEach(viewContext.delete)
41
42 do {
43 try viewContext.save()
44 } catch {
45 let nsError = error as NSError
46 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
47 }
48 }
49 }
50 }
51
52 struct Watchlist_Previews: PreviewProvider {
53 static var previews: some View {
54 Watchlist()
55 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
56 }
57 }