comparison Simoleon/Favourites.swift @ 21:c3dda63f50ed v1.1

Added Core Data and UI changes - Implement Watchlist - Change conversion design - Improve UX
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Mon, 19 Jul 2021 19:27:12 +0100
parents
children 3596690dda73
comparison
equal deleted inserted replaced
20:f8aabe5b7251 21:c3dda63f50ed
1 //
2 // Favourites.swift
3 // Simoleon
4 //
5 // Created by Dennis Concepción Martín on 19/07/2021.
6 //
7
8 import SwiftUI
9
10 struct Favourites: View {
11 @Environment(\.managedObjectContext) private var viewContext
12 @FetchRequest(
13 sortDescriptors: [NSSortDescriptor(keyPath: \Favourite.currencyPair, ascending: true)],
14 animation: .default)
15 private var favourite: FetchedResults<Favourite>
16
17 var body: some View {
18 List {
19 ForEach(favourite) { favourite in
20 CurrencyRow(currencyPair: favourite.currencyPair)
21 }
22 .onDelete(perform: removeFromFavourites)
23 }
24 .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
25 NavigationView {
26 content
27 .navigationTitle("Favourites")
28 .toolbar {
29 #if os(iOS)
30 EditButton()
31 #endif
32 }
33 }
34 }
35 }
36
37 private func removeFromFavourites(offsets: IndexSet) {
38 withAnimation {
39 offsets.map { favourite[$0] }.forEach(viewContext.delete)
40
41 do {
42 try viewContext.save()
43 } catch {
44 let nsError = error as NSError
45 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
46 }
47 }
48 }
49 }
50
51 struct Favourites_Previews: PreviewProvider {
52 static var previews: some View {
53 Favourites()
54 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
55 }
56 }