diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simoleon/Favourites.swift	Mon Jul 19 19:27:12 2021 +0100
@@ -0,0 +1,56 @@
+//
+//  Favourites.swift
+//  Simoleon
+//
+//  Created by Dennis Concepción Martín on 19/07/2021.
+//
+
+import SwiftUI
+
+struct Favourites: View {
+    @Environment(\.managedObjectContext) private var viewContext
+    @FetchRequest(
+        sortDescriptors: [NSSortDescriptor(keyPath: \Favourite.currencyPair, ascending: true)],
+        animation: .default)
+    private var favourite: FetchedResults<Favourite>
+    
+    var body: some View {
+        List {
+            ForEach(favourite) { favourite in
+                CurrencyRow(currencyPair: favourite.currencyPair)
+            }
+            .onDelete(perform: removeFromFavourites)
+        }
+        .if(UIDevice.current.userInterfaceIdiom == .phone) { content in
+            NavigationView {
+                content
+                    .navigationTitle("Favourites")
+                    .toolbar {
+                        #if os(iOS)
+                        EditButton()
+                        #endif
+                    }
+            }
+        }
+    }
+    
+    private func removeFromFavourites(offsets: IndexSet) {
+        withAnimation {
+            offsets.map { favourite[$0] }.forEach(viewContext.delete)
+
+            do {
+                try viewContext.save()
+            } catch {
+                let nsError = error as NSError
+                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
+            }
+        }
+    }
+}
+
+struct Favourites_Previews: PreviewProvider {
+    static var previews: some View {
+        Favourites()
+            .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
+    }
+}